diff options
Diffstat (limited to 'Kernel')
29 files changed, 85 insertions, 118 deletions
diff --git a/Kernel/ACPI/Parser.cpp b/Kernel/ACPI/Parser.cpp index 0a7fb3e10c..e989d3a654 100644 --- a/Kernel/ACPI/Parser.cpp +++ b/Kernel/ACPI/Parser.cpp @@ -41,8 +41,7 @@ KResultOr<size_t> ACPISysFSComponent::read_bytes(off_t offset, size_t count, Use return KSuccess; ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count)); - if (!buffer.write(blob->data() + offset, nread)) - return KResult(EFAULT); + TRY(buffer.write(blob->data() + offset, nread)); return nread; } diff --git a/Kernel/Arch/PC/BIOS.cpp b/Kernel/Arch/PC/BIOS.cpp index 40a96b7839..54203831e5 100644 --- a/Kernel/Arch/PC/BIOS.cpp +++ b/Kernel/Arch/PC/BIOS.cpp @@ -39,8 +39,7 @@ KResultOr<size_t> BIOSSysFSComponent::read_bytes(off_t offset, size_t count, Use return KSuccess; ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count)); - if (!buffer.write(blob->data() + offset, nread)) - return KResult(EFAULT); + TRY(buffer.write(blob->data() + offset, nread)); return nread; } diff --git a/Kernel/Bus/PCI/SysFSPCI.cpp b/Kernel/Bus/PCI/SysFSPCI.cpp index ff90776cd5..3a7506c5ff 100644 --- a/Kernel/Bus/PCI/SysFSPCI.cpp +++ b/Kernel/Bus/PCI/SysFSPCI.cpp @@ -70,8 +70,7 @@ KResultOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, siz return KSuccess; ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count)); - if (!buffer.write(blob->data() + offset, nread)) - return KResult(EFAULT); + TRY(buffer.write(blob->data() + offset, nread)); return nread; } diff --git a/Kernel/Bus/USB/SysFSUSB.cpp b/Kernel/Bus/USB/SysFSUSB.cpp index ab8a57f387..bd4549e10e 100644 --- a/Kernel/Bus/USB/SysFSUSB.cpp +++ b/Kernel/Bus/USB/SysFSUSB.cpp @@ -87,9 +87,7 @@ KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t cou return 0; ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count)); - if (!buffer.write(data_buffer->data() + offset, nread)) - return KResult(EFAULT); - + TRY(buffer.write(data_buffer->data() + offset, nread)); return nread; } diff --git a/Kernel/Devices/AsyncDeviceRequest.h b/Kernel/Devices/AsyncDeviceRequest.h index 2afbce1838..a7c2663b50 100644 --- a/Kernel/Devices/AsyncDeviceRequest.h +++ b/Kernel/Devices/AsyncDeviceRequest.h @@ -81,7 +81,7 @@ public: void* get_private() const { return m_private; } template<typename... Args> - [[nodiscard]] bool write_to_buffer(UserOrKernelBuffer& buffer, Args... args) + KResult write_to_buffer(UserOrKernelBuffer& buffer, Args... args) { if (in_target_context(buffer)) return buffer.write(forward<Args>(args)...); @@ -99,7 +99,7 @@ public: } template<typename... Args> - [[nodiscard]] bool read_from_buffer(const UserOrKernelBuffer& buffer, Args... args) + KResult read_from_buffer(const UserOrKernelBuffer& buffer, Args... args) { if (in_target_context(buffer)) return buffer.read(forward<Args>(args)...); diff --git a/Kernel/Devices/FullDevice.cpp b/Kernel/Devices/FullDevice.cpp index 20e2e7543a..de5aa9c344 100644 --- a/Kernel/Devices/FullDevice.cpp +++ b/Kernel/Devices/FullDevice.cpp @@ -32,8 +32,7 @@ bool FullDevice::can_read(const FileDescription&, size_t) const KResultOr<size_t> FullDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { - if (!buffer.memset(0, size)) - return EFAULT; + TRY(buffer.memset(0, size)); return size; } diff --git a/Kernel/Devices/HID/MouseDevice.cpp b/Kernel/Devices/HID/MouseDevice.cpp index 80c4a1ef73..6ad5658900 100644 --- a/Kernel/Devices/HID/MouseDevice.cpp +++ b/Kernel/Devices/HID/MouseDevice.cpp @@ -39,8 +39,7 @@ KResultOr<size_t> MouseDevice::read(FileDescription&, u64, UserOrKernelBuffer& b dbgln_if(MOUSE_DEBUG, "PS2 Mouse Read: Filter packets"); size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket)); - if (!buffer.write(&packet, nread, bytes_read_from_packet)) - return EFAULT; + TRY(buffer.write(&packet, nread, bytes_read_from_packet)); nread += bytes_read_from_packet; remaining_space_in_buffer -= bytes_read_from_packet; diff --git a/Kernel/Devices/SB16.cpp b/Kernel/Devices/SB16.cpp index ebeb6cfc2d..a22571f1a8 100644 --- a/Kernel/Devices/SB16.cpp +++ b/Kernel/Devices/SB16.cpp @@ -276,8 +276,7 @@ KResultOr<size_t> SB16::write(FileDescription&, u64, UserOrKernelBuffer const& d u8 mode = (u8)SampleFormat::Signed | (u8)SampleFormat::Stereo; - if (!data.read(m_dma_region->vaddr().as_ptr(), length)) - return EFAULT; + TRY(data.read(m_dma_region->vaddr().as_ptr(), length)); dma_start(length); // 16-bit single-cycle output. diff --git a/Kernel/Devices/ZeroDevice.cpp b/Kernel/Devices/ZeroDevice.cpp index bf89706930..2e2d7af96f 100644 --- a/Kernel/Devices/ZeroDevice.cpp +++ b/Kernel/Devices/ZeroDevice.cpp @@ -31,8 +31,7 @@ bool ZeroDevice::can_read(const FileDescription&, size_t) const KResultOr<size_t> ZeroDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { - if (!buffer.memset(0, size)) - return EFAULT; + TRY(buffer.memset(0, size)); return size; } diff --git a/Kernel/DoubleBuffer.cpp b/Kernel/DoubleBuffer.cpp index 1502052d90..79f72eedc7 100644 --- a/Kernel/DoubleBuffer.cpp +++ b/Kernel/DoubleBuffer.cpp @@ -57,8 +57,7 @@ KResultOr<size_t> DoubleBuffer::write(const UserOrKernelBuffer& data, size_t siz MutexLocker locker(m_lock); size_t bytes_to_write = min(size, m_space_for_writing); u8* write_ptr = m_write_buffer->data + m_write_buffer->size; - if (!data.read(write_ptr, bytes_to_write)) - return EFAULT; + TRY(data.read(write_ptr, bytes_to_write)); m_write_buffer->size += bytes_to_write; compute_lockfree_metadata(); if (m_unblock_callback && !m_empty) @@ -76,8 +75,7 @@ KResultOr<size_t> DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) if (m_read_buffer_index >= m_read_buffer->size) return 0; size_t nread = min(m_read_buffer->size - m_read_buffer_index, size); - if (!data.write(m_read_buffer->data + m_read_buffer_index, nread)) - return EFAULT; + TRY(data.write(m_read_buffer->data + m_read_buffer_index, nread)); m_read_buffer_index += nread; compute_lockfree_metadata(); if (m_unblock_callback && m_space_for_writing > 0) @@ -96,8 +94,7 @@ KResultOr<size_t> DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size) if (m_read_buffer_index >= m_read_buffer->size) return 0; size_t nread = min(m_read_buffer->size - m_read_buffer_index, size); - if (!data.write(m_read_buffer->data + m_read_buffer_index, nread)) - return EFAULT; + TRY(data.write(m_read_buffer->data + m_read_buffer_index, nread)); compute_lockfree_metadata(); if (m_unblock_callback && m_space_for_writing > 0) m_unblock_callback(); diff --git a/Kernel/FileSystem/BlockBasedFileSystem.cpp b/Kernel/FileSystem/BlockBasedFileSystem.cpp index 5254364e58..cfc02a650b 100644 --- a/Kernel/FileSystem/BlockBasedFileSystem.cpp +++ b/Kernel/FileSystem/BlockBasedFileSystem.cpp @@ -156,8 +156,7 @@ KResult BlockBasedFileSystem::write_block(BlockIndex index, const UserOrKernelBu // Fill the cache first. TRY(read_block(index, nullptr, block_size())); } - if (!data.read(entry.data + offset, count)) - return EFAULT; + TRY(data.read(entry.data + offset, count)); cache->mark_dirty(entry); entry.has_data = true; @@ -238,8 +237,8 @@ KResult BlockBasedFileSystem::read_block(BlockIndex index, UserOrKernelBuffer* b VERIFY(nread == block_size()); entry.has_data = true; } - if (buffer && !buffer->write(entry.data + offset, count)) - return EFAULT; + if (buffer) + TRY(buffer->write(entry.data + offset, count)); return KSuccess; }); } diff --git a/Kernel/FileSystem/DevFS.cpp b/Kernel/FileSystem/DevFS.cpp index 841197c216..3b2f5a12d6 100644 --- a/Kernel/FileSystem/DevFS.cpp +++ b/Kernel/FileSystem/DevFS.cpp @@ -154,8 +154,7 @@ KResultOr<size_t> DevFSLinkInode::read_bytes(off_t offset, size_t, UserOrKernelB MutexLocker locker(m_inode_lock); VERIFY(offset == 0); VERIFY(m_link); - if (!buffer.write(m_link->characters() + offset, m_link->length())) - return EFAULT; + TRY(buffer.write(m_link->characters() + offset, m_link->length())); return m_link->length(); } diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index f7b9540e9a..cc023aabc2 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -842,8 +842,7 @@ KResultOr<size_t> Ext2FSInode::read_bytes(off_t offset, size_t count, UserOrKern if (is_symlink() && size() < max_inline_symlink_length) { VERIFY(offset == 0); size_t nread = min((off_t)size() - offset, static_cast<off_t>(count)); - if (!buffer.write(((const u8*)m_raw_inode.i_block) + offset, nread)) - return EFAULT; + TRY(buffer.write(((const u8*)m_raw_inode.i_block) + offset, nread)); return nread; } @@ -878,8 +877,7 @@ KResultOr<size_t> Ext2FSInode::read_bytes(off_t offset, size_t count, UserOrKern auto buffer_offset = buffer.offset(nread); if (block_index.value() == 0) { // This is a hole, act as if it's filled with zeroes. - if (!buffer_offset.memset(0, num_bytes_to_copy)) - return EFAULT; + TRY(buffer_offset.memset(0, num_bytes_to_copy)); } else { if (auto result = fs().read_block(block_index, &buffer_offset, num_bytes_to_copy, offset_into_block, allow_cache); result.is_error()) { dmesgln("Ext2FSInode[{}]::read_bytes(): Failed to read block {} (index {})", identifier(), block_index.value(), bi); @@ -982,8 +980,7 @@ KResultOr<size_t> Ext2FSInode::write_bytes(off_t offset, size_t count, const Use VERIFY(offset == 0); if (max((size_t)(offset + count), (size_t)m_raw_inode.i_size) < max_inline_symlink_length) { dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]::write_bytes(): Poking into i_block array for inline symlink ({} bytes)", identifier(), count); - if (!data.read(((u8*)m_raw_inode.i_block) + offset, (size_t)count)) - return EFAULT; + TRY(data.read(((u8*)m_raw_inode.i_block) + offset, count)); if ((size_t)(offset + count) > (size_t)m_raw_inode.i_size) m_raw_inode.i_size = offset + count; set_metadata_dirty(true); diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp index fd4a49c3a0..6d29027a38 100644 --- a/Kernel/FileSystem/FileDescription.cpp +++ b/Kernel/FileSystem/FileDescription.cpp @@ -222,8 +222,9 @@ KResultOr<size_t> FileDescription::get_dir_entries(UserOrKernelBuffer& output_bu if (remaining < stream.size()) { error = EINVAL; return false; - } else if (!output_buffer.write(stream.bytes())) { - error = EFAULT; + } + if (auto result = output_buffer.write(stream.bytes()); result.is_error()) { + error = result.release_error(); return false; } output_buffer = output_buffer.offset(stream.size()); diff --git a/Kernel/FileSystem/ISO9660FileSystem.cpp b/Kernel/FileSystem/ISO9660FileSystem.cpp index 4024fa139c..ed72007252 100644 --- a/Kernel/FileSystem/ISO9660FileSystem.cpp +++ b/Kernel/FileSystem/ISO9660FileSystem.cpp @@ -446,10 +446,7 @@ KResultOr<size_t> ISO9660Inode::read_bytes(off_t offset, size_t size, UserOrKern return EIO; } - bool result = buffer_offset.write(block->data() + initial_offset, bytes_to_read); - if (!result) { - return EFAULT; - } + TRY(buffer_offset.write(block->data() + initial_offset, bytes_to_read)); nread += bytes_to_read; initial_offset = 0; diff --git a/Kernel/FileSystem/Plan9FileSystem.cpp b/Kernel/FileSystem/Plan9FileSystem.cpp index 2532ee1eac..40e5f47936 100644 --- a/Kernel/FileSystem/Plan9FileSystem.cpp +++ b/Kernel/FileSystem/Plan9FileSystem.cpp @@ -745,9 +745,7 @@ KResultOr<size_t> Plan9FSInode::read_bytes(off_t offset, size_t size, UserOrKern // Guard against the server returning more data than requested. size_t nread = min(data.length(), size); - if (!buffer.write(data.characters_without_null_termination(), nread)) - return EFAULT; - + TRY(buffer.write(data.characters_without_null_termination(), nread)); return nread; } diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 3c5b4d9b7a..c5401594f5 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -489,8 +489,7 @@ KResultOr<size_t> ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t co if (!data_buffer) return KResult(EFAULT); ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count)); - if (!buffer.write(data_buffer->data() + offset, nread)) - return KResult(EFAULT); + TRY(buffer.write(data_buffer->data() + offset, nread)); return nread; } if (!description->data()) { @@ -507,8 +506,7 @@ KResultOr<size_t> ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t co return 0; ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count)); - if (!buffer.write(data_buffer->data() + offset, nread)) - return KResult(EFAULT); + TRY(buffer.write(data_buffer->data() + offset, nread)); return nread; } diff --git a/Kernel/FileSystem/TmpFS.cpp b/Kernel/FileSystem/TmpFS.cpp index 8d48ac82a8..cf013adf03 100644 --- a/Kernel/FileSystem/TmpFS.cpp +++ b/Kernel/FileSystem/TmpFS.cpp @@ -138,8 +138,7 @@ KResultOr<size_t> TmpFSInode::read_bytes(off_t offset, size_t size, UserOrKernel if (static_cast<off_t>(size) > m_metadata.size - offset) size = m_metadata.size - offset; - if (!buffer.write(m_content->data() + offset, size)) - return EFAULT; + TRY(buffer.write(m_content->data() + offset, size)); return size; } @@ -182,8 +181,7 @@ KResultOr<size_t> TmpFSInode::write_bytes(off_t offset, size_t size, const UserO notify_watchers(); } - if (!buffer.read(m_content->data() + offset, size)) // TODO: partial reads? - return EFAULT; + TRY(buffer.read(m_content->data() + offset, size)); // TODO: partial reads? did_modify_contents(); return size; diff --git a/Kernel/Memory/RingBuffer.cpp b/Kernel/Memory/RingBuffer.cpp index 96a82e54dc..06d2da02ff 100644 --- a/Kernel/Memory/RingBuffer.cpp +++ b/Kernel/Memory/RingBuffer.cpp @@ -22,20 +22,18 @@ bool RingBuffer::copy_data_in(const UserOrKernelBuffer& buffer, size_t offset, s bytes_copied = min(m_capacity_in_bytes - m_num_used_bytes, min(m_capacity_in_bytes - start_of_free_area, length)); if (bytes_copied == 0) return false; - if (buffer.read(m_region->vaddr().offset(start_of_free_area).as_ptr(), offset, bytes_copied)) { - m_num_used_bytes += bytes_copied; - start_of_copied_data = m_region->physical_page(start_of_free_area / PAGE_SIZE)->paddr().offset(start_of_free_area % PAGE_SIZE); - return true; - } - return false; + if (auto result = buffer.read(m_region->vaddr().offset(start_of_free_area).as_ptr(), offset, bytes_copied); result.is_error()) + return false; + m_num_used_bytes += bytes_copied; + start_of_copied_data = m_region->physical_page(start_of_free_area / PAGE_SIZE)->paddr().offset(start_of_free_area % PAGE_SIZE); + return true; } KResultOr<size_t> RingBuffer::copy_data_out(size_t size, UserOrKernelBuffer& buffer) const { auto start = m_start_of_used % m_capacity_in_bytes; auto num_bytes = min(min(m_num_used_bytes, size), m_capacity_in_bytes - start); - if (!buffer.write(m_region->vaddr().offset(start).as_ptr(), num_bytes)) - return EIO; + TRY(buffer.write(m_region->vaddr().offset(start).as_ptr(), num_bytes)); return num_bytes; } diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 621a440e4b..0e88cc1fc1 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -236,9 +236,9 @@ KResultOr<size_t> IPv4Socket::sendto(FileDescription&, const UserOrKernelBuffer& return set_so_error(ENOMEM); routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(), routing_decision.next_hop, m_peer_address, (IPv4Protocol)protocol(), data_length, m_ttl); - if (!data.read(packet->buffer->data() + ipv4_payload_offset, data_length)) { + if (auto result = data.read(packet->buffer->data() + ipv4_payload_offset, data_length); result.is_error()) { routing_decision.adapter->release_packet_buffer(*packet); - return set_so_error(EFAULT); + return set_so_error(result); } routing_decision.adapter->send_packet(packet->bytes()); routing_decision.adapter->release_packet_buffer(*packet); @@ -371,8 +371,8 @@ KResultOr<size_t> IPv4Socket::receive_packet_buffered(FileDescription& descripti if (type() == SOCK_RAW) { size_t bytes_written = min(packet.data.value().size(), buffer_length); - if (!buffer.write(packet.data.value().data(), bytes_written)) - return set_so_error(EFAULT); + if (auto result = buffer.write(packet.data.value().data(), bytes_written); result.is_error()) + return set_so_error(result); return bytes_written; } diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index 740696ef6a..06b01cbda9 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -169,8 +169,8 @@ KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, Use size_t payload_size = raw_ipv4_packet.size() - sizeof(IPv4Packet) - tcp_packet.header_size(); dbgln_if(TCP_SOCKET_DEBUG, "payload_size {}, will it fit in {}?", payload_size, buffer_size); VERIFY(buffer_size >= payload_size); - if (!buffer.write(tcp_packet.payload(), payload_size)) - return set_so_error(EFAULT); + if (auto result = buffer.write(tcp_packet.payload(), payload_size); result.is_error()) + return set_so_error(result); return payload_size; } @@ -226,9 +226,11 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, tcp_packet.set_ack_number(m_ack_number); } - if (payload && !payload->read(tcp_packet.payload(), payload_size)) { - routing_decision.adapter->release_packet_buffer(*packet); - return set_so_error(EFAULT); + if (payload) { + if (auto result = payload->read(tcp_packet.payload(), payload_size); result.is_error()) { + routing_decision.adapter->release_packet_buffer(*packet); + return set_so_error(result); + } } if (flags & TCPFlags::SYN) { diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp index ef8878ad0a..477b7df6d8 100644 --- a/Kernel/Net/UDPSocket.cpp +++ b/Kernel/Net/UDPSocket.cpp @@ -65,8 +65,8 @@ KResultOr<size_t> UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, Use auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload()); VERIFY(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier. size_t read_size = min(buffer_size, udp_packet.length() - sizeof(UDPPacket)); - if (!buffer.write(udp_packet.payload(), read_size)) - return set_so_error(EFAULT); + if (auto result = buffer.write(udp_packet.payload(), read_size); result.is_error()) + return set_so_error(result); return read_size; } @@ -86,8 +86,8 @@ KResultOr<size_t> UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_ udp_packet.set_source_port(local_port()); udp_packet.set_destination_port(peer_port()); udp_packet.set_length(udp_buffer_size); - if (!data.read(udp_packet.payload(), data_length)) - return set_so_error(EFAULT); + if (auto result = data.read(udp_packet.payload(), data_length); result.is_error()) + return set_so_error(result); routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(), routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, udp_buffer_size, ttl()); diff --git a/Kernel/ProcessExposed.cpp b/Kernel/ProcessExposed.cpp index 87e7956539..b5a3af6605 100644 --- a/Kernel/ProcessExposed.cpp +++ b/Kernel/ProcessExposed.cpp @@ -131,9 +131,7 @@ KResultOr<size_t> ProcFSGlobalInformation::read_bytes(off_t offset, size_t count return 0; ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count)); - if (!buffer.write(data_buffer->data() + offset, nread)) - return KResult(EFAULT); - + TRY(buffer.write(data_buffer->data() + offset, nread)); return nread; } @@ -167,8 +165,7 @@ KResultOr<size_t> ProcFSExposedLink::read_bytes(off_t offset, size_t count, User return KResult(EFAULT); ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count)); - if (!buffer.write(blob->data() + offset, nread)) - return KResult(EFAULT); + TRY(buffer.write(blob->data() + offset, nread)); return nread; } diff --git a/Kernel/Storage/AHCIPort.cpp b/Kernel/Storage/AHCIPort.cpp index cde1054c21..9840a78da4 100644 --- a/Kernel/Storage/AHCIPort.cpp +++ b/Kernel/Storage/AHCIPort.cpp @@ -103,7 +103,7 @@ void AHCIPort::handle_interrupt() VERIFY(m_current_request); VERIFY(m_current_scatter_list); if (m_current_request->request_type() == AsyncBlockDeviceRequest::Read) { - if (!m_current_request->write_to_buffer(m_current_request->buffer(), m_current_scatter_list->dma_region().as_ptr(), m_connected_device->block_size() * m_current_request->block_count())) { + if (auto result = m_current_request->write_to_buffer(m_current_request->buffer(), m_current_scatter_list->dma_region().as_ptr(), m_connected_device->block_size() * m_current_request->block_count()); result.is_error()) { dbgln_if(AHCI_DEBUG, "AHCI Port {}: Request failure, memory fault occurred when reading in data.", representative_port_index()); m_current_scatter_list = nullptr; complete_current_request(AsyncDeviceRequest::MemoryFault); @@ -445,7 +445,7 @@ Optional<AsyncDeviceRequest::RequestResult> AHCIPort::prepare_and_set_scatter_li if (!m_current_scatter_list) return AsyncDeviceRequest::Failure; if (request.request_type() == AsyncBlockDeviceRequest::Write) { - if (!request.read_from_buffer(request.buffer(), m_current_scatter_list->dma_region().as_ptr(), m_connected_device->block_size() * request.block_count())) { + if (auto result = request.read_from_buffer(request.buffer(), m_current_scatter_list->dma_region().as_ptr(), m_connected_device->block_size() * request.block_count()); result.is_error()) { return AsyncDeviceRequest::MemoryFault; } } diff --git a/Kernel/Storage/BMIDEChannel.cpp b/Kernel/Storage/BMIDEChannel.cpp index dbebb611cd..732b8baa41 100644 --- a/Kernel/Storage/BMIDEChannel.cpp +++ b/Kernel/Storage/BMIDEChannel.cpp @@ -135,7 +135,7 @@ void BMIDEChannel::complete_current_request(AsyncDeviceRequest::RequestResult re if (result == AsyncDeviceRequest::Success) { if (current_request->request_type() == AsyncBlockDeviceRequest::Read) { - if (!current_request->write_to_buffer(current_request->buffer(), m_dma_buffer_region->vaddr().as_ptr(), 512 * current_request->block_count())) { + if (auto result = current_request->write_to_buffer(current_request->buffer(), m_dma_buffer_region->vaddr().as_ptr(), 512 * current_request->block_count()); result.is_error()) { lock.unlock(); current_request->complete(AsyncDeviceRequest::MemoryFault); return; @@ -164,7 +164,7 @@ void BMIDEChannel::ata_write_sectors(bool slave_request, u16 capabilities) prdt().offset = m_dma_buffer_page->paddr().get(); prdt().size = 512 * m_current_request->block_count(); - if (!m_current_request->read_from_buffer(m_current_request->buffer(), m_dma_buffer_region->vaddr().as_ptr(), 512 * m_current_request->block_count())) { + if (auto result = m_current_request->read_from_buffer(m_current_request->buffer(), m_dma_buffer_region->vaddr().as_ptr(), 512 * m_current_request->block_count()); result.is_error()) { complete_current_request(AsyncDeviceRequest::MemoryFault); return; } diff --git a/Kernel/Storage/RamdiskDevice.cpp b/Kernel/Storage/RamdiskDevice.cpp index 8b962df4d6..76cf6c7fd3 100644 --- a/Kernel/Storage/RamdiskDevice.cpp +++ b/Kernel/Storage/RamdiskDevice.cpp @@ -45,15 +45,13 @@ void RamdiskDevice::start_request(AsyncBlockDeviceRequest& request) if ((offset + length > base + size) || (offset + length < base)) { request.complete(AsyncDeviceRequest::Failure); } else { - bool success; - + auto result = KResult(KSuccess); if (request.request_type() == AsyncBlockDeviceRequest::Read) { - success = request.buffer().write(offset, length); + result = request.buffer().write(offset, length); } else { - success = request.buffer().read(offset, length); + result = request.buffer().read(offset, length); } - - request.complete(success ? AsyncDeviceRequest::Success : AsyncDeviceRequest::MemoryFault); + request.complete(result.is_success() ? AsyncDeviceRequest::Success : AsyncDeviceRequest::MemoryFault); } } diff --git a/Kernel/Storage/StorageDevice.cpp b/Kernel/Storage/StorageDevice.cpp index abdfb33104..f5cf6c83dd 100644 --- a/Kernel/Storage/StorageDevice.cpp +++ b/Kernel/Storage/StorageDevice.cpp @@ -93,8 +93,7 @@ KResultOr<size_t> StorageDevice::read(FileDescription&, u64 offset, UserOrKernel default: break; } - if (!outbuf.write(data.data(), pos, remaining)) - return EFAULT; + TRY(outbuf.write(data.data(), pos, remaining)); } return pos + remaining; @@ -166,8 +165,7 @@ KResultOr<size_t> StorageDevice::write(FileDescription&, u64 offset, const UserO } } - if (!inbuf.read(data.data(), pos, remaining)) - return EFAULT; + TRY(inbuf.read(data.data(), pos, remaining)); { auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index + whole_blocks, 1, data_buffer, block_size()); diff --git a/Kernel/UserOrKernelBuffer.cpp b/Kernel/UserOrKernelBuffer.cpp index 9d23f7d0dc..ae4953c5bb 100644 --- a/Kernel/UserOrKernelBuffer.cpp +++ b/Kernel/UserOrKernelBuffer.cpp @@ -29,40 +29,40 @@ KResultOr<NonnullOwnPtr<KString>> UserOrKernelBuffer::try_copy_into_kstring(size return KString::try_create(ReadonlyBytes { m_buffer, size }); } -bool UserOrKernelBuffer::write(const void* src, size_t offset, size_t len) +KResult UserOrKernelBuffer::write(void const* src, size_t offset, size_t len) { if (!m_buffer) - return false; + return EFAULT; if (Memory::is_user_address(VirtualAddress(m_buffer))) - return copy_to_user(m_buffer + offset, src, len).is_success(); + return copy_to_user(m_buffer + offset, src, len); memcpy(m_buffer + offset, src, len); - return true; + return KSuccess; } -bool UserOrKernelBuffer::read(void* dest, size_t offset, size_t len) const +KResult UserOrKernelBuffer::read(void* dest, size_t offset, size_t len) const { if (!m_buffer) - return false; + return EFAULT; if (Memory::is_user_address(VirtualAddress(m_buffer))) - return copy_from_user(dest, m_buffer + offset, len).is_success(); + return copy_from_user(dest, m_buffer + offset, len); memcpy(dest, m_buffer + offset, len); - return true; + return KSuccess; } -bool UserOrKernelBuffer::memset(int value, size_t offset, size_t len) +KResult UserOrKernelBuffer::memset(int value, size_t offset, size_t len) { if (!m_buffer) - return false; + return EFAULT; if (Memory::is_user_address(VirtualAddress(m_buffer))) - return memset_user(m_buffer + offset, value, len).is_success(); + return memset_user(m_buffer + offset, value, len); ::memset(m_buffer + offset, value, len); - return true; + return KSuccess; } } diff --git a/Kernel/UserOrKernelBuffer.h b/Kernel/UserOrKernelBuffer.h index 555262f10f..1f348f3742 100644 --- a/Kernel/UserOrKernelBuffer.h +++ b/Kernel/UserOrKernelBuffer.h @@ -55,28 +55,29 @@ public: } KResultOr<NonnullOwnPtr<KString>> try_copy_into_kstring(size_t) const; - [[nodiscard]] bool write(const void* src, size_t offset, size_t len); - [[nodiscard]] bool write(const void* src, size_t len) + KResult write(const void* src, size_t offset, size_t len); + KResult write(const void* src, size_t len) { return write(src, 0, len); } - [[nodiscard]] bool write(ReadonlyBytes bytes) + KResult write(ReadonlyBytes bytes) { return write(bytes.data(), bytes.size()); } - [[nodiscard]] bool read(void* dest, size_t offset, size_t len) const; - [[nodiscard]] bool read(void* dest, size_t len) const + KResult read(void* dest, size_t offset, size_t len) const; + KResult read(void* dest, size_t len) const { return read(dest, 0, len); } - [[nodiscard]] bool read(Bytes bytes) const + + KResult read(Bytes bytes) const { return read(bytes.data(), bytes.size()); } - [[nodiscard]] bool memset(int value, size_t offset, size_t len); - [[nodiscard]] bool memset(int value, size_t len) + KResult memset(int value, size_t offset, size_t len); + KResult memset(int value, size_t len) { return memset(value, 0, len); } @@ -104,8 +105,7 @@ public: return copied_or_error.error(); auto copied = copied_or_error.value(); VERIFY(copied <= to_copy); - if (!write(buffer, nwritten, copied)) - return EFAULT; + TRY(write(buffer, nwritten, copied)); nwritten += copied; if (copied < to_copy) break; @@ -134,8 +134,7 @@ public: size_t nread = 0; while (nread < len) { auto to_copy = min(sizeof(buffer), len - nread); - if (!read(buffer, nread, to_copy)) - return EFAULT; + TRY(read(buffer, nread, to_copy)); ReadonlyBytes read_only_bytes { buffer, to_copy }; KResultOr<size_t> copied_or_error = f(read_only_bytes); if (copied_or_error.is_error()) |