diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-07 16:40:54 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-07 16:42:03 +0200 |
commit | 9669bf29f61591990f26688ae5cdd331bd5c6fa1 (patch) | |
tree | d10218ea56e4db61110892884fe555c2765867ed | |
parent | a01b19c878f7c6971d1ffbde039d4792e7446990 (diff) | |
download | serenity-9669bf29f61591990f26688ae5cdd331bd5c6fa1.zip |
Kernel: Make Device request creation return KResultOr
This allows us to propagate errors in a bunch of new places.
-rw-r--r-- | Kernel/Devices/BlockDevice.cpp | 14 | ||||
-rw-r--r-- | Kernel/Devices/Device.h | 4 | ||||
-rw-r--r-- | Kernel/Storage/Partition/DiskPartition.cpp | 7 | ||||
-rw-r--r-- | Kernel/Storage/StorageDevice.cpp | 10 |
4 files changed, 24 insertions, 11 deletions
diff --git a/Kernel/Devices/BlockDevice.cpp b/Kernel/Devices/BlockDevice.cpp index 094692ab3d..524b7ee951 100644 --- a/Kernel/Devices/BlockDevice.cpp +++ b/Kernel/Devices/BlockDevice.cpp @@ -30,7 +30,12 @@ BlockDevice::~BlockDevice() bool BlockDevice::read_block(u64 index, UserOrKernelBuffer& buffer) { - auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, 1, buffer, 512); + auto read_request_or_error = try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, 1, buffer, 512); + if (read_request_or_error.is_error()) { + dbgln("BlockDevice::read_block({}): try_make_request failed", index); + return false; + } + auto read_request = read_request_or_error.release_value(); switch (read_request->wait().request_result()) { case AsyncDeviceRequest::Success: return true; @@ -51,7 +56,12 @@ bool BlockDevice::read_block(u64 index, UserOrKernelBuffer& buffer) bool BlockDevice::write_block(u64 index, const UserOrKernelBuffer& buffer) { - auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, 1, buffer, 512); + auto write_request_or_error = try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, 1, buffer, 512); + if (write_request_or_error.is_error()) { + dbgln("BlockDevice::write_block({}): try_make_request failed", index); + return false; + } + auto write_request = write_request_or_error.release_value(); switch (write_request->wait().request_result()) { case AsyncDeviceRequest::Success: return true; diff --git a/Kernel/Devices/Device.h b/Kernel/Devices/Device.h index e39b4e80e4..776dfa2dac 100644 --- a/Kernel/Devices/Device.h +++ b/Kernel/Devices/Device.h @@ -48,9 +48,9 @@ public: void process_next_queued_request(Badge<AsyncDeviceRequest>, const AsyncDeviceRequest&); template<typename AsyncRequestType, typename... Args> - NonnullRefPtr<AsyncRequestType> make_request(Args&&... args) + KResultOr<NonnullRefPtr<AsyncRequestType>> try_make_request(Args&&... args) { - auto request = adopt_ref(*new AsyncRequestType(*this, forward<Args>(args)...)); + auto request = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AsyncRequestType(*this, forward<Args>(args)...))); SpinlockLocker lock(m_requests_lock); bool was_empty = m_requests.is_empty(); m_requests.append(request); diff --git a/Kernel/Storage/Partition/DiskPartition.cpp b/Kernel/Storage/Partition/DiskPartition.cpp index 899af240e6..4ba56aa3ca 100644 --- a/Kernel/Storage/Partition/DiskPartition.cpp +++ b/Kernel/Storage/Partition/DiskPartition.cpp @@ -33,8 +33,11 @@ const DiskPartitionMetadata& DiskPartition::metadata() const void DiskPartition::start_request(AsyncBlockDeviceRequest& request) { - request.add_sub_request(m_device->make_request<AsyncBlockDeviceRequest>(request.request_type(), - request.block_index() + m_metadata.start_block(), request.block_count(), request.buffer(), request.buffer_size())); + auto sub_request_or_error = m_device->try_make_request<AsyncBlockDeviceRequest>(request.request_type(), + 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()); } KResultOr<size_t> DiskPartition::read(OpenFileDescription& fd, u64 offset, UserOrKernelBuffer& outbuf, size_t len) diff --git a/Kernel/Storage/StorageDevice.cpp b/Kernel/Storage/StorageDevice.cpp index 594fb5470f..1786d81ca7 100644 --- a/Kernel/Storage/StorageDevice.cpp +++ b/Kernel/Storage/StorageDevice.cpp @@ -55,7 +55,7 @@ KResultOr<size_t> StorageDevice::read(OpenFileDescription&, u64 offset, UserOrKe dbgln_if(STORAGE_DEVICE_DEBUG, "StorageDevice::read() index={}, whole_blocks={}, remaining={}", index, whole_blocks, remaining); if (whole_blocks > 0) { - auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, whole_blocks, outbuf, whole_blocks * block_size()); + auto read_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, whole_blocks, outbuf, whole_blocks * block_size())); auto result = read_request->wait(); if (result.wait_result().was_interrupted()) return EINTR; @@ -78,7 +78,7 @@ KResultOr<size_t> StorageDevice::read(OpenFileDescription&, u64 offset, UserOrKe return ENOMEM; auto data = data_result.release_value(); auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data()); - auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size()); + auto read_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size())); auto result = read_request->wait(); if (result.wait_result().was_interrupted()) return EINTR; @@ -122,7 +122,7 @@ KResultOr<size_t> StorageDevice::write(OpenFileDescription&, u64 offset, const U dbgln_if(STORAGE_DEVICE_DEBUG, "StorageDevice::write() index={}, whole_blocks={}, remaining={}", index, whole_blocks, remaining); if (whole_blocks > 0) { - auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, whole_blocks, inbuf, whole_blocks * block_size()); + auto write_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, whole_blocks, inbuf, whole_blocks * block_size())); auto result = write_request->wait(); if (result.wait_result().was_interrupted()) return EINTR; @@ -148,7 +148,7 @@ KResultOr<size_t> StorageDevice::write(OpenFileDescription&, u64 offset, const U auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data()); { - auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size()); + auto read_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size())); auto result = read_request->wait(); if (result.wait_result().was_interrupted()) return EINTR; @@ -168,7 +168,7 @@ KResultOr<size_t> StorageDevice::write(OpenFileDescription&, u64 offset, const U TRY(inbuf.read(data.data(), pos, remaining)); { - auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index + whole_blocks, 1, data_buffer, block_size()); + auto write_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index + whole_blocks, 1, data_buffer, block_size())); auto result = write_request->wait(); if (result.wait_result().was_interrupted()) return EINTR; |