From 79fa9765ca89869d19364143989436d117974c21 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 8 Nov 2021 00:51:39 +0100 Subject: [PATCH] Kernel: Replace KResult and KResultOr with Error and ErrorOr We now use AK::Error and AK::ErrorOr in both kernel and userspace! This was a slightly tedious refactoring that took a long time, so it's not unlikely that some bugs crept in. Nevertheless, it does pass basic functionality testing, and it's just real nice to finally see the same pattern in all contexts. :^) --- AK/OwnPtr.h | 17 +- AK/Try.h | 2 +- Kernel/API/KResult.h | 199 --------- Kernel/Arch/aarch64/dummy.cpp | 2 +- Kernel/Bus/PCI/Access.cpp | 2 +- Kernel/Bus/PCI/SysFSPCI.cpp | 6 +- Kernel/Bus/PCI/SysFSPCI.h | 4 +- Kernel/Bus/USB/SysFSUSB.cpp | 18 +- Kernel/Bus/USB/SysFSUSB.h | 8 +- Kernel/Bus/USB/UHCI/UHCIController.cpp | 34 +- Kernel/Bus/USB/UHCI/UHCIController.h | 20 +- Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h | 2 +- Kernel/Bus/USB/UHCI/UHCIRootHub.cpp | 8 +- Kernel/Bus/USB/UHCI/UHCIRootHub.h | 8 +- Kernel/Bus/USB/USBController.h | 12 +- Kernel/Bus/USB/USBDevice.cpp | 6 +- Kernel/Bus/USB/USBDevice.h | 4 +- Kernel/Bus/USB/USBHub.cpp | 46 +-- Kernel/Bus/USB/USBHub.h | 14 +- Kernel/Bus/USB/USBPipe.cpp | 4 +- Kernel/Bus/USB/USBPipe.h | 4 +- Kernel/Bus/USB/USBTransfer.cpp | 2 +- Kernel/Bus/USB/USBTransfer.h | 2 +- Kernel/Bus/VirtIO/ConsolePort.cpp | 11 +- Kernel/Bus/VirtIO/ConsolePort.h | 6 +- Kernel/Coredump.cpp | 42 +- Kernel/Coredump.h | 24 +- Kernel/Devices/AsyncDeviceRequest.h | 8 +- Kernel/Devices/ConsoleDevice.cpp | 4 +- Kernel/Devices/ConsoleDevice.h | 4 +- Kernel/Devices/Device.cpp | 10 +- Kernel/Devices/Device.h | 6 +- Kernel/Devices/DeviceManagement.h | 4 +- Kernel/Devices/FullDevice.cpp | 4 +- Kernel/Devices/FullDevice.h | 4 +- Kernel/Devices/HID/HIDManagement.h | 2 +- Kernel/Devices/HID/KeyboardDevice.cpp | 16 +- Kernel/Devices/HID/KeyboardDevice.h | 6 +- Kernel/Devices/HID/MouseDevice.cpp | 2 +- Kernel/Devices/HID/MouseDevice.h | 4 +- Kernel/Devices/KCOVDevice.cpp | 51 +-- Kernel/Devices/KCOVDevice.h | 10 +- Kernel/Devices/KCOVInstance.cpp | 4 +- Kernel/Devices/KCOVInstance.h | 2 +- Kernel/Devices/MemoryDevice.cpp | 4 +- Kernel/Devices/MemoryDevice.h | 6 +- Kernel/Devices/NullDevice.cpp | 4 +- Kernel/Devices/NullDevice.h | 4 +- Kernel/Devices/RandomDevice.cpp | 4 +- Kernel/Devices/RandomDevice.h | 4 +- Kernel/Devices/SB16.cpp | 8 +- Kernel/Devices/SB16.h | 6 +- Kernel/Devices/SerialDevice.cpp | 4 +- Kernel/Devices/SerialDevice.h | 4 +- Kernel/Devices/ZeroDevice.cpp | 4 +- Kernel/Devices/ZeroDevice.h | 4 +- Kernel/DoubleBuffer.cpp | 8 +- Kernel/DoubleBuffer.h | 14 +- Kernel/FileSystem/AnonymousFile.cpp | 4 +- Kernel/FileSystem/AnonymousFile.h | 10 +- Kernel/FileSystem/BlockBasedFileSystem.cpp | 28 +- Kernel/FileSystem/BlockBasedFileSystem.h | 10 +- Kernel/FileSystem/Custody.cpp | 8 +- Kernel/FileSystem/Custody.h | 6 +- Kernel/FileSystem/DevPtsFS.cpp | 34 +- Kernel/FileSystem/DevPtsFS.h | 26 +- Kernel/FileSystem/DevTmpFS.cpp | 74 ++-- Kernel/FileSystem/DevTmpFS.h | 46 +-- Kernel/FileSystem/Ext2FileSystem.cpp | 162 ++++---- Kernel/FileSystem/Ext2FileSystem.h | 82 ++-- Kernel/FileSystem/FIFO.cpp | 16 +- Kernel/FileSystem/FIFO.h | 14 +- Kernel/FileSystem/File.cpp | 14 +- Kernel/FileSystem/File.h | 28 +- Kernel/FileSystem/FileSystem.h | 6 +- Kernel/FileSystem/ISO9660FileSystem.cpp | 76 ++-- Kernel/FileSystem/ISO9660FileSystem.h | 48 +-- Kernel/FileSystem/Inode.cpp | 40 +- Kernel/FileSystem/Inode.h | 52 +-- Kernel/FileSystem/InodeFile.cpp | 24 +- Kernel/FileSystem/InodeFile.h | 22 +- Kernel/FileSystem/InodeMetadata.h | 6 +- Kernel/FileSystem/InodeWatcher.cpp | 21 +- Kernel/FileSystem/InodeWatcher.h | 16 +- Kernel/FileSystem/OpenFileDescription.cpp | 54 +-- Kernel/FileSystem/OpenFileDescription.h | 42 +- Kernel/FileSystem/Plan9FileSystem.cpp | 64 +-- Kernel/FileSystem/Plan9FileSystem.h | 42 +- Kernel/FileSystem/ProcFS.cpp | 104 ++--- Kernel/FileSystem/ProcFS.h | 82 ++-- Kernel/FileSystem/SysFS.cpp | 46 +-- Kernel/FileSystem/SysFS.h | 44 +- Kernel/FileSystem/SysFSComponent.cpp | 8 +- Kernel/FileSystem/SysFSComponent.h | 20 +- Kernel/FileSystem/TmpFS.cpp | 62 +-- Kernel/FileSystem/TmpFS.h | 38 +- Kernel/FileSystem/VirtualFileSystem.cpp | 120 +++--- Kernel/FileSystem/VirtualFileSystem.h | 56 +-- Kernel/Firmware/ACPI/Parser.cpp | 8 +- Kernel/Firmware/ACPI/Parser.h | 6 +- Kernel/Firmware/BIOS.cpp | 10 +- Kernel/Firmware/BIOS.h | 10 +- Kernel/Firmware/PowerStateSwitch.cpp | 6 +- Kernel/Firmware/PowerStateSwitch.h | 6 +- Kernel/Forward.h | 3 - Kernel/GlobalProcessExposed.cpp | 95 +++-- Kernel/Graphics/Bochs/GraphicsAdapter.cpp | 2 +- Kernel/Graphics/FramebufferDevice.cpp | 48 +-- Kernel/Graphics/FramebufferDevice.h | 24 +- Kernel/Graphics/GenericFramebufferDevice.cpp | 34 +- Kernel/Graphics/GenericFramebufferDevice.h | 34 +- .../Graphics/Intel/NativeGraphicsAdapter.cpp | 2 +- Kernel/Graphics/VGACompatibleAdapter.cpp | 2 +- .../Graphics/VirtIOGPU/FramebufferDevice.cpp | 42 +- Kernel/Graphics/VirtIOGPU/FramebufferDevice.h | 26 +- Kernel/Interrupts/APIC.cpp | 2 +- Kernel/KBuffer.h | 4 +- Kernel/KBufferBuilder.cpp | 26 +- Kernel/KBufferBuilder.h | 14 +- Kernel/KLexicalPath.cpp | 2 +- Kernel/KLexicalPath.h | 2 +- Kernel/KString.cpp | 6 +- Kernel/KString.h | 6 +- Kernel/Library/ThreadSafeRefPtr.h | 8 +- Kernel/Memory/AddressSpace.cpp | 26 +- Kernel/Memory/AddressSpace.h | 16 +- Kernel/Memory/AnonymousVMObject.cpp | 22 +- Kernel/Memory/AnonymousVMObject.h | 14 +- Kernel/Memory/MemoryManager.cpp | 12 +- Kernel/Memory/MemoryManager.h | 12 +- Kernel/Memory/PageDirectory.cpp | 2 +- Kernel/Memory/PageDirectory.h | 2 +- Kernel/Memory/PrivateInodeVMObject.cpp | 4 +- Kernel/Memory/PrivateInodeVMObject.h | 4 +- Kernel/Memory/Region.cpp | 10 +- Kernel/Memory/Region.h | 8 +- Kernel/Memory/RingBuffer.cpp | 4 +- Kernel/Memory/RingBuffer.h | 4 +- Kernel/Memory/ScatterGatherList.cpp | 2 +- Kernel/Memory/SharedInodeVMObject.cpp | 4 +- Kernel/Memory/SharedInodeVMObject.h | 4 +- Kernel/Memory/VMObject.h | 2 +- Kernel/Memory/VirtualRange.cpp | 2 +- Kernel/Memory/VirtualRange.h | 4 +- Kernel/Memory/VirtualRangeAllocator.cpp | 6 +- Kernel/Memory/VirtualRangeAllocator.h | 6 +- Kernel/Net/IPv4Socket.cpp | 76 ++-- Kernel/Net/IPv4Socket.h | 42 +- Kernel/Net/LocalSocket.cpp | 58 +-- Kernel/Net/LocalSocket.h | 32 +- Kernel/Net/NetworkingManagement.cpp | 2 +- Kernel/Net/NetworkingManagement.h | 2 +- Kernel/Net/Socket.cpp | 41 +- Kernel/Net/Socket.h | 48 ++- Kernel/Net/TCPSocket.cpp | 42 +- Kernel/Net/TCPSocket.h | 24 +- Kernel/Net/UDPSocket.cpp | 20 +- Kernel/Net/UDPSocket.h | 14 +- Kernel/PerformanceEventBuffer.cpp | 16 +- Kernel/PerformanceEventBuffer.h | 14 +- Kernel/Process.cpp | 35 +- Kernel/Process.h | 384 +++++++++--------- Kernel/ProcessExposed.cpp | 42 +- Kernel/ProcessExposed.h | 48 +-- Kernel/ProcessGroup.cpp | 6 +- Kernel/ProcessGroup.h | 4 +- Kernel/ProcessProcFSTraits.cpp | 6 +- Kernel/ProcessSpecificExposed.cpp | 55 +-- Kernel/StdLib.cpp | 26 +- Kernel/StdLib.h | 70 ++-- Kernel/Storage/Partition/DiskPartition.cpp | 4 +- Kernel/Storage/Partition/DiskPartition.h | 4 +- Kernel/Storage/RamdiskDevice.cpp | 4 +- Kernel/Storage/StorageDevice.cpp | 6 +- Kernel/Storage/StorageDevice.h | 6 +- Kernel/Storage/StorageManagement.cpp | 2 +- Kernel/Syscall.cpp | 12 +- Kernel/Syscalls/access.cpp | 5 +- Kernel/Syscalls/alarm.cpp | 2 +- Kernel/Syscalls/anon_create.cpp | 2 +- Kernel/Syscalls/beep.cpp | 2 +- Kernel/Syscalls/chdir.cpp | 6 +- Kernel/Syscalls/chmod.cpp | 10 +- Kernel/Syscalls/chown.cpp | 11 +- Kernel/Syscalls/clock.cpp | 13 +- Kernel/Syscalls/debug.cpp | 6 +- Kernel/Syscalls/disown.cpp | 2 +- Kernel/Syscalls/dup2.cpp | 2 +- Kernel/Syscalls/emuctl.cpp | 2 +- Kernel/Syscalls/execve.cpp | 72 ++-- Kernel/Syscalls/fcntl.cpp | 8 +- Kernel/Syscalls/fork.cpp | 2 +- Kernel/Syscalls/fsync.cpp | 5 +- Kernel/Syscalls/ftruncate.cpp | 5 +- Kernel/Syscalls/futex.cpp | 2 +- Kernel/Syscalls/get_dir_entries.cpp | 9 +- Kernel/Syscalls/get_stack_bounds.cpp | 5 +- Kernel/Syscalls/getrandom.cpp | 2 +- Kernel/Syscalls/getuid.cpp | 21 +- Kernel/Syscalls/hostname.cpp | 8 +- Kernel/Syscalls/inode_watcher.cpp | 9 +- Kernel/Syscalls/ioctl.cpp | 7 +- Kernel/Syscalls/keymap.cpp | 6 +- Kernel/Syscalls/kill.cpp | 56 +-- Kernel/Syscalls/link.cpp | 10 +- Kernel/Syscalls/lseek.cpp | 5 +- Kernel/Syscalls/mkdir.cpp | 5 +- Kernel/Syscalls/mknod.cpp | 5 +- Kernel/Syscalls/mmap.cpp | 23 +- Kernel/Syscalls/mount.cpp | 18 +- Kernel/Syscalls/open.cpp | 8 +- Kernel/Syscalls/perf_event.cpp | 11 +- Kernel/Syscalls/pipe.cpp | 4 +- Kernel/Syscalls/pledge.cpp | 2 +- Kernel/Syscalls/prctl.cpp | 2 +- Kernel/Syscalls/process.cpp | 16 +- Kernel/Syscalls/profiling.cpp | 6 +- Kernel/Syscalls/ptrace.cpp | 28 +- Kernel/Syscalls/purge.cpp | 10 +- Kernel/Syscalls/read.cpp | 12 +- Kernel/Syscalls/readlink.cpp | 2 +- Kernel/Syscalls/realpath.cpp | 2 +- Kernel/Syscalls/rename.cpp | 5 +- Kernel/Syscalls/rmdir.cpp | 5 +- Kernel/Syscalls/sched.cpp | 9 +- Kernel/Syscalls/select.cpp | 4 +- Kernel/Syscalls/sendfd.cpp | 7 +- Kernel/Syscalls/setpgid.cpp | 10 +- Kernel/Syscalls/setuid.cpp | 16 +- Kernel/Syscalls/sigaction.cpp | 23 +- Kernel/Syscalls/socket.cpp | 63 +-- Kernel/Syscalls/stat.cpp | 10 +- Kernel/Syscalls/statvfs.cpp | 9 +- Kernel/Syscalls/sync.cpp | 2 +- Kernel/Syscalls/sysconf.cpp | 2 +- Kernel/Syscalls/thread.cpp | 25 +- Kernel/Syscalls/times.cpp | 2 +- Kernel/Syscalls/ttyname.cpp | 10 +- Kernel/Syscalls/umask.cpp | 2 +- Kernel/Syscalls/uname.cpp | 5 +- Kernel/Syscalls/unlink.cpp | 5 +- Kernel/Syscalls/unveil.cpp | 6 +- Kernel/Syscalls/utime.cpp | 5 +- Kernel/Syscalls/waitid.cpp | 11 +- Kernel/Syscalls/write.cpp | 12 +- Kernel/TTY/MasterPTY.cpp | 16 +- Kernel/TTY/MasterPTY.h | 14 +- Kernel/TTY/PTYMultiplexer.cpp | 4 +- Kernel/TTY/PTYMultiplexer.h | 6 +- Kernel/TTY/SlavePTY.cpp | 8 +- Kernel/TTY/SlavePTY.h | 6 +- Kernel/TTY/TTY.cpp | 28 +- Kernel/TTY/TTY.h | 12 +- Kernel/TTY/VirtualConsole.cpp | 2 +- Kernel/TTY/VirtualConsole.h | 2 +- Kernel/Thread.cpp | 12 +- Kernel/Thread.h | 24 +- Kernel/ThreadBlockers.cpp | 4 +- Kernel/ThreadTracer.h | 2 +- Kernel/Time/TimeManagement.h | 2 +- Kernel/UserOrKernelBuffer.cpp | 14 +- Kernel/UserOrKernelBuffer.h | 38 +- 262 files changed, 2415 insertions(+), 2600 deletions(-) delete mode 100644 Kernel/API/KResult.h diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index 582693d9e41..b950076efc0 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -6,13 +6,9 @@ #pragma once +#include #include #include -#ifdef KERNEL -# include -#else -# include -#endif #define OWNPTR_SCRUB_BYTE 0xf0 @@ -209,16 +205,6 @@ inline OwnPtr adopt_own_if_nonnull(T* object) return {}; } -#ifdef KERNEL -template -inline Kernel::KResultOr> adopt_nonnull_own_or_enomem(T* object) -{ - auto result = adopt_own_if_nonnull(object); - if (!result) - return ENOMEM; - return result.release_nonnull(); -} -#else template inline ErrorOr> adopt_nonnull_own_or_enomem(T* object) { @@ -227,7 +213,6 @@ inline ErrorOr> adopt_nonnull_own_or_enomem(T* object) return ENOMEM; return result.release_nonnull(); } -#endif template requires(IsConstructible) inline OwnPtr try_make(Args&&... args) diff --git a/AK/Try.h b/AK/Try.h index 4efb21a5a0f..15d470e86b9 100644 --- a/AK/Try.h +++ b/AK/Try.h @@ -7,7 +7,7 @@ #pragma once // NOTE: This macro works with any result type that has the expected APIs. -// It's designed with AK::Result and Kernel::KResult in mind. +// It's designed with AK::Result and AK::Error in mind. #define TRY(expression) \ ({ \ diff --git a/Kernel/API/KResult.h b/Kernel/API/KResult.h deleted file mode 100644 index aa5334f76f3..00000000000 --- a/Kernel/API/KResult.h +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright (c) 2018-2021, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -namespace Kernel { - -enum KSuccessTag { - KSuccess -}; - -class [[nodiscard]] KResult { -public: - KResult(ErrnoCode error) - : m_error(-error) - { - } - KResult(KSuccessTag) - : m_error(0) - { - } - [[nodiscard]] int error() const { return m_error; } - - [[nodiscard]] bool is_success() const { return m_error == 0; } - [[nodiscard]] bool is_error() const { return !is_success(); } - - bool operator==(ErrnoCode error) const { return is_error() && m_error == -error; } - bool operator!=(ErrnoCode error) const { return !is_error() || m_error != -error; } - - bool operator!=(KSuccessTag) const { return is_error(); } - bool operator==(KSuccessTag) const { return !is_error(); } - - // NOTE: These are here to make KResult usable with TRY() - KResult release_error() { return *this; } - void release_value() { } - -private: - template - friend class KResultOr; - KResult() = default; - - int m_error { 0 }; -}; - -template -class [[nodiscard]] KResultOr { -public: - KResultOr(KResult error) - : m_error(error) - , m_is_error(true) - { - } - - KResultOr(ErrnoCode error) - : m_error(error) - , m_is_error(true) - { - } - - ALWAYS_INLINE KResultOr(T&& value) - : m_have_storage(true) - { - new (&m_storage) T(move(value)); - } - - ALWAYS_INLINE KResultOr(const T& value) - : m_have_storage(true) - { - new (&m_storage) T(value); - } - - template - ALWAYS_INLINE KResultOr(U&& value) requires(!IsSame, KResultOr>) - : m_have_storage(true) - { - new (&m_storage) T(forward(value)); - } - - KResultOr(KResultOr&& other) - { - m_is_error = other.m_is_error; - if (m_is_error) { - m_error = other.m_error; - } else { - if (other.m_have_storage) { - new (&m_storage) T(move(other.value())); - m_have_storage = true; - other.value().~T(); - other.m_have_storage = false; - } - } - other.m_is_error = true; - other.m_error = KSuccess; - } - - KResultOr& operator=(KResultOr&& other) - { - if (&other == this) - return *this; - if (!m_is_error && m_have_storage) { - value().~T(); - m_have_storage = false; - } - m_is_error = other.m_is_error; - if (m_is_error) { - m_error = other.m_error; - } else { - if (other.m_have_storage) { - new (&m_storage) T(move(other.value())); - m_have_storage = true; - other.value().~T(); - other.m_have_storage = false; - } - } - other.m_is_error = true; - other.m_error = KSuccess; - return *this; - } - - ~KResultOr() - { - if (!m_is_error && m_have_storage) - value().~T(); - } - - [[nodiscard]] bool is_error() const { return m_is_error; } - - [[nodiscard]] ALWAYS_INLINE KResult error() const - { - VERIFY(m_is_error); - return m_error; - } - - KResult result() const { return m_is_error ? m_error : KSuccess; } - - [[nodiscard]] ALWAYS_INLINE T& value() & - { - VERIFY(!m_is_error); - return *reinterpret_cast(&m_storage); - } - - [[nodiscard]] ALWAYS_INLINE T const& value() const& - { - VERIFY(!m_is_error); - return *reinterpret_cast(&m_storage); - } - - T value() && = delete; - - [[nodiscard]] ALWAYS_INLINE T release_value() - { - VERIFY(!m_is_error); - VERIFY(m_have_storage); - T released_value(move(*reinterpret_cast(&m_storage))); - value().~T(); - m_have_storage = false; - return released_value; - } - - KResult release_error() - { - VERIFY(m_is_error); - return m_error; - } - -private: - union { - alignas(T) char m_storage[sizeof(T)]; - KResult m_error; - }; - bool m_is_error { false }; - bool m_have_storage { false }; -}; - -} - -using Kernel::KResult; -using Kernel::KResultOr; -using Kernel::KSuccess; - -template<> -struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, Kernel::KResult value) - { - if (value.is_error()) - return AK::Formatter::format(builder, "KResult({})", value.error()); - return AK::Formatter::format(builder, "KResult(success)"); - } -}; diff --git a/Kernel/Arch/aarch64/dummy.cpp b/Kernel/Arch/aarch64/dummy.cpp index d0d6d4ade5e..3ac7310e319 100644 --- a/Kernel/Arch/aarch64/dummy.cpp +++ b/Kernel/Arch/aarch64/dummy.cpp @@ -44,7 +44,7 @@ namespace Kernel { void dump_backtrace(PrintToScreen) { } // KString.cpp -KResultOr> KString::try_create_uninitialized(size_t, char*&) { return ENOMEM; } +ErrorOr> KString::try_create_uninitialized(size_t, char*&) { return ENOMEM; } void KString::operator delete(void*) { } // SafeMem.h diff --git a/Kernel/Bus/PCI/Access.cpp b/Kernel/Bus/PCI/Access.cpp index ad1f8c9106d..664d7bc1502 100644 --- a/Kernel/Bus/PCI/Access.cpp +++ b/Kernel/Bus/PCI/Access.cpp @@ -5,8 +5,8 @@ */ #include +#include #include -#include #include #include #include diff --git a/Kernel/Bus/PCI/SysFSPCI.cpp b/Kernel/Bus/PCI/SysFSPCI.cpp index a8fc6e9ddc8..ab79bdd4f6f 100644 --- a/Kernel/Bus/PCI/SysFSPCI.cpp +++ b/Kernel/Bus/PCI/SysFSPCI.cpp @@ -59,19 +59,19 @@ PCIDeviceAttributeSysFSComponent::PCIDeviceAttributeSysFSComponent(String name, { } -KResultOr PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const +ErrorOr PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const { auto blob = TRY(try_to_generate_buffer()); if ((size_t)offset >= blob->size()) - return KSuccess; + return 0; ssize_t nread = min(static_cast(blob->size() - offset), static_cast(count)); TRY(buffer.write(blob->data() + offset, nread)); return nread; } -KResultOr> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const +ErrorOr> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const { String value; switch (m_field_bytes_width) { diff --git a/Kernel/Bus/PCI/SysFSPCI.h b/Kernel/Bus/PCI/SysFSPCI.h index 4286150eafb..60b2bfddabc 100644 --- a/Kernel/Bus/PCI/SysFSPCI.h +++ b/Kernel/Bus/PCI/SysFSPCI.h @@ -37,11 +37,11 @@ class PCIDeviceAttributeSysFSComponent : public SysFSComponent { public: static NonnullRefPtr create(String name, const PCIDeviceSysFSDirectory& device, PCI::RegisterOffset offset, size_t field_bytes_width); - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override; virtual ~PCIDeviceAttributeSysFSComponent() {}; protected: - KResultOr> try_to_generate_buffer() const; + ErrorOr> try_to_generate_buffer() const; PCIDeviceAttributeSysFSComponent(String name, const PCIDeviceSysFSDirectory& device, PCI::RegisterOffset offset, size_t field_bytes_width); NonnullRefPtr m_device; PCI::RegisterOffset m_offset; diff --git a/Kernel/Bus/USB/SysFSUSB.cpp b/Kernel/Bus/USB/SysFSUSB.cpp index 5bc16aa245d..d242c8cb851 100644 --- a/Kernel/Bus/USB/SysFSUSB.cpp +++ b/Kernel/Bus/USB/SysFSUSB.cpp @@ -23,7 +23,7 @@ SysFSUSBDeviceInformation::~SysFSUSBDeviceInformation() { } -KResult SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder) +ErrorOr SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder) { VERIFY(m_lock.is_locked()); JsonArraySerializer array { builder }; @@ -44,10 +44,10 @@ KResult SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder) obj.add("num_configurations", m_device->device_descriptor().num_configurations); obj.finish(); array.finish(); - return KSuccess; + return {}; } -KResult SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description) const +ErrorOr SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description) const { MutexLocker lock(m_lock); auto& cached_data = description.data(); @@ -60,10 +60,10 @@ KResult SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description typed_cached_data.buffer = builder.build(); if (!typed_cached_data.buffer) return ENOMEM; - return KSuccess; + return {}; } -KResultOr SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const +ErrorOr SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const { dbgln_if(PROCFS_DEBUG, "SysFSUSBDeviceInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count); @@ -71,13 +71,13 @@ KResultOr SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t cou VERIFY(buffer.user_or_kernel_ptr()); if (!description) - return KResult(EIO); + return Error::from_errno(EIO); MutexLocker locker(m_lock); if (!description->data()) { dbgln("SysFSUSBDeviceInformation: Do not have cached data!"); - return KResult(EIO); + return Error::from_errno(EIO); } auto& typed_cached_data = static_cast(*description->data()); @@ -91,7 +91,7 @@ KResultOr SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t cou return nread; } -KResult SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function callback) const +ErrorOr SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function callback) const { SpinlockLocker lock(m_lock); // Note: if the parent directory is null, it means something bad happened as this should not happen for the USB directory. @@ -103,7 +103,7 @@ KResult SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function SysFSUSBBusDirectory::lookup(StringView name) diff --git a/Kernel/Bus/USB/SysFSUSB.h b/Kernel/Bus/USB/SysFSUSB.h index 2380235647c..9891c87b13a 100644 --- a/Kernel/Bus/USB/SysFSUSB.h +++ b/Kernel/Bus/USB/SysFSUSB.h @@ -26,15 +26,15 @@ public: protected: explicit SysFSUSBDeviceInformation(USB::Device& device); - virtual KResultOr read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; IntrusiveListNode> m_list_node; NonnullRefPtr m_device; private: - KResult try_generate(KBufferBuilder&); - virtual KResult refresh_data(OpenFileDescription& description) const override; + ErrorOr try_generate(KBufferBuilder&); + virtual ErrorOr refresh_data(OpenFileDescription& description) const override; mutable Mutex m_lock { "SysFSUSBDeviceInformation" }; }; @@ -46,7 +46,7 @@ public: void plug(USB::Device&); void unplug(USB::Device&); - virtual KResult traverse_as_directory(unsigned, Function) const override; + virtual ErrorOr traverse_as_directory(unsigned, Function) const override; virtual RefPtr lookup(StringView name) override; private: diff --git a/Kernel/Bus/USB/UHCI/UHCIController.cpp b/Kernel/Bus/USB/UHCI/UHCIController.cpp index b64f4791d0d..88f945408bd 100644 --- a/Kernel/Bus/USB/UHCI/UHCIController.cpp +++ b/Kernel/Bus/USB/UHCI/UHCIController.cpp @@ -62,7 +62,7 @@ static constexpr u16 UCHI_PORTSC_NON_WRITE_CLEAR_BIT_MASK = 0x1FF5; // This is u static constexpr u8 UHCI_NUMBER_OF_ISOCHRONOUS_TDS = 128; static constexpr u16 UHCI_NUMBER_OF_FRAMES = 1024; -KResultOr> UHCIController::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier) +ErrorOr> UHCIController::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier) { // NOTE: This assumes that address is pointing to a valid UHCI controller. auto controller = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) UHCIController(pci_device_identifier))); @@ -70,7 +70,7 @@ KResultOr> UHCIController::try_to_initialize(PCI:: return controller; } -KResult UHCIController::initialize() +ErrorOr UHCIController::initialize() { dmesgln("UHCI: Controller found {} @ {}", PCI::get_hardware_id(pci_address()), pci_address()); dmesgln("UHCI: I/O base {}", m_io_base); @@ -93,7 +93,7 @@ UNMAP_AFTER_INIT UHCIController::~UHCIController() { } -KResult UHCIController::reset() +ErrorOr UHCIController::reset() { TRY(stop()); @@ -126,10 +126,10 @@ KResult UHCIController::reset() write_usbintr(0); dbgln("UHCI: Reset completed"); - return KSuccess; + return {}; } -UNMAP_AFTER_INIT KResult UHCIController::create_structures() +UNMAP_AFTER_INIT ErrorOr UHCIController::create_structures() { m_queue_head_pool = TRY(UHCIDescriptorPool::try_create("Queue Head Pool"sv)); @@ -173,7 +173,7 @@ UNMAP_AFTER_INIT KResult UHCIController::create_structures() m_transfer_descriptor_pool->print_pool_information(); } - return KSuccess; + return {}; } UNMAP_AFTER_INIT void UHCIController::setup_schedule() @@ -245,7 +245,7 @@ TransferDescriptor* UHCIController::allocate_transfer_descriptor() return m_transfer_descriptor_pool->try_take_free_descriptor(); } -KResult UHCIController::stop() +ErrorOr UHCIController::stop() { write_usbcmd(read_usbcmd() & ~UHCI_USBCMD_RUN); // FIXME: Timeout @@ -253,10 +253,10 @@ KResult UHCIController::stop() if (read_usbsts() & UHCI_USBSTS_HOST_CONTROLLER_HALTED) break; } - return KSuccess; + return {}; } -KResult UHCIController::start() +ErrorOr UHCIController::start() { write_usbcmd(read_usbcmd() | UHCI_USBCMD_RUN); // FIXME: Timeout @@ -268,7 +268,7 @@ KResult UHCIController::start() m_root_hub = TRY(UHCIRootHub::try_create(*this)); TRY(m_root_hub->setup({})); - return KSuccess; + return {}; } TransferDescriptor* UHCIController::create_transfer_descriptor(Pipe& pipe, PacketID direction, size_t data_len) @@ -303,7 +303,7 @@ TransferDescriptor* UHCIController::create_transfer_descriptor(Pipe& pipe, Packe return td; } -KResult UHCIController::create_chain(Pipe& pipe, PacketID direction, Ptr32& buffer_address, size_t max_size, size_t transfer_size, TransferDescriptor** td_chain, TransferDescriptor** last_td) +ErrorOr UHCIController::create_chain(Pipe& pipe, PacketID direction, Ptr32& buffer_address, size_t max_size, size_t transfer_size, TransferDescriptor** td_chain, TransferDescriptor** last_td) { // We need to create `n` transfer descriptors based on the max // size of each transfer (which we've learned from the device already by reading @@ -345,7 +345,7 @@ KResult UHCIController::create_chain(Pipe& pipe, PacketID direction, Ptr32& *last_td = current_td; *td_chain = first_td; - return KSuccess; + return {}; } void UHCIController::free_descriptor_chain(TransferDescriptor* first_descriptor) @@ -361,7 +361,7 @@ void UHCIController::free_descriptor_chain(TransferDescriptor* first_descriptor) } } -KResultOr UHCIController::submit_control_transfer(Transfer& transfer) +ErrorOr UHCIController::submit_control_transfer(Transfer& transfer) { Pipe& pipe = transfer.pipe(); // Short circuit the pipe related to this transfer bool direction_in = (transfer.request().request_type & USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST) == USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST; @@ -589,7 +589,7 @@ void UHCIController::reset_port(u8 port) m_port_reset_change_statuses |= (1 << port); } -KResult UHCIController::set_port_feature(Badge, u8 port, HubFeatureSelector feature_selector) +ErrorOr UHCIController::set_port_feature(Badge, u8 port, HubFeatureSelector feature_selector) { // The check is done by UHCIRootHub. VERIFY(port < NUMBER_OF_ROOT_PORTS); @@ -621,10 +621,10 @@ KResult UHCIController::set_port_feature(Badge, u8 port, HubFeature return EINVAL; } - return KSuccess; + return {}; } -KResult UHCIController::clear_port_feature(Badge, u8 port, HubFeatureSelector feature_selector) +ErrorOr UHCIController::clear_port_feature(Badge, u8 port, HubFeatureSelector feature_selector) { // The check is done by UHCIRootHub. VERIFY(port < NUMBER_OF_ROOT_PORTS); @@ -670,7 +670,7 @@ KResult UHCIController::clear_port_feature(Badge, u8 port, HubFeatu else write_portsc2(port_data); - return KSuccess; + return {}; } } diff --git a/Kernel/Bus/USB/UHCI/UHCIController.h b/Kernel/Bus/USB/UHCI/UHCIController.h index d9c426ff3c5..20790479edc 100644 --- a/Kernel/Bus/USB/UHCI/UHCIController.h +++ b/Kernel/Bus/USB/UHCI/UHCIController.h @@ -33,24 +33,24 @@ class UHCIController final public: static constexpr u8 NUMBER_OF_ROOT_PORTS = 2; - static KResultOr> try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier); + static ErrorOr> try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier); virtual ~UHCIController() override; virtual StringView purpose() const override { return "UHCI"sv; } - virtual KResult initialize() override; - virtual KResult reset() override; - virtual KResult stop() override; - virtual KResult start() override; + virtual ErrorOr initialize() override; + virtual ErrorOr reset() override; + virtual ErrorOr stop() override; + virtual ErrorOr start() override; void spawn_port_proc(); void do_debug_transfer(); - virtual KResultOr submit_control_transfer(Transfer& transfer) override; + virtual ErrorOr submit_control_transfer(Transfer& transfer) override; void get_port_status(Badge, u8, HubStatus&); - KResult set_port_feature(Badge, u8, HubFeatureSelector); - KResult clear_port_feature(Badge, u8, HubFeatureSelector); + ErrorOr set_port_feature(Badge, u8, HubFeatureSelector); + ErrorOr clear_port_feature(Badge, u8, HubFeatureSelector); private: explicit UHCIController(PCI::DeviceIdentifier const& pci_device_identifier); @@ -75,12 +75,12 @@ private: virtual bool handle_irq(const RegisterState&) override; - KResult create_structures(); + ErrorOr create_structures(); void setup_schedule(); size_t poll_transfer_queue(QueueHead& transfer_queue); TransferDescriptor* create_transfer_descriptor(Pipe& pipe, PacketID direction, size_t data_len); - KResult create_chain(Pipe& pipe, PacketID direction, Ptr32& buffer_address, size_t max_size, size_t transfer_size, TransferDescriptor** td_chain, TransferDescriptor** last_td); + ErrorOr create_chain(Pipe& pipe, PacketID direction, Ptr32& buffer_address, size_t max_size, size_t transfer_size, TransferDescriptor** td_chain, TransferDescriptor** last_td); void free_descriptor_chain(TransferDescriptor* first_descriptor); QueueHead* allocate_queue_head(); diff --git a/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h b/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h index b099e3d1baa..a4658a88c56 100644 --- a/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h +++ b/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h @@ -28,7 +28,7 @@ class UHCIDescriptorPool { static_assert(sizeof(T) <= PAGE_SIZE); public: - static KResultOr>> try_create(StringView name) + static ErrorOr>> try_create(StringView name) { auto pool_memory_block = TRY(MM.allocate_kernel_region(PAGE_SIZE, "UHCI Descriptor Pool", Memory::Region::Access::ReadWrite)); return adopt_nonnull_own_or_enomem(new (nothrow) UHCIDescriptorPool(move(pool_memory_block), name)); diff --git a/Kernel/Bus/USB/UHCI/UHCIRootHub.cpp b/Kernel/Bus/USB/UHCI/UHCIRootHub.cpp index 2f65a492759..936d3edbb03 100644 --- a/Kernel/Bus/USB/UHCI/UHCIRootHub.cpp +++ b/Kernel/Bus/USB/UHCI/UHCIRootHub.cpp @@ -83,7 +83,7 @@ static USBHubDescriptor uhci_root_hub_hub_descriptor = { 0x0, // Self-powered }; -KResultOr> UHCIRootHub::try_create(NonnullRefPtr uhci_controller) +ErrorOr> UHCIRootHub::try_create(NonnullRefPtr uhci_controller) { return adopt_nonnull_own_or_enomem(new (nothrow) UHCIRootHub(uhci_controller)); } @@ -93,7 +93,7 @@ UHCIRootHub::UHCIRootHub(NonnullRefPtr uhci_controller) { } -KResult UHCIRootHub::setup(Badge) +ErrorOr UHCIRootHub::setup(Badge) { m_hub = TRY(Hub::try_create_root_hub(m_uhci_controller, Device::DeviceSpeed::FullSpeed)); @@ -104,10 +104,10 @@ KResult UHCIRootHub::setup(Badge) // NOTE: The root hub is no longer on the default address. TRY(m_hub->enumerate_and_power_on_hub()); - return KSuccess; + return {}; } -KResultOr UHCIRootHub::handle_control_transfer(Transfer& transfer) +ErrorOr UHCIRootHub::handle_control_transfer(Transfer& transfer) { auto& request = transfer.request(); auto* request_data = transfer.buffer().as_ptr() + sizeof(USBRequestData); diff --git a/Kernel/Bus/USB/UHCI/UHCIRootHub.h b/Kernel/Bus/USB/UHCI/UHCIRootHub.h index 15972872351..bcfd8ba8504 100644 --- a/Kernel/Bus/USB/UHCI/UHCIRootHub.h +++ b/Kernel/Bus/USB/UHCI/UHCIRootHub.h @@ -6,9 +6,9 @@ #pragma once +#include #include #include -#include #include #include @@ -18,16 +18,16 @@ class UHCIController; class UHCIRootHub { public: - static KResultOr> try_create(NonnullRefPtr); + static ErrorOr> try_create(NonnullRefPtr); UHCIRootHub(NonnullRefPtr); ~UHCIRootHub() = default; - KResult setup(Badge); + ErrorOr setup(Badge); u8 device_address() const { return m_hub->address(); } - KResultOr handle_control_transfer(Transfer& transfer); + ErrorOr handle_control_transfer(Transfer& transfer); void check_for_port_updates() { m_hub->check_for_port_updates(); } diff --git a/Kernel/Bus/USB/USBController.h b/Kernel/Bus/USB/USBController.h index 3d93bd9e128..f5f8d561f8b 100644 --- a/Kernel/Bus/USB/USBController.h +++ b/Kernel/Bus/USB/USBController.h @@ -6,8 +6,8 @@ #pragma once +#include #include -#include #include #include @@ -17,13 +17,13 @@ class USBController : public RefCounted { public: virtual ~USBController() = default; - virtual KResult initialize() = 0; + virtual ErrorOr initialize() = 0; - virtual KResult reset() = 0; - virtual KResult stop() = 0; - virtual KResult start() = 0; + virtual ErrorOr reset() = 0; + virtual ErrorOr stop() = 0; + virtual ErrorOr start() = 0; - virtual KResultOr submit_control_transfer(Transfer&) = 0; + virtual ErrorOr submit_control_transfer(Transfer&) = 0; u8 allocate_address(); diff --git a/Kernel/Bus/USB/USBDevice.cpp b/Kernel/Bus/USB/USBDevice.cpp index 44228eb84fa..d2de629f45d 100644 --- a/Kernel/Bus/USB/USBDevice.cpp +++ b/Kernel/Bus/USB/USBDevice.cpp @@ -16,7 +16,7 @@ namespace Kernel::USB { -KResultOr> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed) +ErrorOr> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed) { auto pipe = TRY(Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0)); auto device = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Device(controller, port, speed, move(pipe)))); @@ -56,7 +56,7 @@ Device::~Device() { } -KResult Device::enumerate_device() +ErrorOr Device::enumerate_device() { USBDeviceDescriptor dev_descriptor {}; @@ -118,7 +118,7 @@ KResult Device::enumerate_device() dbgln_if(USB_DEBUG, "USB Device: Set address to {}", m_address); memcpy(&m_device_descriptor, &dev_descriptor, sizeof(USBDeviceDescriptor)); - return KSuccess; + return {}; } } diff --git a/Kernel/Bus/USB/USBDevice.h b/Kernel/Bus/USB/USBDevice.h index caefbe66c5d..0a255fc0ef2 100644 --- a/Kernel/Bus/USB/USBDevice.h +++ b/Kernel/Bus/USB/USBDevice.h @@ -27,13 +27,13 @@ public: }; public: - static KResultOr> try_create(USBController const&, u8, DeviceSpeed); + static ErrorOr> try_create(USBController const&, u8, DeviceSpeed); Device(USBController const&, u8, DeviceSpeed, NonnullOwnPtr default_pipe); Device(Device const& device, NonnullOwnPtr default_pipe); virtual ~Device(); - KResult enumerate_device(); + ErrorOr enumerate_device(); u8 port() const { return m_device_port; } DeviceSpeed speed() const { return m_device_speed; } diff --git a/Kernel/Bus/USB/USBHub.cpp b/Kernel/Bus/USB/USBHub.cpp index a486b710b32..f5684858fcb 100644 --- a/Kernel/Bus/USB/USBHub.cpp +++ b/Kernel/Bus/USB/USBHub.cpp @@ -14,7 +14,7 @@ namespace Kernel::USB { -KResultOr> Hub::try_create_root_hub(NonnullRefPtr controller, DeviceSpeed device_speed) +ErrorOr> Hub::try_create_root_hub(NonnullRefPtr controller, DeviceSpeed device_speed) { // NOTE: Enumeration does not happen here, as the controller must know what the device address is at all times during enumeration to intercept requests. auto pipe = TRY(Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0)); @@ -22,7 +22,7 @@ KResultOr> Hub::try_create_root_hub(NonnullRefPtr> Hub::try_create_from_device(Device const& device) +ErrorOr> Hub::try_create_from_device(Device const& device) { auto pipe = TRY(Pipe::try_create_pipe(device.controller(), Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, device.device_descriptor().max_packet_size, device.address())); auto hub = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Hub(device, move(pipe)))); @@ -40,7 +40,7 @@ Hub::Hub(Device const& device, NonnullOwnPtr default_pipe) { } -KResult Hub::enumerate_and_power_on_hub() +ErrorOr Hub::enumerate_and_power_on_hub() { // USBDevice::enumerate_device must be called before this. VERIFY(m_address > 0); @@ -85,11 +85,11 @@ KResult Hub::enumerate_and_power_on_hub() memcpy(&m_hub_descriptor, &descriptor, sizeof(USBHubDescriptor)); - return KSuccess; + return {}; } // USB 2.0 Specification Section 11.24.2.7 -KResult Hub::get_port_status(u8 port, HubStatus& hub_status) +ErrorOr Hub::get_port_status(u8 port, HubStatus& hub_status) { // Ports are 1-based. if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports) @@ -103,29 +103,29 @@ KResult Hub::get_port_status(u8 port, HubStatus& hub_status) return EIO; } - return KSuccess; + return {}; } // USB 2.0 Specification Section 11.24.2.2 -KResult Hub::clear_port_feature(u8 port, HubFeatureSelector feature_selector) +ErrorOr Hub::clear_port_feature(u8 port, HubFeatureSelector feature_selector) { // Ports are 1-based. if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports) return EINVAL; TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::CLEAR_FEATURE, feature_selector, port, 0, nullptr)); - return KSuccess; + return {}; } // USB 2.0 Specification Section 11.24.2.13 -KResult Hub::set_port_feature(u8 port, HubFeatureSelector feature_selector) +ErrorOr Hub::set_port_feature(u8 port, HubFeatureSelector feature_selector) { // Ports are 1-based. if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports) return EINVAL; TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::SET_FEATURE, feature_selector, port, 0, nullptr)); - return KSuccess; + return {}; } void Hub::remove_children_from_sysfs() @@ -140,16 +140,14 @@ void Hub::check_for_port_updates() dbgln_if(USB_DEBUG, "USB Hub: Checking for port updates on port {}...", port_number); HubStatus port_status {}; - auto result = get_port_status(port_number, port_status); - if (result.is_error()) { + if (auto result = get_port_status(port_number, port_status); result.is_error()) { dbgln("USB Hub: Error occurred when getting status for port {}: {}. Checking next port instead.", port_number, result.error()); continue; } if (port_status.change & PORT_STATUS_CONNECT_STATUS_CHANGED) { // Clear the connection status change notification. - result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_CONNECTION); - if (result.is_error()) { + if (auto result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_CONNECTION); result.is_error()) { dbgln("USB Hub: Error occurred when clearing port connection change for port {}: {}.", port_number, result.error()); return; } @@ -174,8 +172,7 @@ void Hub::check_for_port_updates() IO::delay(debounce_disconnect_check_interval); debounce_timer += debounce_disconnect_check_interval; - result = get_port_status(port_number, port_status); - if (result.is_error()) { + if (auto result = get_port_status(port_number, port_status); result.is_error()) { dbgln("USB Hub: Error occurred when getting status while debouncing port {}: {}.", port_number, result.error()); return; } @@ -185,8 +182,8 @@ void Hub::check_for_port_updates() dbgln_if(USB_DEBUG, "USB Hub: Connection status changed while debouncing, resetting debounce timer."); debounce_timer = 0; - result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_CONNECTION); - if (result.is_error()) { + + if (auto result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_CONNECTION); result.is_error()) { dbgln("USB Hub: Error occurred when clearing port connection change while debouncing port {}: {}.", port_number, result.error()); return; } @@ -194,8 +191,7 @@ void Hub::check_for_port_updates() // Reset the port dbgln_if(USB_DEBUG, "USB Hub: Debounce finished. Driving reset..."); - result = set_port_feature(port_number, HubFeatureSelector::PORT_RESET); - if (result.is_error()) { + if (auto result = set_port_feature(port_number, HubFeatureSelector::PORT_RESET); result.is_error()) { dbgln("USB Hub: Error occurred when resetting port {}: {}.", port_number, result.error()); return; } @@ -207,8 +203,7 @@ void Hub::check_for_port_updates() constexpr u16 reset_delay = 10 * 1000; IO::delay(reset_delay); - result = get_port_status(port_number, port_status); - if (result.is_error()) { + if (auto result = get_port_status(port_number, port_status); result.is_error()) { dbgln("USB Hub: Error occurred when getting status while resetting port {}: {}.", port_number, result.error()); return; } @@ -218,8 +213,8 @@ void Hub::check_for_port_updates() } // Stop asserting reset. This also causes the port to become enabled. - result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_RESET); - if (result.is_error()) { + + if (auto result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_RESET); result.is_error()) { dbgln("USB Hub: Error occurred when resetting port {}: {}.", port_number, result.error()); return; } @@ -233,8 +228,7 @@ void Hub::check_for_port_updates() // The port is ready to go. This is where we start communicating with the device to set up a driver for it. - result = get_port_status(port_number, port_status); - if (result.is_error()) { + if (auto result = get_port_status(port_number, port_status); result.is_error()) { dbgln("USB Hub: Error occurred when getting status for port {} after reset: {}.", port_number, result.error()); return; } diff --git a/Kernel/Bus/USB/USBHub.h b/Kernel/Bus/USB/USBHub.h index 35529df67ed..0d4da8ee6cf 100644 --- a/Kernel/Bus/USB/USBHub.h +++ b/Kernel/Bus/USB/USBHub.h @@ -80,18 +80,18 @@ static constexpr u16 PORT_STATUS_RESET_CHANGED = (1 << 4); class Hub : public Device { public: - static KResultOr> try_create_root_hub(NonnullRefPtr, DeviceSpeed); - static KResultOr> try_create_from_device(Device const&); + static ErrorOr> try_create_root_hub(NonnullRefPtr, DeviceSpeed); + static ErrorOr> try_create_from_device(Device const&); virtual ~Hub() override = default; - KResult enumerate_and_power_on_hub(); + ErrorOr enumerate_and_power_on_hub(); - KResult get_port_status(u8, HubStatus&); - KResult clear_port_feature(u8, HubFeatureSelector); - KResult set_port_feature(u8, HubFeatureSelector); + ErrorOr get_port_status(u8, HubStatus&); + ErrorOr clear_port_feature(u8, HubFeatureSelector); + ErrorOr set_port_feature(u8, HubFeatureSelector); - KResult reset_port(u8); + ErrorOr reset_port(u8); void check_for_port_updates(); diff --git a/Kernel/Bus/USB/USBPipe.cpp b/Kernel/Bus/USB/USBPipe.cpp index dd6d358a1cf..940b0f5178b 100644 --- a/Kernel/Bus/USB/USBPipe.cpp +++ b/Kernel/Bus/USB/USBPipe.cpp @@ -11,7 +11,7 @@ namespace Kernel::USB { -KResultOr> Pipe::try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval) +ErrorOr> Pipe::try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval) { return adopt_nonnull_own_or_enomem(new (nothrow) Pipe(controller, type, direction, endpoint_address, max_packet_size, poll_interval, device_address)); } @@ -47,7 +47,7 @@ Pipe::Pipe(USBController const& controller, Type type, Direction direction, u8 e { } -KResultOr Pipe::control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data) +ErrorOr Pipe::control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data) { USBRequestData usb_request; diff --git a/Kernel/Bus/USB/USBPipe.h b/Kernel/Bus/USB/USBPipe.h index 5a41bdfa9c9..38ff76283ca 100644 --- a/Kernel/Bus/USB/USBPipe.h +++ b/Kernel/Bus/USB/USBPipe.h @@ -41,7 +41,7 @@ public: }; public: - static KResultOr> try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval = 0); + static ErrorOr> try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval = 0); Type type() const { return m_type; } Direction direction() const { return m_direction; } @@ -57,7 +57,7 @@ public: void set_toggle(bool toggle) { m_data_toggle = toggle; } void set_device_address(i8 addr) { m_device_address = addr; } - KResultOr control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data); + ErrorOr control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data); Pipe(USBController const& controller, Type type, Direction direction, u16 max_packet_size); Pipe(USBController const& controller, Type type, Direction direction, USBEndpointDescriptor& endpoint); diff --git a/Kernel/Bus/USB/USBTransfer.cpp b/Kernel/Bus/USB/USBTransfer.cpp index ca2263b08ee..e2eb744a2b8 100644 --- a/Kernel/Bus/USB/USBTransfer.cpp +++ b/Kernel/Bus/USB/USBTransfer.cpp @@ -9,7 +9,7 @@ namespace Kernel::USB { -KResultOr> Transfer::try_create(Pipe& pipe, u16 length) +ErrorOr> Transfer::try_create(Pipe& pipe, u16 length) { // Initialize data buffer for transfer // This will definitely need to be refactored in the future, I doubt this will scale well... diff --git a/Kernel/Bus/USB/USBTransfer.h b/Kernel/Bus/USB/USBTransfer.h index 35c342feb0f..1c579a79cb6 100644 --- a/Kernel/Bus/USB/USBTransfer.h +++ b/Kernel/Bus/USB/USBTransfer.h @@ -19,7 +19,7 @@ namespace Kernel::USB { class Transfer : public RefCounted { public: - static KResultOr> try_create(Pipe&, u16 length); + static ErrorOr> try_create(Pipe&, u16 length); public: Transfer() = delete; diff --git a/Kernel/Bus/VirtIO/ConsolePort.cpp b/Kernel/Bus/VirtIO/ConsolePort.cpp index 71210060621..7e3bd0792a2 100644 --- a/Kernel/Bus/VirtIO/ConsolePort.cpp +++ b/Kernel/Bus/VirtIO/ConsolePort.cpp @@ -90,7 +90,7 @@ bool ConsolePort::can_read(const OpenFileDescription&, size_t) const return m_receive_buffer->used_bytes() > 0; } -KResultOr ConsolePort::read(OpenFileDescription& desc, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr ConsolePort::read(OpenFileDescription& desc, u64, UserOrKernelBuffer& buffer, size_t size) { if (!size) return 0; @@ -100,10 +100,7 @@ KResultOr ConsolePort::read(OpenFileDescription& desc, u64, UserOrKernel if (!can_read(desc, size)) return EAGAIN; - auto bytes_copied_or_error = m_receive_buffer->copy_data_out(size, buffer); - if (bytes_copied_or_error.is_error()) - return bytes_copied_or_error.error(); - auto bytes_copied = bytes_copied_or_error.release_value(); + auto bytes_copied = TRY(m_receive_buffer->copy_data_out(size, buffer)); m_receive_buffer->reclaim_space(m_receive_buffer->start_of_used(), bytes_copied); if (m_receive_buffer_exhausted && m_receive_buffer->used_bytes() == 0) { @@ -123,7 +120,7 @@ bool ConsolePort::can_write(const OpenFileDescription&, size_t) const return m_console.get_queue(m_transmit_queue).has_free_slots() && m_transmit_buffer->has_space(); } -KResultOr ConsolePort::write(OpenFileDescription& desc, u64, const UserOrKernelBuffer& data, size_t size) +ErrorOr ConsolePort::write(OpenFileDescription& desc, u64, const UserOrKernelBuffer& data, size_t size) { if (!size) return 0; @@ -157,7 +154,7 @@ KResultOr ConsolePort::write(OpenFileDescription& desc, u64, const UserO return total_bytes_copied; } -KResultOr> ConsolePort::open(int options) +ErrorOr> ConsolePort::open(int options) { if (!m_open) m_console.send_open_control_message(m_port, true); diff --git a/Kernel/Bus/VirtIO/ConsolePort.h b/Kernel/Bus/VirtIO/ConsolePort.h index d9b246382d7..81a8aca8741 100644 --- a/Kernel/Bus/VirtIO/ConsolePort.h +++ b/Kernel/Bus/VirtIO/ConsolePort.h @@ -38,10 +38,10 @@ private: virtual StringView class_name() const override { return "VirtIOConsolePort"sv; } virtual bool can_read(const OpenFileDescription&, size_t) const override; - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; virtual bool can_write(const OpenFileDescription&, size_t) const override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; - virtual KResultOr> open(int options) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr> open(int options) override; static unsigned next_device_id; u16 m_receive_queue {}; diff --git a/Kernel/Coredump.cpp b/Kernel/Coredump.cpp index 4bc2f3a8daa..529d4e3e61b 100644 --- a/Kernel/Coredump.cpp +++ b/Kernel/Coredump.cpp @@ -30,7 +30,7 @@ namespace Kernel { return region.name().starts_with("LibJS:"sv) || region.name().starts_with("malloc:"sv); } -KResultOr> Coredump::try_create(NonnullRefPtr process, StringView output_path) +ErrorOr> Coredump::try_create(NonnullRefPtr process, StringView output_path) { if (!process->is_dumpable()) { dbgln("Refusing to generate coredump for non-dumpable process {}", process->pid().value()); @@ -56,7 +56,7 @@ Coredump::Coredump(NonnullRefPtr process, NonnullRefPtr> Coredump::try_create_target_file(Process const& process, StringView output_path) +ErrorOr> Coredump::try_create_target_file(Process const& process, StringView output_path) { auto output_directory = KLexicalPath::dirname(output_path); auto dump_directory = TRY(VirtualFileSystem::the().open_directory(output_directory, VirtualFileSystem::the().root_custody())); @@ -73,7 +73,7 @@ KResultOr> Coredump::try_create_target_file(P UidAndGid { process.uid(), process.gid() })); } -KResult Coredump::write_elf_header() +ErrorOr Coredump::write_elf_header() { ElfW(Ehdr) elf_file_header; elf_file_header.e_ident[EI_MAG0] = 0x7f; @@ -115,10 +115,10 @@ KResult Coredump::write_elf_header() TRY(m_description->write(UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast(&elf_file_header)), sizeof(ElfW(Ehdr)))); - return KSuccess; + return {}; } -KResult Coredump::write_program_headers(size_t notes_size) +ErrorOr Coredump::write_program_headers(size_t notes_size) { size_t offset = sizeof(ElfW(Ehdr)) + m_num_program_headers * sizeof(ElfW(Phdr)); for (auto& region : m_process->address_space().regions()) { @@ -162,10 +162,10 @@ KResult Coredump::write_program_headers(size_t notes_size) TRY(m_description->write(UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast(¬es_pheader)), sizeof(ElfW(Phdr)))); - return KSuccess; + return {}; } -KResult Coredump::write_regions() +ErrorOr Coredump::write_regions() { for (auto& region : m_process->address_space().regions()) { VERIFY(!region->is_kernel()); @@ -195,16 +195,16 @@ KResult Coredump::write_regions() TRY(m_description->write(src_buffer.value(), PAGE_SIZE)); } } - return KSuccess; + return {}; } -KResult Coredump::write_notes_segment(ReadonlyBytes notes_segment) +ErrorOr Coredump::write_notes_segment(ReadonlyBytes notes_segment) { TRY(m_description->write(UserOrKernelBuffer::for_kernel_buffer(const_cast(notes_segment.data())), notes_segment.size())); - return KSuccess; + return {}; } -KResult Coredump::create_notes_process_data(auto& builder) const +ErrorOr Coredump::create_notes_process_data(auto& builder) const { ELF::Core::ProcessInfo info {}; info.header.type = ELF::Core::NotesEntryHeader::Type::ProcessInfo; @@ -230,10 +230,10 @@ KResult Coredump::create_notes_process_data(auto& builder) const } TRY(builder.append('\0')); - return KSuccess; + return {}; } -KResult Coredump::create_notes_threads_data(auto& builder) const +ErrorOr Coredump::create_notes_threads_data(auto& builder) const { for (auto& thread : m_process->threads_for_coredump({})) { ELF::Core::ThreadInfo info {}; @@ -245,10 +245,10 @@ KResult Coredump::create_notes_threads_data(auto& builder) const TRY(builder.append_bytes(ReadonlyBytes { &info, sizeof(info) })); } - return KSuccess; + return {}; } -KResult Coredump::create_notes_regions_data(auto& builder) const +ErrorOr Coredump::create_notes_regions_data(auto& builder) const { size_t region_index = 0; for (auto& region : m_process->address_space().regions()) { @@ -274,10 +274,10 @@ KResult Coredump::create_notes_regions_data(auto& builder) const else TRY(builder.append(name.characters_without_null_termination(), name.length() + 1)); } - return KSuccess; + return {}; } -KResult Coredump::create_notes_metadata_data(auto& builder) const +ErrorOr Coredump::create_notes_metadata_data(auto& builder) const { ELF::Core::Metadata metadata {}; metadata.header.type = ELF::Core::NotesEntryHeader::Type::Metadata; @@ -290,10 +290,10 @@ KResult Coredump::create_notes_metadata_data(auto& builder) const }); } TRY(builder.append('\0')); - return KSuccess; + return {}; } -KResult Coredump::create_notes_segment_data(auto& builder) const +ErrorOr Coredump::create_notes_segment_data(auto& builder) const { TRY(create_notes_process_data(builder)); TRY(create_notes_threads_data(builder)); @@ -304,10 +304,10 @@ KResult Coredump::create_notes_segment_data(auto& builder) const null_entry.type = ELF::Core::NotesEntryHeader::Type::Null; TRY(builder.append(ReadonlyBytes { &null_entry, sizeof(null_entry) })); - return KSuccess; + return {}; } -KResult Coredump::write() +ErrorOr Coredump::write() { SpinlockLocker lock(m_process->address_space().get_lock()); ScopedAddressSpaceSwitcher switcher(m_process); diff --git a/Kernel/Coredump.h b/Kernel/Coredump.h index 439f02fbdc6..3be53ffe0a0 100644 --- a/Kernel/Coredump.h +++ b/Kernel/Coredump.h @@ -15,25 +15,25 @@ namespace Kernel { class Coredump { public: - static KResultOr> try_create(NonnullRefPtr, StringView output_path); + static ErrorOr> try_create(NonnullRefPtr, StringView output_path); ~Coredump() = default; - KResult write(); + ErrorOr write(); private: Coredump(NonnullRefPtr, NonnullRefPtr); - static KResultOr> try_create_target_file(Process const&, StringView output_path); + static ErrorOr> try_create_target_file(Process const&, StringView output_path); - KResult write_elf_header(); - KResult write_program_headers(size_t notes_size); - KResult write_regions(); - KResult write_notes_segment(ReadonlyBytes); + ErrorOr write_elf_header(); + ErrorOr write_program_headers(size_t notes_size); + ErrorOr write_regions(); + ErrorOr write_notes_segment(ReadonlyBytes); - KResult create_notes_segment_data(auto&) const; - KResult create_notes_process_data(auto&) const; - KResult create_notes_threads_data(auto&) const; - KResult create_notes_regions_data(auto&) const; - KResult create_notes_metadata_data(auto&) const; + ErrorOr create_notes_segment_data(auto&) const; + ErrorOr create_notes_process_data(auto&) const; + ErrorOr create_notes_threads_data(auto&) const; + ErrorOr create_notes_regions_data(auto&) const; + ErrorOr create_notes_metadata_data(auto&) const; NonnullRefPtr m_process; NonnullRefPtr m_description; diff --git a/Kernel/Devices/AsyncDeviceRequest.h b/Kernel/Devices/AsyncDeviceRequest.h index 1bd60e571ac..ed690703a53 100644 --- a/Kernel/Devices/AsyncDeviceRequest.h +++ b/Kernel/Devices/AsyncDeviceRequest.h @@ -81,7 +81,7 @@ public: void* get_private() const { return m_private; } template - KResult write_to_buffer(UserOrKernelBuffer& buffer, Args... args) + ErrorOr write_to_buffer(UserOrKernelBuffer& buffer, Args... args) { if (in_target_context(buffer)) return buffer.write(forward(args)...); @@ -90,7 +90,7 @@ public: } template - KResultOr write_to_buffer_buffered(UserOrKernelBuffer& buffer, Args... args) + ErrorOr write_to_buffer_buffered(UserOrKernelBuffer& buffer, Args... args) { if (in_target_context(buffer)) return buffer.write_buffered(forward(args)...); @@ -99,7 +99,7 @@ public: } template - KResult read_from_buffer(const UserOrKernelBuffer& buffer, Args... args) + ErrorOr read_from_buffer(const UserOrKernelBuffer& buffer, Args... args) { if (in_target_context(buffer)) return buffer.read(forward(args)...); @@ -108,7 +108,7 @@ public: } template - KResultOr read_from_buffer_buffered(const UserOrKernelBuffer& buffer, Args... args) + ErrorOr read_from_buffer_buffered(const UserOrKernelBuffer& buffer, Args... args) { if (in_target_context(buffer)) return buffer.read_buffered(forward(args)...); diff --git a/Kernel/Devices/ConsoleDevice.cpp b/Kernel/Devices/ConsoleDevice.cpp index 3fb332747cf..b589d62a95a 100644 --- a/Kernel/Devices/ConsoleDevice.cpp +++ b/Kernel/Devices/ConsoleDevice.cpp @@ -37,14 +37,14 @@ bool ConsoleDevice::can_read(const Kernel::OpenFileDescription&, size_t) const return false; } -Kernel::KResultOr ConsoleDevice::read(OpenFileDescription&, u64, Kernel::UserOrKernelBuffer&, size_t) +ErrorOr ConsoleDevice::read(OpenFileDescription&, u64, Kernel::UserOrKernelBuffer&, size_t) { // FIXME: Implement reading from the console. // Maybe we could use a ring buffer for this device? return 0; } -Kernel::KResultOr ConsoleDevice::write(OpenFileDescription&, u64, const Kernel::UserOrKernelBuffer& data, size_t size) +ErrorOr ConsoleDevice::write(OpenFileDescription&, u64, const Kernel::UserOrKernelBuffer& data, size_t size) { if (!size) return 0; diff --git a/Kernel/Devices/ConsoleDevice.h b/Kernel/Devices/ConsoleDevice.h index ec039a972f2..0309e28ff0a 100644 --- a/Kernel/Devices/ConsoleDevice.h +++ b/Kernel/Devices/ConsoleDevice.h @@ -24,8 +24,8 @@ public: // ^CharacterDevice virtual bool can_read(const Kernel::OpenFileDescription&, size_t) const override; virtual bool can_write(const Kernel::OpenFileDescription&, size_t) const override { return true; } - virtual Kernel::KResultOr read(OpenFileDescription&, u64, Kernel::UserOrKernelBuffer&, size_t) override; - virtual Kernel::KResultOr write(OpenFileDescription&, u64, const Kernel::UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, Kernel::UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const Kernel::UserOrKernelBuffer&, size_t) override; virtual StringView class_name() const override { return "Console"sv; } void put_char(char); diff --git a/Kernel/Devices/Device.cpp b/Kernel/Devices/Device.cpp index 7c9faa960c6..ccd46b18811 100644 --- a/Kernel/Devices/Device.cpp +++ b/Kernel/Devices/Device.cpp @@ -44,7 +44,7 @@ SysFSBlockDevicesDirectory::SysFSBlockDevicesDirectory(SysFSDevicesDirectory con : SysFSDirectory("block"sv, devices_directory) { } -KResult SysFSBlockDevicesDirectory::traverse_as_directory(unsigned fsid, Function callback) const +ErrorOr SysFSBlockDevicesDirectory::traverse_as_directory(unsigned fsid, Function callback) const { VERIFY(m_parent_directory); callback({ ".", { fsid, component_index() }, 0 }); @@ -57,7 +57,7 @@ KResult SysFSBlockDevicesDirectory::traverse_as_directory(unsigned fsid, Functio callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 }); } }); - return KSuccess; + return {}; } RefPtr SysFSBlockDevicesDirectory::lookup(StringView name) { @@ -80,7 +80,7 @@ SysFSCharacterDevicesDirectory::SysFSCharacterDevicesDirectory(SysFSDevicesDirec : SysFSDirectory("char"sv, devices_directory) { } -KResult SysFSCharacterDevicesDirectory::traverse_as_directory(unsigned fsid, Function callback) const +ErrorOr SysFSCharacterDevicesDirectory::traverse_as_directory(unsigned fsid, Function callback) const { VERIFY(m_parent_directory); callback({ ".", { fsid, component_index() }, 0 }); @@ -93,7 +93,7 @@ KResult SysFSCharacterDevicesDirectory::traverse_as_directory(unsigned fsid, Fun callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 }); } }); - return KSuccess; + return {}; } RefPtr SysFSCharacterDevicesDirectory::lookup(StringView name) { @@ -140,7 +140,7 @@ Device::~Device() VERIFY(m_state == State::BeingRemoved); } -KResultOr> Device::pseudo_path(const OpenFileDescription&) const +ErrorOr> Device::pseudo_path(const OpenFileDescription&) const { return KString::try_create(String::formatted("device:{},{}", major(), minor())); } diff --git a/Kernel/Devices/Device.h b/Kernel/Devices/Device.h index 6675242127a..49be585c03a 100644 --- a/Kernel/Devices/Device.h +++ b/Kernel/Devices/Device.h @@ -15,10 +15,10 @@ // - BlockDevice (random access) // - CharacterDevice (sequential) #include +#include #include #include #include -#include #include #include #include @@ -40,7 +40,7 @@ public: unsigned major() const { return m_major; } unsigned minor() const { return m_minor; } - virtual KResultOr> pseudo_path(const OpenFileDescription&) const override; + virtual ErrorOr> pseudo_path(const OpenFileDescription&) const override; UserID uid() const { return m_uid; } GroupID gid() const { return m_gid; } @@ -51,7 +51,7 @@ public: void process_next_queued_request(Badge, const AsyncDeviceRequest&); template - KResultOr> try_make_request(Args&&... args) + ErrorOr> try_make_request(Args&&... args) { auto request = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AsyncRequestType(*this, forward(args)...))); SpinlockLocker lock(m_requests_lock); diff --git a/Kernel/Devices/DeviceManagement.h b/Kernel/Devices/DeviceManagement.h index 50b263ee024..680d72f811f 100644 --- a/Kernel/Devices/DeviceManagement.h +++ b/Kernel/Devices/DeviceManagement.h @@ -7,12 +7,12 @@ #pragma once #include +#include #include #include #include #include #include -#include #include #include #include @@ -51,7 +51,7 @@ public: ConsoleDevice& console_device(); template - static inline KResultOr> try_create_device(Args&&... args) + static inline ErrorOr> try_create_device(Args&&... args) { auto device = TRY(adopt_nonnull_ref_or_enomem(new DeviceType(forward(args)...))); device->after_inserting(); diff --git a/Kernel/Devices/FullDevice.cpp b/Kernel/Devices/FullDevice.cpp index 48f0288b382..686303d0fdf 100644 --- a/Kernel/Devices/FullDevice.cpp +++ b/Kernel/Devices/FullDevice.cpp @@ -34,13 +34,13 @@ bool FullDevice::can_read(const OpenFileDescription&, size_t) const return true; } -KResultOr FullDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr FullDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { TRY(buffer.memset(0, size)); return size; } -KResultOr FullDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t size) +ErrorOr FullDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t size) { if (size == 0) return 0; diff --git a/Kernel/Devices/FullDevice.h b/Kernel/Devices/FullDevice.h index fc7e9926ec9..b9ae0432b7c 100644 --- a/Kernel/Devices/FullDevice.h +++ b/Kernel/Devices/FullDevice.h @@ -22,8 +22,8 @@ private: FullDevice(); // ^CharacterDevice - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; virtual bool can_read(const OpenFileDescription&, size_t) const override; virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } virtual StringView class_name() const override { return "FullDevice"sv; } diff --git a/Kernel/Devices/HID/HIDManagement.h b/Kernel/Devices/HID/HIDManagement.h index 864cfa71855..667acd7cf1f 100644 --- a/Kernel/Devices/HID/HIDManagement.h +++ b/Kernel/Devices/HID/HIDManagement.h @@ -8,11 +8,11 @@ #include #include +#include #include #include #include #include -#include #include #include #include diff --git a/Kernel/Devices/HID/KeyboardDevice.cpp b/Kernel/Devices/HID/KeyboardDevice.cpp index fd59ffe8253..ff42ce3e9c8 100644 --- a/Kernel/Devices/HID/KeyboardDevice.cpp +++ b/Kernel/Devices/HID/KeyboardDevice.cpp @@ -278,7 +278,7 @@ bool KeyboardDevice::can_read(const OpenFileDescription&, size_t) const return !m_queue.is_empty(); } -KResultOr KeyboardDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr KeyboardDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { size_t nread = 0; SpinlockLocker lock(m_queue_lock); @@ -292,13 +292,11 @@ KResultOr KeyboardDevice::read(OpenFileDescription&, u64, UserOrKernelBu lock.unlock(); - auto result = buffer.write_buffered(sizeof(Event), [&](Bytes bytes) { + auto result = TRY(buffer.write_buffered(sizeof(Event), [&](Bytes bytes) { memcpy(bytes.data(), &event, sizeof(Event)); return bytes.size(); - }); - if (result.is_error()) - return result.error(); - VERIFY(result.value() == sizeof(Event)); + })); + VERIFY(result == sizeof(Event)); nread += sizeof(Event); lock.lock(); @@ -306,7 +304,7 @@ KResultOr KeyboardDevice::read(OpenFileDescription&, u64, UserOrKernelBu return nread; } -KResult KeyboardDevice::ioctl(OpenFileDescription&, unsigned request, Userspace arg) +ErrorOr KeyboardDevice::ioctl(OpenFileDescription&, unsigned request, Userspace arg) { switch (request) { case KEYBOARD_IOCTL_GET_NUM_LOCK: { @@ -319,7 +317,7 @@ KResult KeyboardDevice::ioctl(OpenFileDescription&, unsigned request, Userspace< if (num_lock_value != 0 && num_lock_value != 1) return EINVAL; m_num_lock_on = !!num_lock_value; - return KSuccess; + return {}; } case KEYBOARD_IOCTL_GET_CAPS_LOCK: { auto output = static_ptr_cast(arg); @@ -330,7 +328,7 @@ KResult KeyboardDevice::ioctl(OpenFileDescription&, unsigned request, Userspace< if (caps_lock_value != 0 && caps_lock_value != 1) return EINVAL; m_caps_lock_on = !!caps_lock_value; - return KSuccess; + return {}; } default: return EINVAL; diff --git a/Kernel/Devices/HID/KeyboardDevice.h b/Kernel/Devices/HID/KeyboardDevice.h index 60c10c25fed..f5885aabb40 100644 --- a/Kernel/Devices/HID/KeyboardDevice.h +++ b/Kernel/Devices/HID/KeyboardDevice.h @@ -25,16 +25,16 @@ public: virtual ~KeyboardDevice() override; // ^CharacterDevice - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; virtual bool can_read(const OpenFileDescription&, size_t) const override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } // ^HIDDevice virtual Type instrument_type() const override { return Type::Keyboard; } // ^File - virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; + virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; void update_modifier(u8 modifier, bool state) { diff --git a/Kernel/Devices/HID/MouseDevice.cpp b/Kernel/Devices/HID/MouseDevice.cpp index 152ded74212..87441315fa3 100644 --- a/Kernel/Devices/HID/MouseDevice.cpp +++ b/Kernel/Devices/HID/MouseDevice.cpp @@ -24,7 +24,7 @@ bool MouseDevice::can_read(const OpenFileDescription&, size_t) const return !m_queue.is_empty(); } -KResultOr MouseDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr MouseDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { VERIFY(size > 0); size_t nread = 0; diff --git a/Kernel/Devices/HID/MouseDevice.h b/Kernel/Devices/HID/MouseDevice.h index 41a939072d8..e240c27cae6 100644 --- a/Kernel/Devices/HID/MouseDevice.h +++ b/Kernel/Devices/HID/MouseDevice.h @@ -23,9 +23,9 @@ public: virtual ~MouseDevice() override; // ^CharacterDevice - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; virtual bool can_read(const OpenFileDescription&, size_t) const override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } // ^HIDDevice diff --git a/Kernel/Devices/KCOVDevice.cpp b/Kernel/Devices/KCOVDevice.cpp index 8534e8cbb19..d621472ca3d 100644 --- a/Kernel/Devices/KCOVDevice.cpp +++ b/Kernel/Devices/KCOVDevice.cpp @@ -65,7 +65,7 @@ void KCOVDevice::free_process() delete kcov_instance; } -KResultOr> KCOVDevice::open(int options) +ErrorOr> KCOVDevice::open(int options) { auto pid = Process::current().pid(); if (proc_instance->get(pid).has_value()) @@ -77,9 +77,8 @@ KResultOr> KCOVDevice::open(int options) return File::open(options); } -KResult KCOVDevice::ioctl(OpenFileDescription&, unsigned request, Userspace arg) +ErrorOr KCOVDevice::ioctl(OpenFileDescription&, unsigned request, Userspace arg) { - KResult return_value = KSuccess; auto thread = Thread::current(); auto tid = thread->tid(); auto pid = thread->pid(); @@ -90,48 +89,34 @@ KResult KCOVDevice::ioctl(OpenFileDescription&, unsigned request, Userspacespinlock()); switch (request) { - case KCOV_SETBUFSIZE: { - if (kcov_instance->state() >= KCOVInstance::TRACING) { - return_value = EBUSY; - break; - } - return_value = kcov_instance->buffer_allocate((FlatPtr)arg.unsafe_userspace_ptr()); - break; - } - case KCOV_ENABLE: { - if (kcov_instance->state() >= KCOVInstance::TRACING) { - return_value = EBUSY; - break; - } - if (!kcov_instance->has_buffer()) { - return_value = ENOBUFS; - break; - } + case KCOV_SETBUFSIZE: + if (kcov_instance->state() >= KCOVInstance::TRACING) + return EBUSY; + return kcov_instance->buffer_allocate((FlatPtr)arg.unsafe_userspace_ptr()); + case KCOV_ENABLE: + if (kcov_instance->state() >= KCOVInstance::TRACING) + return EBUSY; + if (!kcov_instance->has_buffer()) + return ENOBUFS; VERIFY(kcov_instance->state() == KCOVInstance::OPENED); kcov_instance->set_state(KCOVInstance::TRACING); thread_instance->set(tid, kcov_instance); - break; - } + return {}; case KCOV_DISABLE: { auto maybe_kcov_instance = thread_instance->get(tid); - if (!maybe_kcov_instance.has_value()) { - return_value = ENOENT; - break; - } + if (!maybe_kcov_instance.has_value()) + return ENOENT; VERIFY(kcov_instance->state() == KCOVInstance::TRACING); kcov_instance->set_state(KCOVInstance::OPENED); thread_instance->remove(tid); - break; + return {}; } - default: { - return_value = EINVAL; + default: + return EINVAL; } - }; - - return return_value; } -KResultOr KCOVDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) +ErrorOr KCOVDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) { auto pid = process.pid(); auto maybe_kcov_instance = proc_instance->get(pid); diff --git a/Kernel/Devices/KCOVDevice.h b/Kernel/Devices/KCOVDevice.h index 7905e4002da..3535752c717 100644 --- a/Kernel/Devices/KCOVDevice.h +++ b/Kernel/Devices/KCOVDevice.h @@ -23,8 +23,8 @@ public: static void free_process(); // ^File - KResultOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; - KResultOr> open(int options) override; + ErrorOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; + ErrorOr> open(int options) override; protected: KCOVDevice(); @@ -34,9 +34,9 @@ protected: virtual bool can_read(const OpenFileDescription&, size_t) const override final { return true; } virtual bool can_write(const OpenFileDescription&, size_t) const override final { return true; } virtual void start_request(AsyncBlockDeviceRequest& request) override final { request.complete(AsyncDeviceRequest::Failure); } - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override { return EINVAL; } - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } - virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override { return EINVAL; } + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } + virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; }; } diff --git a/Kernel/Devices/KCOVInstance.cpp b/Kernel/Devices/KCOVInstance.cpp index 39441bca299..9286675ee79 100644 --- a/Kernel/Devices/KCOVInstance.cpp +++ b/Kernel/Devices/KCOVInstance.cpp @@ -14,7 +14,7 @@ KCOVInstance::KCOVInstance(ProcessID pid) m_pid = pid; } -KResult KCOVInstance::buffer_allocate(size_t buffer_size_in_entries) +ErrorOr KCOVInstance::buffer_allocate(size_t buffer_size_in_entries) { if (buffer_size_in_entries < 2 || buffer_size_in_entries > KCOV_MAX_ENTRIES) return EINVAL; @@ -34,7 +34,7 @@ KResult KCOVInstance::buffer_allocate(size_t buffer_size_in_entries) Memory::Region::Access::ReadWrite)); m_buffer = (u64*)m_kernel_region->vaddr().as_ptr(); - return KSuccess; + return {}; } void KCOVInstance::buffer_add_pc(u64 pc) diff --git a/Kernel/Devices/KCOVInstance.h b/Kernel/Devices/KCOVInstance.h index b9099ff6010..097e7b64681 100644 --- a/Kernel/Devices/KCOVInstance.h +++ b/Kernel/Devices/KCOVInstance.h @@ -31,7 +31,7 @@ class KCOVInstance final { public: explicit KCOVInstance(ProcessID pid); - KResult buffer_allocate(size_t buffer_size_in_entries); + ErrorOr buffer_allocate(size_t buffer_size_in_entries); bool has_buffer() const { return m_buffer != nullptr; } void buffer_add_pc(u64 pc); diff --git a/Kernel/Devices/MemoryDevice.cpp b/Kernel/Devices/MemoryDevice.cpp index 3a0d42ad7ee..c728ccb1856 100644 --- a/Kernel/Devices/MemoryDevice.cpp +++ b/Kernel/Devices/MemoryDevice.cpp @@ -31,7 +31,7 @@ UNMAP_AFTER_INIT MemoryDevice::~MemoryDevice() { } -KResultOr MemoryDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) +ErrorOr MemoryDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) { TODO(); } @@ -41,7 +41,7 @@ void MemoryDevice::did_seek(OpenFileDescription&, off_t) TODO(); } -KResultOr MemoryDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) +ErrorOr MemoryDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) { auto viewed_address = PhysicalAddress(offset); diff --git a/Kernel/Devices/MemoryDevice.h b/Kernel/Devices/MemoryDevice.h index 363a09bd829..90c3d928d72 100644 --- a/Kernel/Devices/MemoryDevice.h +++ b/Kernel/Devices/MemoryDevice.h @@ -21,7 +21,7 @@ public: static NonnullRefPtr must_create(); ~MemoryDevice(); - virtual KResultOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; + virtual ErrorOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; private: MemoryDevice(); @@ -30,8 +30,8 @@ private: virtual bool can_read(const OpenFileDescription&, size_t) const override { return true; } virtual bool can_write(const OpenFileDescription&, size_t) const override { return false; } virtual bool is_seekable() const override { return true; } - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } virtual void did_seek(OpenFileDescription&, off_t) override; diff --git a/Kernel/Devices/NullDevice.cpp b/Kernel/Devices/NullDevice.cpp index 29d27f69225..efd78ca75aa 100644 --- a/Kernel/Devices/NullDevice.cpp +++ b/Kernel/Devices/NullDevice.cpp @@ -33,12 +33,12 @@ bool NullDevice::can_read(const OpenFileDescription&, size_t) const return true; } -KResultOr NullDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) +ErrorOr NullDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) { return 0; } -KResultOr NullDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t buffer_size) +ErrorOr NullDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t buffer_size) { return buffer_size; } diff --git a/Kernel/Devices/NullDevice.h b/Kernel/Devices/NullDevice.h index bc638ab4a36..e95c0cd51f7 100644 --- a/Kernel/Devices/NullDevice.h +++ b/Kernel/Devices/NullDevice.h @@ -22,8 +22,8 @@ public: private: NullDevice(); // ^CharacterDevice - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } virtual bool can_read(const OpenFileDescription&, size_t) const override; virtual StringView class_name() const override { return "NullDevice"sv; } diff --git a/Kernel/Devices/RandomDevice.cpp b/Kernel/Devices/RandomDevice.cpp index cc669783664..5c8790c98fc 100644 --- a/Kernel/Devices/RandomDevice.cpp +++ b/Kernel/Devices/RandomDevice.cpp @@ -33,7 +33,7 @@ bool RandomDevice::can_read(const OpenFileDescription&, size_t) const return true; } -KResultOr RandomDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr RandomDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { return buffer.write_buffered<256>(size, [&](Bytes bytes) { get_good_random_bytes(bytes); @@ -41,7 +41,7 @@ KResultOr RandomDevice::read(OpenFileDescription&, u64, UserOrKernelBuff }); } -KResultOr RandomDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t size) +ErrorOr RandomDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t size) { // FIXME: Use input for entropy? I guess that could be a neat feature? return size; diff --git a/Kernel/Devices/RandomDevice.h b/Kernel/Devices/RandomDevice.h index bdb804826b4..cd1ebc08f83 100644 --- a/Kernel/Devices/RandomDevice.h +++ b/Kernel/Devices/RandomDevice.h @@ -22,8 +22,8 @@ private: RandomDevice(); // ^CharacterDevice - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; virtual bool can_read(const OpenFileDescription&, size_t) const override; virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } virtual StringView class_name() const override { return "RandomDevice"sv; } diff --git a/Kernel/Devices/SB16.cpp b/Kernel/Devices/SB16.cpp index 229b8b1d030..027bde6595a 100644 --- a/Kernel/Devices/SB16.cpp +++ b/Kernel/Devices/SB16.cpp @@ -113,7 +113,7 @@ UNMAP_AFTER_INIT void SB16::initialize() set_sample_rate(m_sample_rate); } -KResult SB16::ioctl(OpenFileDescription&, unsigned request, Userspace arg) +ErrorOr SB16::ioctl(OpenFileDescription&, unsigned request, Userspace arg) { switch (request) { case SOUNDCARD_IOCTL_GET_SAMPLE_RATE: { @@ -126,7 +126,7 @@ KResult SB16::ioctl(OpenFileDescription&, unsigned request, Userspace arg return EINVAL; if (m_sample_rate != sample_rate_value) set_sample_rate(sample_rate_value); - return KSuccess; + return {}; } default: return EINVAL; @@ -186,7 +186,7 @@ bool SB16::can_read(OpenFileDescription const&, size_t) const return false; } -KResultOr SB16::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) +ErrorOr SB16::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) { return 0; } @@ -246,7 +246,7 @@ void SB16::wait_for_irq() disable_irq(); } -KResultOr SB16::write(OpenFileDescription&, u64, UserOrKernelBuffer const& data, size_t length) +ErrorOr SB16::write(OpenFileDescription&, u64, UserOrKernelBuffer const& data, size_t length) { if (!m_dma_region) { auto page = MM.allocate_supervisor_physical_page(); diff --git a/Kernel/Devices/SB16.h b/Kernel/Devices/SB16.h index 54746d9e1ab..fe596b5204a 100644 --- a/Kernel/Devices/SB16.h +++ b/Kernel/Devices/SB16.h @@ -27,13 +27,13 @@ public: // ^CharacterDevice virtual bool can_read(const OpenFileDescription&, size_t) const override; - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } virtual StringView purpose() const override { return class_name(); } - virtual KResult ioctl(OpenFileDescription&, unsigned, Userspace) override; + virtual ErrorOr ioctl(OpenFileDescription&, unsigned, Userspace) override; private: SB16(); diff --git a/Kernel/Devices/SerialDevice.cpp b/Kernel/Devices/SerialDevice.cpp index b4f6618d28c..9a21dfb4475 100644 --- a/Kernel/Devices/SerialDevice.cpp +++ b/Kernel/Devices/SerialDevice.cpp @@ -61,7 +61,7 @@ bool SerialDevice::can_read(const OpenFileDescription&, size_t) const return (get_line_status() & DataReady) != 0; } -KResultOr SerialDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr SerialDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { if (!size) return 0; @@ -82,7 +82,7 @@ bool SerialDevice::can_write(const OpenFileDescription&, size_t) const return (get_line_status() & EmptyTransmitterHoldingRegister) != 0; } -KResultOr SerialDevice::write(OpenFileDescription& description, u64, const UserOrKernelBuffer& buffer, size_t size) +ErrorOr SerialDevice::write(OpenFileDescription& description, u64, const UserOrKernelBuffer& buffer, size_t size) { if (!size) return 0; diff --git a/Kernel/Devices/SerialDevice.h b/Kernel/Devices/SerialDevice.h index 9768f4c96c0..221597f4ab5 100644 --- a/Kernel/Devices/SerialDevice.h +++ b/Kernel/Devices/SerialDevice.h @@ -22,9 +22,9 @@ public: // ^CharacterDevice virtual bool can_read(const OpenFileDescription&, size_t) const override; - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; virtual bool can_write(const OpenFileDescription&, size_t) const override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; void put_char(char); diff --git a/Kernel/Devices/ZeroDevice.cpp b/Kernel/Devices/ZeroDevice.cpp index ed10d1ef39f..400fa511177 100644 --- a/Kernel/Devices/ZeroDevice.cpp +++ b/Kernel/Devices/ZeroDevice.cpp @@ -33,13 +33,13 @@ bool ZeroDevice::can_read(const OpenFileDescription&, size_t) const return true; } -KResultOr ZeroDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr ZeroDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { TRY(buffer.memset(0, size)); return size; } -KResultOr ZeroDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t size) +ErrorOr ZeroDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t size) { return size; } diff --git a/Kernel/Devices/ZeroDevice.h b/Kernel/Devices/ZeroDevice.h index 33161b326f4..d5c627d1418 100644 --- a/Kernel/Devices/ZeroDevice.h +++ b/Kernel/Devices/ZeroDevice.h @@ -22,8 +22,8 @@ private: ZeroDevice(); // ^CharacterDevice - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; virtual bool can_read(const OpenFileDescription&, size_t) const override; virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } virtual StringView class_name() const override { return "ZeroDevice"sv; } diff --git a/Kernel/DoubleBuffer.cpp b/Kernel/DoubleBuffer.cpp index c9b8df333b2..ecd90942c70 100644 --- a/Kernel/DoubleBuffer.cpp +++ b/Kernel/DoubleBuffer.cpp @@ -17,7 +17,7 @@ inline void DoubleBuffer::compute_lockfree_metadata() m_space_for_writing = m_capacity - m_write_buffer->size; } -KResultOr> DoubleBuffer::try_create(size_t capacity) +ErrorOr> DoubleBuffer::try_create(size_t capacity) { auto storage = TRY(KBuffer::try_create_with_size(capacity * 2, Memory::Region::Access::ReadWrite, "DoubleBuffer")); return adopt_nonnull_own_or_enomem(new (nothrow) DoubleBuffer(capacity, move(storage))); @@ -45,7 +45,7 @@ void DoubleBuffer::flip() compute_lockfree_metadata(); } -KResultOr DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size) +ErrorOr DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size) { if (!size) return 0; @@ -60,7 +60,7 @@ KResultOr DoubleBuffer::write(const UserOrKernelBuffer& data, size_t siz return bytes_to_write; } -KResultOr DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) +ErrorOr DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) { if (!size) return 0; @@ -78,7 +78,7 @@ KResultOr DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) return nread; } -KResultOr DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size) +ErrorOr DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size) { if (!size) return 0; diff --git a/Kernel/DoubleBuffer.h b/Kernel/DoubleBuffer.h index c826f35fa48..958d537d402 100644 --- a/Kernel/DoubleBuffer.h +++ b/Kernel/DoubleBuffer.h @@ -16,20 +16,20 @@ namespace Kernel { class DoubleBuffer { public: - static KResultOr> try_create(size_t capacity = 65536); - KResultOr write(const UserOrKernelBuffer&, size_t); - KResultOr write(const u8* data, size_t size) + static ErrorOr> try_create(size_t capacity = 65536); + ErrorOr write(const UserOrKernelBuffer&, size_t); + ErrorOr write(const u8* data, size_t size) { return write(UserOrKernelBuffer::for_kernel_buffer(const_cast(data)), size); } - KResultOr read(UserOrKernelBuffer&, size_t); - KResultOr read(u8* data, size_t size) + ErrorOr read(UserOrKernelBuffer&, size_t); + ErrorOr read(u8* data, size_t size) { auto buffer = UserOrKernelBuffer::for_kernel_buffer(data); return read(buffer, size); } - KResultOr peek(UserOrKernelBuffer&, size_t); - KResultOr peek(u8* data, size_t size) + ErrorOr peek(UserOrKernelBuffer&, size_t); + ErrorOr peek(u8* data, size_t size) { auto buffer = UserOrKernelBuffer::for_kernel_buffer(data); return peek(buffer, size); diff --git a/Kernel/FileSystem/AnonymousFile.cpp b/Kernel/FileSystem/AnonymousFile.cpp index 81022dda8f6..b3e0a92b708 100644 --- a/Kernel/FileSystem/AnonymousFile.cpp +++ b/Kernel/FileSystem/AnonymousFile.cpp @@ -19,7 +19,7 @@ AnonymousFile::~AnonymousFile() { } -KResultOr AnonymousFile::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) +ErrorOr AnonymousFile::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) { if (offset != 0) return EINVAL; @@ -30,7 +30,7 @@ KResultOr AnonymousFile::mmap(Process& process, OpenFileDescrip return process.address_space().allocate_region_with_vmobject(range, m_vmobject, offset, {}, prot, shared); } -KResultOr> AnonymousFile::pseudo_path(const OpenFileDescription&) const +ErrorOr> AnonymousFile::pseudo_path(const OpenFileDescription&) const { return KString::try_create(":anonymous-file:"sv); } diff --git a/Kernel/FileSystem/AnonymousFile.h b/Kernel/FileSystem/AnonymousFile.h index 49a21e7fc7f..a6e85810c83 100644 --- a/Kernel/FileSystem/AnonymousFile.h +++ b/Kernel/FileSystem/AnonymousFile.h @@ -13,22 +13,22 @@ namespace Kernel { class AnonymousFile final : public File { public: - static KResultOr> try_create(NonnullRefPtr vmobject) + static ErrorOr> try_create(NonnullRefPtr vmobject) { return adopt_nonnull_ref_or_enomem(new (nothrow) AnonymousFile(move(vmobject))); } virtual ~AnonymousFile() override; - virtual KResultOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; + virtual ErrorOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; private: virtual StringView class_name() const override { return "AnonymousFile"sv; } - virtual KResultOr> pseudo_path(const OpenFileDescription&) const; + virtual ErrorOr> pseudo_path(const OpenFileDescription&) const; virtual bool can_read(const OpenFileDescription&, size_t) const override { return false; } virtual bool can_write(const OpenFileDescription&, size_t) const override { return false; } - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override { return ENOTSUP; } - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return ENOTSUP; } + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override { return ENOTSUP; } + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return ENOTSUP; } explicit AnonymousFile(NonnullRefPtr); diff --git a/Kernel/FileSystem/BlockBasedFileSystem.cpp b/Kernel/FileSystem/BlockBasedFileSystem.cpp index 346a839e972..2b329a7b9ba 100644 --- a/Kernel/FileSystem/BlockBasedFileSystem.cpp +++ b/Kernel/FileSystem/BlockBasedFileSystem.cpp @@ -114,7 +114,7 @@ BlockBasedFileSystem::~BlockBasedFileSystem() { } -KResult BlockBasedFileSystem::initialize() +ErrorOr BlockBasedFileSystem::initialize() { VERIFY(block_size() != 0); auto cached_block_data = TRY(KBuffer::try_create_with_size(DiskCache::EntryCount * block_size())); @@ -124,22 +124,22 @@ KResult BlockBasedFileSystem::initialize() m_cache.with_exclusive([&](auto& cache) { cache = move(disk_cache); }); - return KSuccess; + return {}; } -KResult BlockBasedFileSystem::write_block(BlockIndex index, const UserOrKernelBuffer& data, size_t count, size_t offset, bool allow_cache) +ErrorOr BlockBasedFileSystem::write_block(BlockIndex index, const UserOrKernelBuffer& data, size_t count, size_t offset, bool allow_cache) { VERIFY(m_logical_block_size); VERIFY(offset + count <= block_size()); dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::write_block {}, size={}", index, count); - return m_cache.with_exclusive([&](auto& cache) -> KResult { + return m_cache.with_exclusive([&](auto& cache) -> ErrorOr { if (!allow_cache) { flush_specific_block_if_needed(index); auto base_offset = index.value() * block_size() + offset; auto nwritten = TRY(file_description().write(base_offset, data, count)); VERIFY(nwritten == count); - return KSuccess; + return {}; } auto& entry = cache->get(index); @@ -151,7 +151,7 @@ KResult BlockBasedFileSystem::write_block(BlockIndex index, const UserOrKernelBu cache->mark_dirty(entry); entry.has_data = true; - return KSuccess; + return {}; }); } @@ -195,29 +195,29 @@ bool BlockBasedFileSystem::raw_write_blocks(BlockIndex index, size_t count, cons return true; } -KResult BlockBasedFileSystem::write_blocks(BlockIndex index, unsigned count, const UserOrKernelBuffer& data, bool allow_cache) +ErrorOr BlockBasedFileSystem::write_blocks(BlockIndex index, unsigned count, const UserOrKernelBuffer& data, bool allow_cache) { VERIFY(m_logical_block_size); dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::write_blocks {}, count={}", index, count); for (unsigned i = 0; i < count; ++i) { TRY(write_block(BlockIndex { index.value() + i }, data.offset(i * block_size()), block_size(), 0, allow_cache)); } - return KSuccess; + return {}; } -KResult BlockBasedFileSystem::read_block(BlockIndex index, UserOrKernelBuffer* buffer, size_t count, size_t offset, bool allow_cache) const +ErrorOr BlockBasedFileSystem::read_block(BlockIndex index, UserOrKernelBuffer* buffer, size_t count, size_t offset, bool allow_cache) const { VERIFY(m_logical_block_size); VERIFY(offset + count <= block_size()); dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::read_block {}", index); - return m_cache.with_exclusive([&](auto& cache) -> KResult { + return m_cache.with_exclusive([&](auto& cache) -> ErrorOr { if (!allow_cache) { const_cast(this)->flush_specific_block_if_needed(index); auto base_offset = index.value() * block_size() + offset; auto nread = TRY(file_description().read(*buffer, base_offset, count)); VERIFY(nread == count); - return KSuccess; + return {}; } auto& entry = cache->get(index); @@ -230,11 +230,11 @@ KResult BlockBasedFileSystem::read_block(BlockIndex index, UserOrKernelBuffer* b } if (buffer) TRY(buffer->write(entry.data + offset, count)); - return KSuccess; + return {}; }); } -KResult BlockBasedFileSystem::read_blocks(BlockIndex index, unsigned count, UserOrKernelBuffer& buffer, bool allow_cache) const +ErrorOr BlockBasedFileSystem::read_blocks(BlockIndex index, unsigned count, UserOrKernelBuffer& buffer, bool allow_cache) const { VERIFY(m_logical_block_size); if (!count) @@ -247,7 +247,7 @@ KResult BlockBasedFileSystem::read_blocks(BlockIndex index, unsigned count, User out = out.offset(block_size()); } - return KSuccess; + return {}; } void BlockBasedFileSystem::flush_specific_block_if_needed(BlockIndex index) diff --git a/Kernel/FileSystem/BlockBasedFileSystem.h b/Kernel/FileSystem/BlockBasedFileSystem.h index 2e62f652d47..cc0008dd646 100644 --- a/Kernel/FileSystem/BlockBasedFileSystem.h +++ b/Kernel/FileSystem/BlockBasedFileSystem.h @@ -16,7 +16,7 @@ public: TYPEDEF_DISTINCT_ORDERED_ID(u64, BlockIndex); virtual ~BlockBasedFileSystem() override; - virtual KResult initialize() override; + virtual ErrorOr initialize() override; u64 logical_block_size() const { return m_logical_block_size; }; @@ -26,8 +26,8 @@ public: protected: explicit BlockBasedFileSystem(OpenFileDescription&); - KResult read_block(BlockIndex, UserOrKernelBuffer*, size_t count, size_t offset = 0, bool allow_cache = true) const; - KResult read_blocks(BlockIndex, unsigned count, UserOrKernelBuffer&, bool allow_cache = true) const; + ErrorOr read_block(BlockIndex, UserOrKernelBuffer*, size_t count, size_t offset = 0, bool allow_cache = true) const; + ErrorOr read_blocks(BlockIndex, unsigned count, UserOrKernelBuffer&, bool allow_cache = true) const; bool raw_read(BlockIndex, UserOrKernelBuffer&); bool raw_write(BlockIndex, const UserOrKernelBuffer&); @@ -35,8 +35,8 @@ protected: bool raw_read_blocks(BlockIndex index, size_t count, UserOrKernelBuffer&); bool raw_write_blocks(BlockIndex index, size_t count, const UserOrKernelBuffer&); - KResult write_block(BlockIndex, const UserOrKernelBuffer&, size_t count, size_t offset = 0, bool allow_cache = true); - KResult write_blocks(BlockIndex, unsigned count, const UserOrKernelBuffer&, bool allow_cache = true); + ErrorOr write_block(BlockIndex, const UserOrKernelBuffer&, size_t count, size_t offset = 0, bool allow_cache = true); + ErrorOr write_blocks(BlockIndex, unsigned count, const UserOrKernelBuffer&, bool allow_cache = true); u64 m_logical_block_size { 512 }; diff --git a/Kernel/FileSystem/Custody.cpp b/Kernel/FileSystem/Custody.cpp index 7e77e514cbd..3e31125fac0 100644 --- a/Kernel/FileSystem/Custody.cpp +++ b/Kernel/FileSystem/Custody.cpp @@ -21,15 +21,15 @@ static MutexProtected& all_custodies() return s_all_custodies; } -KResultOr> Custody::try_create(Custody* parent, StringView name, Inode& inode, int mount_flags) +ErrorOr> Custody::try_create(Custody* parent, StringView name, Inode& inode, int mount_flags) { - return all_custodies().with_exclusive([&](auto& all_custodies) -> KResultOr> { + return all_custodies().with_exclusive([&](auto& all_custodies) -> ErrorOr> { for (Custody& custody : all_custodies) { if (custody.parent() == parent && custody.name() == name && &custody.inode() == &inode && custody.mount_flags() == mount_flags) { - return custody; + return NonnullRefPtr { custody }; } } @@ -66,7 +66,7 @@ Custody::~Custody() { } -KResultOr> Custody::try_serialize_absolute_path() const +ErrorOr> Custody::try_serialize_absolute_path() const { if (!parent()) return KString::try_create("/"sv); diff --git a/Kernel/FileSystem/Custody.h b/Kernel/FileSystem/Custody.h index 52e0facf903..e759a83f5db 100644 --- a/Kernel/FileSystem/Custody.h +++ b/Kernel/FileSystem/Custody.h @@ -6,11 +6,11 @@ #pragma once +#include #include #include #include #include -#include #include #include #include @@ -24,7 +24,7 @@ class Custody : public RefCountedBase { public: bool unref() const; - static KResultOr> try_create(Custody* parent, StringView name, Inode&, int mount_flags); + static ErrorOr> try_create(Custody* parent, StringView name, Inode&, int mount_flags); ~Custody(); @@ -33,7 +33,7 @@ public: Inode& inode() { return *m_inode; } Inode const& inode() const { return *m_inode; } StringView name() const { return m_name->view(); } - KResultOr> try_serialize_absolute_path() const; + ErrorOr> try_serialize_absolute_path() const; String absolute_path() const; int mount_flags() const { return m_mount_flags; } diff --git a/Kernel/FileSystem/DevPtsFS.cpp b/Kernel/FileSystem/DevPtsFS.cpp index b96cb5bf13d..ef49e6b4c06 100644 --- a/Kernel/FileSystem/DevPtsFS.cpp +++ b/Kernel/FileSystem/DevPtsFS.cpp @@ -12,7 +12,7 @@ namespace Kernel { -KResultOr> DevPtsFS::try_create() +ErrorOr> DevPtsFS::try_create() { return adopt_nonnull_ref_or_enomem(new (nothrow) DevPtsFS); } @@ -25,7 +25,7 @@ DevPtsFS::~DevPtsFS() { } -KResult DevPtsFS::initialize() +ErrorOr DevPtsFS::initialize() { m_root_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevPtsFSInode(*this, 1, nullptr))); m_root_inode->m_metadata.inode = { fsid(), 1 }; @@ -34,7 +34,7 @@ KResult DevPtsFS::initialize() m_root_inode->m_metadata.gid = 0; m_root_inode->m_metadata.size = 0; m_root_inode->m_metadata.mtime = mepoch; - return KSuccess; + return {}; } static unsigned inode_index_to_pty_index(InodeIndex inode_index) @@ -53,7 +53,7 @@ Inode& DevPtsFS::root_inode() return *m_root_inode; } -KResultOr> DevPtsFS::get_inode(InodeIdentifier inode_id) const +ErrorOr> DevPtsFS::get_inode(InodeIdentifier inode_id) const { if (inode_id.index() == 1) return *m_root_inode; @@ -85,12 +85,12 @@ DevPtsFSInode::~DevPtsFSInode() { } -KResultOr DevPtsFSInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const +ErrorOr DevPtsFSInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { VERIFY_NOT_REACHED(); } -KResultOr DevPtsFSInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) +ErrorOr DevPtsFSInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) { VERIFY_NOT_REACHED(); } @@ -105,7 +105,7 @@ InodeMetadata DevPtsFSInode::metadata() const return m_metadata; } -KResult DevPtsFSInode::traverse_as_directory(Function callback) const +ErrorOr DevPtsFSInode::traverse_as_directory(Function callback) const { if (identifier().index() > 1) return ENOTDIR; @@ -121,10 +121,10 @@ KResult DevPtsFSInode::traverse_as_directory(Function> DevPtsFSInode::lookup(StringView name) +ErrorOr> DevPtsFSInode::lookup(StringView name) { VERIFY(identifier().index() == 1); @@ -135,7 +135,7 @@ KResultOr> DevPtsFSInode::lookup(StringView name) if (!pty_index.has_value()) return ENOENT; - return SlavePTY::all_instances().with([&](auto& list) -> KResultOr> { + return SlavePTY::all_instances().with([&](auto& list) -> ErrorOr> { for (SlavePTY& slave_pty : list) { if (slave_pty.index() != pty_index.value()) continue; @@ -145,32 +145,32 @@ KResultOr> DevPtsFSInode::lookup(StringView name) }); } -KResult DevPtsFSInode::flush_metadata() +ErrorOr DevPtsFSInode::flush_metadata() { - return KSuccess; + return {}; } -KResult DevPtsFSInode::add_child(Inode&, const StringView&, mode_t) +ErrorOr DevPtsFSInode::add_child(Inode&, const StringView&, mode_t) { return EROFS; } -KResultOr> DevPtsFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) +ErrorOr> DevPtsFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) { return EROFS; } -KResult DevPtsFSInode::remove_child(const StringView&) +ErrorOr DevPtsFSInode::remove_child(const StringView&) { return EROFS; } -KResult DevPtsFSInode::chmod(mode_t) +ErrorOr DevPtsFSInode::chmod(mode_t) { return EROFS; } -KResult DevPtsFSInode::chown(UserID, GroupID) +ErrorOr DevPtsFSInode::chown(UserID, GroupID) { return EROFS; } diff --git a/Kernel/FileSystem/DevPtsFS.h b/Kernel/FileSystem/DevPtsFS.h index e907a521777..9b2f6a02298 100644 --- a/Kernel/FileSystem/DevPtsFS.h +++ b/Kernel/FileSystem/DevPtsFS.h @@ -20,16 +20,16 @@ class DevPtsFS final : public FileSystem { public: virtual ~DevPtsFS() override; - static KResultOr> try_create(); + static ErrorOr> try_create(); - virtual KResult initialize() override; + virtual ErrorOr initialize() override; virtual StringView class_name() const override { return "DevPtsFS"sv; } virtual Inode& root_inode() override; private: DevPtsFS(); - KResultOr> get_inode(InodeIdentifier) const; + ErrorOr> get_inode(InodeIdentifier) const; RefPtr m_root_inode; }; @@ -47,17 +47,17 @@ private: DevPtsFSInode(DevPtsFS&, InodeIndex, SlavePTY*); // ^Inode - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; - virtual KResult flush_metadata() override; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual KResult add_child(Inode&, const StringView& name, mode_t) override; - virtual KResult remove_child(const StringView& name) override; - virtual KResult chmod(mode_t) override; - virtual KResult chown(UserID, GroupID) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; + virtual ErrorOr flush_metadata() override; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; + virtual ErrorOr add_child(Inode&, const StringView& name, mode_t) override; + virtual ErrorOr remove_child(const StringView& name) override; + virtual ErrorOr chmod(mode_t) override; + virtual ErrorOr chown(UserID, GroupID) override; WeakPtr m_pty; InodeMetadata m_metadata; diff --git a/Kernel/FileSystem/DevTmpFS.cpp b/Kernel/FileSystem/DevTmpFS.cpp index 6793152bb52..2a129153124 100644 --- a/Kernel/FileSystem/DevTmpFS.cpp +++ b/Kernel/FileSystem/DevTmpFS.cpp @@ -11,7 +11,7 @@ namespace Kernel { -KResultOr> DevTmpFS::try_create() +ErrorOr> DevTmpFS::try_create() { return adopt_nonnull_ref_or_enomem(new (nothrow) DevTmpFS); } @@ -32,10 +32,10 @@ DevTmpFS::~DevTmpFS() { } -KResult DevTmpFS::initialize() +ErrorOr DevTmpFS::initialize() { m_root_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevTmpFSRootDirectoryInode(*this))); - return KSuccess; + return {}; } Inode& DevTmpFS::root_inode() @@ -55,37 +55,37 @@ DevTmpFSInode::DevTmpFSInode(DevTmpFS& fs, unsigned major_number, unsigned minor { } -KResultOr DevTmpFSInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const +ErrorOr DevTmpFSInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { VERIFY_NOT_REACHED(); } -KResult DevTmpFSInode::traverse_as_directory(Function) const +ErrorOr DevTmpFSInode::traverse_as_directory(Function) const { VERIFY_NOT_REACHED(); } -KResultOr> DevTmpFSInode::lookup(StringView) +ErrorOr> DevTmpFSInode::lookup(StringView) { VERIFY_NOT_REACHED(); } -KResult DevTmpFSInode::flush_metadata() +ErrorOr DevTmpFSInode::flush_metadata() { - return KSuccess; + return {}; } -KResultOr DevTmpFSInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) +ErrorOr DevTmpFSInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) { VERIFY_NOT_REACHED(); } -KResultOr> DevTmpFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) +ErrorOr> DevTmpFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) { VERIFY_NOT_REACHED(); } -KResult DevTmpFSInode::add_child(Inode&, const StringView&, mode_t) +ErrorOr DevTmpFSInode::add_child(Inode&, const StringView&, mode_t) { VERIFY_NOT_REACHED(); } @@ -134,30 +134,30 @@ InodeMetadata DevTmpFSInode::metadata() const return metadata; } -KResult DevTmpFSInode::remove_child(const StringView&) +ErrorOr DevTmpFSInode::remove_child(const StringView&) { VERIFY_NOT_REACHED(); } -KResult DevTmpFSInode::chmod(mode_t mode) +ErrorOr DevTmpFSInode::chmod(mode_t mode) { MutexLocker locker(m_inode_lock); mode &= 0777; if (m_mode == mode) - return KSuccess; + return {}; m_mode = mode; - return KSuccess; + return {}; } -KResult DevTmpFSInode::chown(UserID uid, GroupID gid) +ErrorOr DevTmpFSInode::chown(UserID uid, GroupID gid) { MutexLocker locker(m_inode_lock); m_uid = uid; m_gid = gid; - return KSuccess; + return {}; } -KResult DevTmpFSInode::truncate(u64) +ErrorOr DevTmpFSInode::truncate(u64) { return EPERM; } @@ -177,7 +177,7 @@ DevTmpFSLinkInode::DevTmpFSLinkInode(DevTmpFS& fs, NonnullOwnPtr name) { } -KResultOr DevTmpFSLinkInode::read_bytes(off_t offset, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const +ErrorOr DevTmpFSLinkInode::read_bytes(off_t offset, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const { MutexLocker locker(m_inode_lock); VERIFY(offset == 0); @@ -186,7 +186,7 @@ KResultOr DevTmpFSLinkInode::read_bytes(off_t offset, size_t, UserOrKern return m_link->length(); } -KResultOr DevTmpFSLinkInode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription*) +ErrorOr DevTmpFSLinkInode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription*) { auto new_string = TRY(buffer.try_copy_into_kstring(count)); @@ -210,7 +210,7 @@ DevTmpFSDirectoryInode::~DevTmpFSDirectoryInode() { } -KResult DevTmpFSDirectoryInode::traverse_as_directory(Function callback) const +ErrorOr DevTmpFSDirectoryInode::traverse_as_directory(Function callback) const { MutexLocker locker(m_inode_lock); callback({ ".", identifier(), 0 }); @@ -219,10 +219,10 @@ KResult DevTmpFSDirectoryInode::traverse_as_directory(Function> DevTmpFSDirectoryInode::lookup(StringView name) +ErrorOr> DevTmpFSDirectoryInode::lookup(StringView name) { MutexLocker locker(m_inode_lock); for (auto& node : m_nodes) { @@ -230,27 +230,27 @@ KResultOr> DevTmpFSDirectoryInode::lookup(StringView name) return node; } } - return KResult(ENOENT); + return Error::from_errno(ENOENT); } -KResult DevTmpFSDirectoryInode::remove_child(const StringView& name) +ErrorOr DevTmpFSDirectoryInode::remove_child(const StringView& name) { MutexLocker locker(m_inode_lock); for (auto& node : m_nodes) { if (node.name() == name) { m_nodes.remove(node); - return KSuccess; + return {}; } } - return KResult(ENOENT); + return Error::from_errno(ENOENT); } -KResultOr> DevTmpFSDirectoryInode::create_child(StringView name, mode_t mode, dev_t device_mode, UserID, GroupID) +ErrorOr> DevTmpFSDirectoryInode::create_child(StringView name, mode_t mode, dev_t device_mode, UserID, GroupID) { MutexLocker locker(m_inode_lock); for (auto& node : m_nodes) { if (node.name() == name) - return KResult(EEXIST); + return Error::from_errno(EEXIST); } InodeMetadata metadata; @@ -288,12 +288,12 @@ DevTmpFSRootDirectoryInode::DevTmpFSRootDirectoryInode(DevTmpFS& fs) DevTmpFSRootDirectoryInode::~DevTmpFSRootDirectoryInode() { } -KResult DevTmpFSRootDirectoryInode::chmod(mode_t) +ErrorOr DevTmpFSRootDirectoryInode::chmod(mode_t) { return EPERM; } -KResult DevTmpFSRootDirectoryInode::chown(UserID, GroupID) +ErrorOr DevTmpFSRootDirectoryInode::chown(UserID, GroupID) { return EPERM; } @@ -314,30 +314,30 @@ StringView DevTmpFSDeviceInode::name() const return m_name->view(); } -KResultOr DevTmpFSDeviceInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const +ErrorOr DevTmpFSDeviceInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const { MutexLocker locker(m_inode_lock); VERIFY(!!description); RefPtr device = DeviceManagement::the().get_device(m_major_number, m_minor_number); if (!device) - return KResult(ENODEV); + return Error::from_errno(ENODEV); if (!device->can_read(*description, offset)) - return KResult(ENOTIMPL); + return Error::from_errno(ENOTIMPL); auto result = const_cast(*device).read(*description, offset, buffer, count); if (result.is_error()) return result; return result.value(); } -KResultOr DevTmpFSDeviceInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& buffer, OpenFileDescription* description) +ErrorOr DevTmpFSDeviceInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& buffer, OpenFileDescription* description) { MutexLocker locker(m_inode_lock); VERIFY(!!description); RefPtr device = DeviceManagement::the().get_device(m_major_number, m_minor_number); if (!device) - return KResult(ENODEV); + return Error::from_errno(ENODEV); if (!device->can_write(*description, offset)) - return KResult(ENOTIMPL); + return Error::from_errno(ENOTIMPL); auto result = const_cast(*device).write(*description, offset, buffer, count); if (result.is_error()) return result; diff --git a/Kernel/FileSystem/DevTmpFS.h b/Kernel/FileSystem/DevTmpFS.h index 418252d6afa..9a203d83a87 100644 --- a/Kernel/FileSystem/DevTmpFS.h +++ b/Kernel/FileSystem/DevTmpFS.h @@ -20,9 +20,9 @@ class DevTmpFS final : public FileSystem { public: virtual ~DevTmpFS() override; - static KResultOr> try_create(); + static ErrorOr> try_create(); - virtual KResult initialize() override; + virtual ErrorOr initialize() override; virtual StringView class_name() const override { return "DevTmpFS"sv; } virtual Inode& root_inode() override; @@ -48,18 +48,18 @@ public: protected: explicit DevTmpFSInode(DevTmpFS&); DevTmpFSInode(DevTmpFS&, unsigned, unsigned); - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; - virtual KResult flush_metadata() override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; + virtual ErrorOr flush_metadata() override; virtual InodeMetadata metadata() const override final; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual KResult add_child(Inode&, const StringView& name, mode_t) override; - virtual KResult remove_child(const StringView& name) override; - virtual KResult chmod(mode_t) override; - virtual KResult chown(UserID, GroupID) override; - virtual KResult truncate(u64) override; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; + virtual ErrorOr add_child(Inode&, const StringView& name, mode_t) override; + virtual ErrorOr remove_child(const StringView& name) override; + virtual ErrorOr chmod(mode_t) override; + virtual ErrorOr chown(UserID, GroupID) override; + virtual ErrorOr truncate(u64) override; mode_t m_mode { 0600 }; UserID m_uid { 0 }; @@ -96,8 +96,8 @@ private: virtual Type node_type() const override { return m_block_device ? Type::BlockDevice : Type::CharacterDevice; } // ^Inode - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; NonnullOwnPtr m_name; const bool m_block_device; @@ -117,8 +117,8 @@ protected: virtual Type node_type() const override { return Type::Link; } // ^Inode - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; NonnullOwnPtr m_name; OwnPtr m_link; @@ -136,10 +136,10 @@ protected: // ^DevTmpFSInode virtual Type node_type() const override { return Type::Directory; } - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual KResult remove_child(const StringView& name) override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; + virtual ErrorOr remove_child(const StringView& name) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; DevTmpFSDirectoryInode(DevTmpFS&, NonnullOwnPtr name); // ^Inode OwnPtr m_name; @@ -161,8 +161,8 @@ private: virtual Type node_type() const override { return Type::Directory; } explicit DevTmpFSRootDirectoryInode(DevTmpFS&); - virtual KResult chmod(mode_t) override; - virtual KResult chown(UserID, GroupID) override; + virtual ErrorOr chmod(mode_t) override; + virtual ErrorOr chown(UserID, GroupID) override; }; } diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 983f8995d35..721d479184f 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -54,7 +54,7 @@ static unsigned divide_rounded_up(unsigned a, unsigned b) return (a / b) + (a % b != 0); } -KResultOr> Ext2FS::try_create(OpenFileDescription& file_description) +ErrorOr> Ext2FS::try_create(OpenFileDescription& file_description) { return adopt_nonnull_ref_or_enomem(new (nothrow) Ext2FS(file_description)); } @@ -86,7 +86,7 @@ const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const return block_group_descriptors()[group_index.value() - 1]; } -KResult Ext2FS::initialize() +ErrorOr Ext2FS::initialize() { MutexLocker locker(m_lock); @@ -144,7 +144,7 @@ KResult Ext2FS::initialize() } m_root_inode = static_ptr_cast(TRY(get_inode({ fsid(), EXT2_ROOT_INO }))); - return KSuccess; + return {}; } Ext2FSInode& Ext2FS::root_inode() @@ -204,7 +204,7 @@ Ext2FS::BlockListShape Ext2FS::compute_block_list_shape(unsigned blocks) const return shape; } -KResult Ext2FSInode::write_indirect_block(BlockBasedFileSystem::BlockIndex block, Span blocks_indices) +ErrorOr Ext2FSInode::write_indirect_block(BlockBasedFileSystem::BlockIndex block, Span blocks_indices) { const auto entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block()); VERIFY(blocks_indices.size() <= entries_per_block); @@ -224,7 +224,7 @@ KResult Ext2FSInode::write_indirect_block(BlockBasedFileSystem::BlockIndex block return fs().write_block(block, buffer, stream.size()); } -KResult Ext2FSInode::grow_doubly_indirect_block(BlockBasedFileSystem::BlockIndex block, size_t old_blocks_length, Span blocks_indices, Vector& new_meta_blocks, unsigned& meta_blocks) +ErrorOr Ext2FSInode::grow_doubly_indirect_block(BlockBasedFileSystem::BlockIndex block, size_t old_blocks_length, Span blocks_indices, Vector& new_meta_blocks, unsigned& meta_blocks) { const auto entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block()); const auto entries_per_doubly_indirect_block = entries_per_block * entries_per_block; @@ -267,7 +267,7 @@ KResult Ext2FSInode::grow_doubly_indirect_block(BlockBasedFileSystem::BlockIndex return fs().write_block(block, buffer, stream.size()); } -KResult Ext2FSInode::shrink_doubly_indirect_block(BlockBasedFileSystem::BlockIndex block, size_t old_blocks_length, size_t new_blocks_length, unsigned& meta_blocks) +ErrorOr Ext2FSInode::shrink_doubly_indirect_block(BlockBasedFileSystem::BlockIndex block, size_t old_blocks_length, size_t new_blocks_length, unsigned& meta_blocks) { const auto entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block()); const auto entries_per_doubly_indirect_block = entries_per_block * entries_per_block; @@ -299,10 +299,10 @@ KResult Ext2FSInode::shrink_doubly_indirect_block(BlockBasedFileSystem::BlockInd meta_blocks--; } - return KSuccess; + return {}; } -KResult Ext2FSInode::grow_triply_indirect_block(BlockBasedFileSystem::BlockIndex block, size_t old_blocks_length, Span blocks_indices, Vector& new_meta_blocks, unsigned& meta_blocks) +ErrorOr Ext2FSInode::grow_triply_indirect_block(BlockBasedFileSystem::BlockIndex block, size_t old_blocks_length, Span blocks_indices, Vector& new_meta_blocks, unsigned& meta_blocks) { const auto entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block()); const auto entries_per_doubly_indirect_block = entries_per_block * entries_per_block; @@ -348,7 +348,7 @@ KResult Ext2FSInode::grow_triply_indirect_block(BlockBasedFileSystem::BlockIndex return fs().write_block(block, buffer, stream.size()); } -KResult Ext2FSInode::shrink_triply_indirect_block(BlockBasedFileSystem::BlockIndex block, size_t old_blocks_length, size_t new_blocks_length, unsigned& meta_blocks) +ErrorOr Ext2FSInode::shrink_triply_indirect_block(BlockBasedFileSystem::BlockIndex block, size_t old_blocks_length, size_t new_blocks_length, unsigned& meta_blocks) { const auto entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block()); const auto entries_per_doubly_indirect_block = entries_per_block * entries_per_block; @@ -383,10 +383,10 @@ KResult Ext2FSInode::shrink_triply_indirect_block(BlockBasedFileSystem::BlockInd meta_blocks--; } - return KSuccess; + return {}; } -KResult Ext2FSInode::flush_block_list() +ErrorOr Ext2FSInode::flush_block_list() { MutexLocker locker(m_inode_lock); @@ -394,7 +394,7 @@ KResult Ext2FSInode::flush_block_list() m_raw_inode.i_blocks = 0; memset(m_raw_inode.i_block, 0, sizeof(m_raw_inode.i_block)); set_metadata_dirty(true); - return KSuccess; + return {}; } // NOTE: There is a mismatch between i_blocks and blocks.size() since i_blocks includes meta blocks and blocks.size() does not. @@ -512,7 +512,7 @@ KResult Ext2FSInode::flush_block_list() VERIFY(new_meta_blocks.size() == 0); VERIFY(old_shape.meta_blocks == new_shape.meta_blocks); if (!remaining_blocks) - return KSuccess; + return {}; dbgln("we don't know how to write qind ext2fs blocks, they don't exist anyway!"); VERIFY_NOT_REACHED(); @@ -633,7 +633,7 @@ Vector Ext2FSInode::compute_block_list_impl_internal(const e return list; } -KResult Ext2FS::free_inode(Ext2FSInode& inode) +ErrorOr Ext2FS::free_inode(Ext2FSInode& inode) { MutexLocker locker(m_lock); VERIFY(inode.m_raw_inode.i_links_count == 0); @@ -662,7 +662,7 @@ KResult Ext2FS::free_inode(Ext2FSInode& inode) // Mark the inode as free. TRY(set_inode_allocation_state(inode.index(), false)); - return KSuccess; + return {}; } void Ext2FS::flush_block_group_descriptor_table() @@ -773,7 +773,7 @@ InodeMetadata Ext2FSInode::metadata() const return metadata; } -KResult Ext2FSInode::flush_metadata() +ErrorOr Ext2FSInode::flush_metadata() { MutexLocker locker(m_inode_lock); dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]::flush_metadata(): Flushing inode", identifier()); @@ -786,10 +786,10 @@ KResult Ext2FSInode::flush_metadata() } } set_metadata_dirty(false); - return KSuccess; + return {}; } -KResultOr> Ext2FS::get_inode(InodeIdentifier inode) const +ErrorOr> Ext2FS::get_inode(InodeIdentifier inode) const { MutexLocker locker(m_lock); VERIFY(inode.fsid() == fsid()); @@ -799,7 +799,7 @@ KResultOr> Ext2FS::get_inode(InodeIdentifier inode) const if (it != m_inode_cache.end()) { if (!it->value) return ENOENT; - return *it->value; + return NonnullRefPtr { *it->value }; } } @@ -824,7 +824,7 @@ KResultOr> Ext2FS::get_inode(InodeIdentifier inode) const return new_inode; } -KResultOr Ext2FSInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const +ErrorOr Ext2FSInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const { MutexLocker inode_locker(m_inode_lock); VERIFY(offset >= 0); @@ -878,7 +878,7 @@ KResultOr Ext2FSInode::read_bytes(off_t offset, size_t count, UserOrKern } 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); - return result.error(); + return result.release_error(); } } remaining_count -= num_bytes_to_copy; @@ -888,11 +888,11 @@ KResultOr Ext2FSInode::read_bytes(off_t offset, size_t count, UserOrKern return nread; } -KResult Ext2FSInode::resize(u64 new_size) +ErrorOr Ext2FSInode::resize(u64 new_size) { auto old_size = size(); if (old_size == new_size) - return KSuccess; + return {}; if (!((u32)fs().get_features_readonly() & (u32)Ext2FS::FeaturesReadOnly::FileSize64bits) && (new_size >= static_cast(-1))) return ENOSPC; @@ -959,10 +959,10 @@ KResult Ext2FSInode::resize(u64 new_size) } } - return KSuccess; + return {}; } -KResultOr Ext2FSInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& data, OpenFileDescription* description) +ErrorOr Ext2FSInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& data, OpenFileDescription* description) { VERIFY(offset >= 0); @@ -1018,7 +1018,7 @@ KResultOr Ext2FSInode::write_bytes(off_t offset, size_t count, const Use dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]::write_bytes(): Writing block {} (offset_into_block: {})", identifier(), m_block_list[bi.value()], offset_into_block); if (auto result = fs().write_block(m_block_list[bi.value()], data.offset(nwritten), num_bytes_to_copy, offset_into_block, allow_cache); result.is_error()) { dbgln("Ext2FSInode[{}]::write_bytes(): Failed to write block {} (index {})", identifier(), m_block_list[bi.value()], bi); - return result; + return result.release_error(); } remaining_count -= num_bytes_to_copy; nwritten += num_bytes_to_copy; @@ -1059,7 +1059,7 @@ Ext2FS::FeaturesReadOnly Ext2FS::get_features_readonly() const return Ext2FS::FeaturesReadOnly::None; } -KResult Ext2FSInode::traverse_as_directory(Function callback) const +ErrorOr Ext2FSInode::traverse_as_directory(Function callback) const { VERIFY(is_directory()); @@ -1081,16 +1081,16 @@ KResult Ext2FSInode::traverse_as_directory(Functioninode != 0) { dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]::traverse_as_directory(): inode {}, name_len: {}, rec_len: {}, file_type: {}, name: {}", identifier(), entry->inode, entry->name_len, entry->rec_len, entry->file_type, StringView(entry->name, entry->name_len)); if (!callback({ { entry->name, entry->name_len }, { fsid(), entry->inode }, entry->file_type })) - return KSuccess; + return {}; } entry = (ext2_dir_entry_2*)((char*)entry + entry->rec_len); } } - return KSuccess; + return {}; } -KResult Ext2FSInode::write_directory(Vector& entries) +ErrorOr Ext2FSInode::write_directory(Vector& entries) { MutexLocker locker(m_inode_lock); auto block_size = fs().block_size(); @@ -1147,17 +1147,17 @@ KResult Ext2FSInode::write_directory(Vector& entries) set_metadata_dirty(true); if (nwritten != directory_data.size()) return EIO; - return KSuccess; + return {}; } -KResultOr> Ext2FSInode::create_child(StringView name, mode_t mode, dev_t dev, UserID uid, GroupID gid) +ErrorOr> Ext2FSInode::create_child(StringView name, mode_t mode, dev_t dev, UserID uid, GroupID gid) { if (::is_directory(mode)) return fs().create_directory(*this, name, mode, uid, gid); return fs().create_inode(*this, name, mode, dev, uid, gid); } -KResult Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode) +ErrorOr Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode) { MutexLocker locker(m_inode_lock); VERIFY(is_directory()); @@ -1192,10 +1192,10 @@ KResult Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode m_lookup_cache.set(name, child.index()); did_add_child(child.identifier(), name); - return KSuccess; + return {}; } -KResult Ext2FSInode::remove_child(const StringView& name) +ErrorOr Ext2FSInode::remove_child(const StringView& name) { MutexLocker locker(m_inode_lock); dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]::remove_child(): Removing '{}'", identifier(), name); @@ -1225,7 +1225,7 @@ KResult Ext2FSInode::remove_child(const StringView& name) TRY(child_inode->decrement_link_count()); did_remove_child(child_id, name); - return KSuccess; + return {}; } u64 Ext2FS::inodes_per_block() const @@ -1247,7 +1247,7 @@ u64 Ext2FS::blocks_per_group() const return EXT2_BLOCKS_PER_GROUP(&super_block()); } -KResult Ext2FS::write_ext2_inode(InodeIndex inode, ext2_inode const& e2inode) +ErrorOr Ext2FS::write_ext2_inode(InodeIndex inode, ext2_inode const& e2inode) { BlockIndex block_index; unsigned offset; @@ -1257,7 +1257,7 @@ KResult Ext2FS::write_ext2_inode(InodeIndex inode, ext2_inode const& e2inode) return write_block(block_index, buffer, inode_size(), offset); } -auto Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count) -> KResultOr> +auto Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count) -> ErrorOr> { dbgln_if(EXT2_DEBUG, "Ext2FS: allocate_blocks(preferred group: {}, count {})", preferred_group_index, count); if (count == 0) @@ -1304,10 +1304,7 @@ auto Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count) -> dbgln_if(EXT2_DEBUG, "Ext2FS: allocating free region of size: {} [{}]", free_region_size, group_index); for (size_t i = 0; i < free_region_size; ++i) { BlockIndex block_index = (first_unset_bit_index.value() + i) + first_block_in_group.value(); - if (auto result = set_block_allocation_state(block_index, true); result.is_error()) { - dbgln("Ext2FS: Failed to allocate block {} in allocate_blocks()", block_index); - return result; - } + TRY(set_block_allocation_state(block_index, true)); blocks.unchecked_append(block_index); dbgln_if(EXT2_DEBUG, " allocated > {}", block_index); } @@ -1317,7 +1314,7 @@ auto Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count) -> return blocks; } -KResultOr Ext2FS::allocate_inode(GroupIndex preferred_group) +ErrorOr Ext2FS::allocate_inode(GroupIndex preferred_group) { dbgln_if(EXT2_DEBUG, "Ext2FS: allocate_inode(preferred_group: {})", preferred_group); MutexLocker locker(m_lock); @@ -1391,7 +1388,7 @@ auto Ext2FS::group_index_from_inode(InodeIndex inode) const -> GroupIndex return (inode.value() - 1) / inodes_per_group() + 1; } -KResultOr Ext2FS::get_inode_allocation_state(InodeIndex index) const +ErrorOr Ext2FS::get_inode_allocation_state(InodeIndex index) const { MutexLocker locker(m_lock); if (index == 0) @@ -1405,7 +1402,7 @@ KResultOr Ext2FS::get_inode_allocation_state(InodeIndex index) const return cached_bitmap->bitmap(inodes_per_group()).get(bit_index); } -KResult Ext2FS::update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, bool new_state, u32& super_block_counter, u16& group_descriptor_counter) +ErrorOr Ext2FS::update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, bool new_state, u32& super_block_counter, u16& group_descriptor_counter) { auto* cached_bitmap = TRY(get_bitmap_block(bitmap_block)); bool current_state = cached_bitmap->bitmap(blocks_per_group()).get(bit_index); @@ -1426,10 +1423,10 @@ KResult Ext2FS::update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, b m_super_block_dirty = true; m_block_group_descriptors_dirty = true; - return KSuccess; + return {}; } -KResult Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state) +ErrorOr Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state) { MutexLocker locker(m_lock); auto group_index = group_index_from_inode(inode_index); @@ -1446,26 +1443,23 @@ Ext2FS::BlockIndex Ext2FS::first_block_index() const return block_size() == 1024 ? 1 : 0; } -KResultOr Ext2FS::get_bitmap_block(BlockIndex bitmap_block_index) +ErrorOr Ext2FS::get_bitmap_block(BlockIndex bitmap_block_index) { for (auto& cached_bitmap : m_cached_bitmaps) { if (cached_bitmap->bitmap_block_index == bitmap_block_index) - return cached_bitmap; + return cached_bitmap.ptr(); } auto block = TRY(KBuffer::try_create_with_size(block_size(), Memory::Region::Access::ReadWrite, "Ext2FS: Cached bitmap block")); auto buffer = UserOrKernelBuffer::for_kernel_buffer(block->data()); - if (auto result = read_block(bitmap_block_index, &buffer, block_size()); result.is_error()) { - dbgln("Ext2FS: Failed to load bitmap block {}", bitmap_block_index); - return result; - } + TRY(read_block(bitmap_block_index, &buffer, block_size())); auto new_bitmap = TRY(adopt_nonnull_own_or_enomem(new (nothrow) CachedBitmap(bitmap_block_index, move(block)))); if (!m_cached_bitmaps.try_append(move(new_bitmap))) return ENOMEM; - return m_cached_bitmaps.last(); + return m_cached_bitmaps.last().ptr(); } -KResult Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state) +ErrorOr Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state) { VERIFY(block_index != 0); MutexLocker locker(m_lock); @@ -1479,7 +1473,7 @@ KResult Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_stat return update_bitmap_block(bgd.bg_block_bitmap, bit_index, new_state, m_super_block.s_free_blocks_count, bgd.bg_free_blocks_count); } -KResult Ext2FS::create_directory(Ext2FSInode& parent_inode, StringView name, mode_t mode, UserID uid, GroupID gid) +ErrorOr> Ext2FS::create_directory(Ext2FSInode& parent_inode, StringView name, mode_t mode, UserID uid, GroupID gid) { MutexLocker locker(m_lock); VERIFY(is_directory(mode)); @@ -1499,10 +1493,10 @@ KResult Ext2FS::create_directory(Ext2FSInode& parent_inode, StringView name, mod ++bgd.bg_used_dirs_count; m_block_group_descriptors_dirty = true; - return KSuccess; + return inode; } -KResultOr> Ext2FS::create_inode(Ext2FSInode& parent_inode, StringView name, mode_t mode, dev_t dev, UserID uid, GroupID gid) +ErrorOr> Ext2FS::create_inode(Ext2FSInode& parent_inode, StringView name, mode_t mode, dev_t dev, UserID uid, GroupID gid) { if (name.length() > EXT2_NAME_LEN) return ENAMETOOLONG; @@ -1542,11 +1536,11 @@ KResultOr> Ext2FS::create_inode(Ext2FSInode& parent_inode, return new_inode; } -KResult Ext2FSInode::populate_lookup_cache() const +ErrorOr Ext2FSInode::populate_lookup_cache() const { MutexLocker locker(m_inode_lock); if (!m_lookup_cache.is_empty()) - return KSuccess; + return {}; HashMap children; TRY(traverse_as_directory([&children](auto& entry) { @@ -1556,10 +1550,10 @@ KResult Ext2FSInode::populate_lookup_cache() const VERIFY(m_lookup_cache.is_empty()); m_lookup_cache = move(children); - return KSuccess; + return {}; } -KResultOr> Ext2FSInode::lookup(StringView name) +ErrorOr> Ext2FSInode::lookup(StringView name) { VERIFY(is_directory()); dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]:lookup(): Looking up '{}'", identifier(), name); @@ -1584,37 +1578,37 @@ void Ext2FSInode::one_ref_left() // FIXME: I would like to not live forever, but uncached Ext2FS is fucking painful right now. } -KResult Ext2FSInode::set_atime(time_t t) +ErrorOr Ext2FSInode::set_atime(time_t t) { MutexLocker locker(m_inode_lock); if (fs().is_readonly()) return EROFS; m_raw_inode.i_atime = t; set_metadata_dirty(true); - return KSuccess; + return {}; } -KResult Ext2FSInode::set_ctime(time_t t) +ErrorOr Ext2FSInode::set_ctime(time_t t) { MutexLocker locker(m_inode_lock); if (fs().is_readonly()) return EROFS; m_raw_inode.i_ctime = t; set_metadata_dirty(true); - return KSuccess; + return {}; } -KResult Ext2FSInode::set_mtime(time_t t) +ErrorOr Ext2FSInode::set_mtime(time_t t) { MutexLocker locker(m_inode_lock); if (fs().is_readonly()) return EROFS; m_raw_inode.i_mtime = t; set_metadata_dirty(true); - return KSuccess; + return {}; } -KResult Ext2FSInode::increment_link_count() +ErrorOr Ext2FSInode::increment_link_count() { MutexLocker locker(m_inode_lock); if (fs().is_readonly()) @@ -1624,10 +1618,10 @@ KResult Ext2FSInode::increment_link_count() return EMLINK; ++m_raw_inode.i_links_count; set_metadata_dirty(true); - return KSuccess; + return {}; } -KResult Ext2FSInode::decrement_link_count() +ErrorOr Ext2FSInode::decrement_link_count() { MutexLocker locker(m_inode_lock); if (fs().is_readonly()) @@ -1642,7 +1636,7 @@ KResult Ext2FSInode::decrement_link_count() if (ref_count() == 1 && m_raw_inode.i_links_count == 0) fs().uncache_inode(index()); - return KSuccess; + return {}; } void Ext2FS::uncache_inode(InodeIndex index) @@ -1651,38 +1645,38 @@ void Ext2FS::uncache_inode(InodeIndex index) m_inode_cache.remove(index); } -KResult Ext2FSInode::chmod(mode_t mode) +ErrorOr Ext2FSInode::chmod(mode_t mode) { MutexLocker locker(m_inode_lock); if (m_raw_inode.i_mode == mode) - return KSuccess; + return {}; m_raw_inode.i_mode = mode; set_metadata_dirty(true); - return KSuccess; + return {}; } -KResult Ext2FSInode::chown(UserID uid, GroupID gid) +ErrorOr Ext2FSInode::chown(UserID uid, GroupID gid) { MutexLocker locker(m_inode_lock); if (m_raw_inode.i_uid == uid && m_raw_inode.i_gid == gid) - return KSuccess; + return {}; m_raw_inode.i_uid = uid.value(); m_raw_inode.i_gid = gid.value(); set_metadata_dirty(true); - return KSuccess; + return {}; } -KResult Ext2FSInode::truncate(u64 size) +ErrorOr Ext2FSInode::truncate(u64 size) { MutexLocker locker(m_inode_lock); if (static_cast(m_raw_inode.i_size) == size) - return KSuccess; + return {}; TRY(resize(size)); set_metadata_dirty(true); - return KSuccess; + return {}; } -KResultOr Ext2FSInode::get_block_address(int index) +ErrorOr Ext2FSInode::get_block_address(int index) { MutexLocker locker(m_inode_lock); @@ -1719,7 +1713,7 @@ unsigned Ext2FS::free_inode_count() const return super_block().s_free_inodes_count; } -KResult Ext2FS::prepare_to_unmount() +ErrorOr Ext2FS::prepare_to_unmount() { MutexLocker locker(m_lock); @@ -1730,6 +1724,6 @@ KResult Ext2FS::prepare_to_unmount() m_inode_cache.clear(); m_root_inode = nullptr; - return KSuccess; + return {}; } } diff --git a/Kernel/FileSystem/Ext2FileSystem.h b/Kernel/FileSystem/Ext2FileSystem.h index b09e04bd722..bf220ee454d 100644 --- a/Kernel/FileSystem/Ext2FileSystem.h +++ b/Kernel/FileSystem/Ext2FileSystem.h @@ -38,34 +38,34 @@ public: private: // ^Inode - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; - virtual KResult flush_metadata() override; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& data, OpenFileDescription*) override; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual KResult add_child(Inode& child, const StringView& name, mode_t) override; - virtual KResult remove_child(const StringView& name) override; - virtual KResult set_atime(time_t) override; - virtual KResult set_ctime(time_t) override; - virtual KResult set_mtime(time_t) override; - virtual KResult increment_link_count() override; - virtual KResult decrement_link_count() override; - virtual KResult chmod(mode_t) override; - virtual KResult chown(UserID, GroupID) override; - virtual KResult truncate(u64) override; - virtual KResultOr get_block_address(int) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; + virtual ErrorOr flush_metadata() override; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& data, OpenFileDescription*) override; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; + virtual ErrorOr add_child(Inode& child, const StringView& name, mode_t) override; + virtual ErrorOr remove_child(const StringView& name) override; + virtual ErrorOr set_atime(time_t) override; + virtual ErrorOr set_ctime(time_t) override; + virtual ErrorOr set_mtime(time_t) override; + virtual ErrorOr increment_link_count() override; + virtual ErrorOr decrement_link_count() override; + virtual ErrorOr chmod(mode_t) override; + virtual ErrorOr chown(UserID, GroupID) override; + virtual ErrorOr truncate(u64) override; + virtual ErrorOr get_block_address(int) override; - KResult write_directory(Vector&); - KResult populate_lookup_cache() const; - KResult resize(u64); - KResult write_indirect_block(BlockBasedFileSystem::BlockIndex, Span); - KResult grow_doubly_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, Span, Vector&, unsigned&); - KResult shrink_doubly_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, size_t, unsigned&); - KResult grow_triply_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, Span, Vector&, unsigned&); - KResult shrink_triply_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, size_t, unsigned&); - KResult flush_block_list(); + ErrorOr write_directory(Vector&); + ErrorOr populate_lookup_cache() const; + ErrorOr resize(u64); + ErrorOr write_indirect_block(BlockBasedFileSystem::BlockIndex, Span); + ErrorOr grow_doubly_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, Span, Vector&, unsigned&); + ErrorOr shrink_doubly_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, size_t, unsigned&); + ErrorOr grow_triply_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, Span, Vector&, unsigned&); + ErrorOr shrink_triply_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, size_t, unsigned&); + ErrorOr flush_block_list(); Vector compute_block_list() const; Vector compute_block_list_with_meta_blocks() const; Vector compute_block_list_impl(bool include_block_list_blocks) const; @@ -89,17 +89,17 @@ public: FileSize64bits = 1 << 1, }; - static KResultOr> try_create(OpenFileDescription&); + static ErrorOr> try_create(OpenFileDescription&); virtual ~Ext2FS() override; - virtual KResult initialize() override; + virtual ErrorOr initialize() override; virtual unsigned total_block_count() const override; virtual unsigned free_block_count() const override; virtual unsigned total_inode_count() const override; virtual unsigned free_inode_count() const override; - virtual KResult prepare_to_unmount() override; + virtual ErrorOr prepare_to_unmount() override; virtual bool supports_watchers() const override { return true; } @@ -122,30 +122,30 @@ private: u64 blocks_per_group() const; u64 inode_size() const; - KResult write_ext2_inode(InodeIndex, ext2_inode const&); + ErrorOr write_ext2_inode(InodeIndex, ext2_inode const&); bool find_block_containing_inode(InodeIndex, BlockIndex& block_index, unsigned& offset) const; bool flush_super_block(); virtual StringView class_name() const override { return "Ext2FS"sv; } virtual Ext2FSInode& root_inode() override; - KResultOr> get_inode(InodeIdentifier) const; - KResultOr> create_inode(Ext2FSInode& parent_inode, StringView name, mode_t, dev_t, UserID, GroupID); - KResult create_directory(Ext2FSInode& parent_inode, StringView name, mode_t, UserID, GroupID); + ErrorOr> get_inode(InodeIdentifier) const; + ErrorOr> create_inode(Ext2FSInode& parent_inode, StringView name, mode_t, dev_t, UserID, GroupID); + ErrorOr> create_directory(Ext2FSInode& parent_inode, StringView name, mode_t, UserID, GroupID); virtual void flush_writes() override; BlockIndex first_block_index() const; - KResultOr allocate_inode(GroupIndex preferred_group = 0); - KResultOr> allocate_blocks(GroupIndex preferred_group_index, size_t count); + ErrorOr allocate_inode(GroupIndex preferred_group = 0); + ErrorOr> allocate_blocks(GroupIndex preferred_group_index, size_t count); GroupIndex group_index_from_inode(InodeIndex) const; GroupIndex group_index_from_block_index(BlockIndex) const; - KResultOr get_inode_allocation_state(InodeIndex) const; - KResult set_inode_allocation_state(InodeIndex, bool); - KResult set_block_allocation_state(BlockIndex, bool); + ErrorOr get_inode_allocation_state(InodeIndex) const; + ErrorOr set_inode_allocation_state(InodeIndex, bool); + ErrorOr set_block_allocation_state(BlockIndex, bool); void uncache_inode(InodeIndex); - KResult free_inode(Ext2FSInode&); + ErrorOr free_inode(Ext2FSInode&); struct BlockListShape { unsigned direct_blocks { 0 }; @@ -179,8 +179,8 @@ private: BitmapView bitmap(u32 blocks_per_group) { return BitmapView { buffer->data(), blocks_per_group }; } }; - KResultOr get_bitmap_block(BlockIndex); - KResult update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, bool new_state, u32& super_block_counter, u16& group_descriptor_counter); + ErrorOr get_bitmap_block(BlockIndex); + ErrorOr update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, bool new_state, u32& super_block_counter, u16& group_descriptor_counter); Vector> m_cached_bitmaps; RefPtr m_root_inode; diff --git a/Kernel/FileSystem/FIFO.cpp b/Kernel/FileSystem/FIFO.cpp index 2d1e5850434..1fadae8c6b4 100644 --- a/Kernel/FileSystem/FIFO.cpp +++ b/Kernel/FileSystem/FIFO.cpp @@ -16,13 +16,13 @@ namespace Kernel { static Atomic s_next_fifo_id = 1; -KResultOr> FIFO::try_create(UserID uid) +ErrorOr> FIFO::try_create(UserID uid) { auto buffer = TRY(DoubleBuffer::try_create()); return adopt_nonnull_ref_or_enomem(new (nothrow) FIFO(uid, move(buffer))); } -KResultOr> FIFO::open_direction(FIFO::Direction direction) +ErrorOr> FIFO::open_direction(FIFO::Direction direction) { auto description = TRY(OpenFileDescription::try_create(*this)); attach(direction); @@ -30,7 +30,7 @@ KResultOr> FIFO::open_direction(FIFO::Directi return description; } -KResultOr> FIFO::open_direction_blocking(FIFO::Direction direction) +ErrorOr> FIFO::open_direction_blocking(FIFO::Direction direction) { MutexLocker locker(m_open_lock); @@ -109,7 +109,7 @@ bool FIFO::can_write(const OpenFileDescription&, size_t) const return m_buffer->space_for_writing() || !m_readers; } -KResultOr FIFO::read(OpenFileDescription& fd, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr FIFO::read(OpenFileDescription& fd, u64, UserOrKernelBuffer& buffer, size_t size) { if (m_buffer->is_empty()) { if (!m_writers) @@ -120,7 +120,7 @@ KResultOr FIFO::read(OpenFileDescription& fd, u64, UserOrKernelBuffer& b return m_buffer->read(buffer, size); } -KResultOr FIFO::write(OpenFileDescription& fd, u64, const UserOrKernelBuffer& buffer, size_t size) +ErrorOr FIFO::write(OpenFileDescription& fd, u64, const UserOrKernelBuffer& buffer, size_t size) { if (!m_readers) { Thread::current()->send_signal(SIGPIPE, &Process::current()); @@ -132,16 +132,16 @@ KResultOr FIFO::write(OpenFileDescription& fd, u64, const UserOrKernelBu return m_buffer->write(buffer, size); } -KResultOr> FIFO::pseudo_path(const OpenFileDescription&) const +ErrorOr> FIFO::pseudo_path(const OpenFileDescription&) const { return KString::try_create(String::formatted("fifo:{}", m_fifo_id)); } -KResult FIFO::stat(::stat& st) const +ErrorOr FIFO::stat(::stat& st) const { memset(&st, 0, sizeof(st)); st.st_mode = S_IFIFO; - return KSuccess; + return {}; } } diff --git a/Kernel/FileSystem/FIFO.h b/Kernel/FileSystem/FIFO.h index 63523608632..9c20a814526 100644 --- a/Kernel/FileSystem/FIFO.h +++ b/Kernel/FileSystem/FIFO.h @@ -24,13 +24,13 @@ public: Writer }; - static KResultOr> try_create(UserID); + static ErrorOr> try_create(UserID); virtual ~FIFO() override; UserID uid() const { return m_uid; } - KResultOr> open_direction(Direction); - KResultOr> open_direction_blocking(Direction); + ErrorOr> open_direction(Direction); + ErrorOr> open_direction_blocking(Direction); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Woverloaded-virtual" @@ -40,12 +40,12 @@ public: private: // ^File - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; - virtual KResult stat(::stat&) const override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr stat(::stat&) const override; virtual bool can_read(const OpenFileDescription&, size_t) const override; virtual bool can_write(const OpenFileDescription&, size_t) const override; - virtual KResultOr> pseudo_path(const OpenFileDescription&) const override; + virtual ErrorOr> pseudo_path(const OpenFileDescription&) const override; virtual StringView class_name() const override { return "FIFO"sv; } virtual bool is_fifo() const override { return true; } diff --git a/Kernel/FileSystem/File.cpp b/Kernel/FileSystem/File.cpp index fd98a3a6290..527d48d9222 100644 --- a/Kernel/FileSystem/File.cpp +++ b/Kernel/FileSystem/File.cpp @@ -29,7 +29,7 @@ bool File::unref() const return true; } -KResultOr> File::open(int options) +ErrorOr> File::open(int options) { auto description = OpenFileDescription::try_create(*this); if (!description.is_error()) { @@ -39,25 +39,25 @@ KResultOr> File::open(int options) return description; } -KResult File::close() +ErrorOr File::close() { - return KSuccess; + return {}; } -KResult File::ioctl(OpenFileDescription&, unsigned, Userspace) +ErrorOr File::ioctl(OpenFileDescription&, unsigned, Userspace) { return ENOTTY; } -KResultOr File::mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64, int, bool) +ErrorOr File::mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64, int, bool) { return ENODEV; } -KResult File::attach(OpenFileDescription&) +ErrorOr File::attach(OpenFileDescription&) { m_attach_count++; - return KSuccess; + return {}; } void File::detach(OpenFileDescription&) diff --git a/Kernel/FileSystem/File.h b/Kernel/FileSystem/File.h index c42ac1c51ad..85b5fc8a356 100644 --- a/Kernel/FileSystem/File.h +++ b/Kernel/FileSystem/File.h @@ -6,12 +6,12 @@ #pragma once +#include #include #include #include #include #include -#include #include #include #include @@ -78,28 +78,28 @@ public: virtual void before_removing() { } virtual ~File(); - virtual KResultOr> open(int options); - virtual KResult close(); + virtual ErrorOr> open(int options); + virtual ErrorOr close(); virtual bool can_read(const OpenFileDescription&, size_t) const = 0; virtual bool can_write(const OpenFileDescription&, size_t) const = 0; - virtual KResult attach(OpenFileDescription&); + virtual ErrorOr attach(OpenFileDescription&); virtual void detach(OpenFileDescription&); virtual void did_seek(OpenFileDescription&, off_t) { } - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) = 0; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) = 0; - virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace arg); - virtual KResultOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared); - virtual KResult stat(::stat&) const { return EBADF; } + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) = 0; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) = 0; + virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg); + virtual ErrorOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared); + virtual ErrorOr stat(::stat&) const { return EBADF; } // Although this might be better described "name" or "description", these terms already have other meanings. - virtual KResultOr> pseudo_path(const OpenFileDescription&) const = 0; + virtual ErrorOr> pseudo_path(const OpenFileDescription&) const = 0; - virtual KResult truncate(u64) { return EINVAL; } - virtual KResult sync() { return EINVAL; } - virtual KResult chown(OpenFileDescription&, UserID, GroupID) { return EBADF; } - virtual KResult chmod(OpenFileDescription&, mode_t) { return EBADF; } + virtual ErrorOr truncate(u64) { return EINVAL; } + virtual ErrorOr sync() { return EINVAL; } + virtual ErrorOr chown(OpenFileDescription&, UserID, GroupID) { return EBADF; } + virtual ErrorOr chmod(OpenFileDescription&, mode_t) { return EBADF; } virtual StringView class_name() const = 0; diff --git a/Kernel/FileSystem/FileSystem.h b/Kernel/FileSystem/FileSystem.h index b5e22ef15c4..9222d8e6aae 100644 --- a/Kernel/FileSystem/FileSystem.h +++ b/Kernel/FileSystem/FileSystem.h @@ -6,10 +6,10 @@ #pragma once +#include #include #include #include -#include #include #include #include @@ -31,7 +31,7 @@ public: static void sync(); static void lock_all(); - virtual KResult initialize() = 0; + virtual ErrorOr initialize() = 0; virtual StringView class_name() const = 0; virtual Inode& root_inode() = 0; virtual bool supports_watchers() const { return false; } @@ -43,7 +43,7 @@ public: virtual unsigned total_inode_count() const { return 0; } virtual unsigned free_inode_count() const { return 0; } - virtual KResult prepare_to_unmount() { return KSuccess; } + virtual ErrorOr prepare_to_unmount() { return {}; } struct DirectoryEntryView { DirectoryEntryView(const StringView& name, InodeIdentifier, u8 file_type); diff --git a/Kernel/FileSystem/ISO9660FileSystem.cpp b/Kernel/FileSystem/ISO9660FileSystem.cpp index 492433f3d73..b42398719fa 100644 --- a/Kernel/FileSystem/ISO9660FileSystem.cpp +++ b/Kernel/FileSystem/ISO9660FileSystem.cpp @@ -46,7 +46,7 @@ public: ISO::DirectoryRecordHeader const* operator*() { return m_current_header; } // Recurses into subdirectories. May fail. - KResultOr next() + ErrorOr next() { if (done()) return false; @@ -148,10 +148,10 @@ public: } private: - KResult read_directory_contents() + ErrorOr read_directory_contents() { m_current_directory.entry = TRY(m_fs.directory_entry_for_record({}, m_current_header)); - return KSuccess; + return {}; } void get_header() @@ -171,7 +171,7 @@ private: Vector m_directory_stack; }; -KResultOr> ISO9660FS::try_create(OpenFileDescription& description) +ErrorOr> ISO9660FS::try_create(OpenFileDescription& description) { return adopt_nonnull_ref_or_enomem(new (nothrow) ISO9660FS(description)); } @@ -187,12 +187,12 @@ ISO9660FS::~ISO9660FS() { } -KResult ISO9660FS::initialize() +ErrorOr ISO9660FS::initialize() { TRY(BlockBasedFileSystem::initialize()); TRY(parse_volume_set()); TRY(create_root_inode()); - return KSuccess; + return {}; } Inode& ISO9660FS::root_inode() @@ -211,7 +211,7 @@ unsigned ISO9660FS::total_inode_count() const if (!m_cached_inode_count) { auto result = calculate_inode_count(); if (result.is_error()) { - // FIXME: This should be able to return a KResult. + // FIXME: This should be able to return a ErrorOr. return 0; } } @@ -228,7 +228,7 @@ u8 ISO9660FS::internal_file_type_to_directory_entry_type(const DirectoryEntryVie return DT_REG; } -KResult ISO9660FS::parse_volume_set() +ErrorOr ISO9660FS::parse_volume_set() { VERIFY(!m_primary_volume); @@ -278,10 +278,10 @@ all_headers_read: } m_logical_block_size = LittleEndian { m_primary_volume->logical_block_size.little }; - return KSuccess; + return {}; } -KResult ISO9660FS::create_root_inode() +ErrorOr ISO9660FS::create_root_inode() { if (!m_primary_volume) { dbgln_if(ISO9660_DEBUG, "Primary volume doesn't exist, can't create root inode"); @@ -289,10 +289,10 @@ KResult ISO9660FS::create_root_inode() } m_root_inode = TRY(ISO9660Inode::try_create_from_directory_record(*this, m_primary_volume->root_directory_record_header, {})); - return KSuccess; + return {}; } -KResult ISO9660FS::calculate_inode_count() const +ErrorOr ISO9660FS::calculate_inode_count() const { if (!m_primary_volume) { dbgln_if(ISO9660_DEBUG, "Primary volume doesn't exist, can't calculate inode count"); @@ -323,13 +323,13 @@ KResult ISO9660FS::calculate_inode_count() const })); m_cached_inode_count = inode_count; - return KSuccess; + return {}; } -KResult ISO9660FS::visit_directory_record(ISO::DirectoryRecordHeader const& record, Function(ISO::DirectoryRecordHeader const*)> const& visitor) const +ErrorOr ISO9660FS::visit_directory_record(ISO::DirectoryRecordHeader const& record, Function(ISO::DirectoryRecordHeader const*)> const& visitor) const { if (!has_flag(record.file_flags, ISO::FileFlags::Directory)) { - return KSuccess; + return {}; } ISO9660DirectoryIterator iterator { const_cast(*this), record }; @@ -342,7 +342,7 @@ KResult ISO9660FS::visit_directory_record(ISO::DirectoryRecordHeader const& reco if (!has_moved) { // If next() hasn't moved then we have read through all the // directories, and can exit. - return KSuccess; + return {}; } continue; @@ -352,20 +352,20 @@ KResult ISO9660FS::visit_directory_record(ISO::DirectoryRecordHeader const& reco if (iterator.skip()) break; if (!iterator.go_up()) - return KSuccess; + return {}; } continue; } case RecursionDecision::Break: - return KSuccess; + return {}; } } - return KSuccess; + return {}; } -KResultOr> ISO9660FS::directory_entry_for_record(Badge, ISO::DirectoryRecordHeader const* record) +ErrorOr> ISO9660FS::directory_entry_for_record(Badge, ISO::DirectoryRecordHeader const* record) { u32 extent_location = LittleEndian { record->extent_location.little }; u32 data_length = LittleEndian { record->data_length.little }; @@ -407,7 +407,7 @@ u32 ISO9660FS::calculate_directory_entry_cache_key(ISO::DirectoryRecordHeader co return LittleEndian { record.extent_location.little }; } -KResultOr ISO9660Inode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const +ErrorOr ISO9660Inode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const { MutexLocker inode_locker(m_inode_lock); @@ -450,7 +450,7 @@ InodeMetadata ISO9660Inode::metadata() const return m_metadata; } -KResult ISO9660Inode::traverse_as_directory(Function visitor) const +ErrorOr ISO9660Inode::traverse_as_directory(Function visitor) const { Array file_identifier_buffer; @@ -469,7 +469,7 @@ KResult ISO9660Inode::traverse_as_directory(Function> ISO9660Inode::lookup(StringView name) +ErrorOr> ISO9660Inode::lookup(StringView name) { RefPtr inode; Array file_identifier_buffer; @@ -481,8 +481,8 @@ KResultOr> ISO9660Inode::lookup(StringView name) auto maybe_inode = ISO9660Inode::try_create_from_directory_record(fs(), *record, filename); if (maybe_inode.is_error()) { // FIXME: The Inode API does not handle allocation failures very - // well... we can't return a KResultOr from here. It - // would be nice if we could return a KResult(Or) from + // well... we can't return a ErrorOr from here. It + // would be nice if we could return a ErrorOr(Or) from // any place where allocation may happen. dbgln("Could not allocate inode for lookup!"); } else { @@ -499,57 +499,57 @@ KResultOr> ISO9660Inode::lookup(StringView name) return inode.release_nonnull(); } -KResult ISO9660Inode::flush_metadata() +ErrorOr ISO9660Inode::flush_metadata() { - return KSuccess; + return {}; } -KResultOr ISO9660Inode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) +ErrorOr ISO9660Inode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) { return EROFS; } -KResultOr> ISO9660Inode::create_child(StringView, mode_t, dev_t, UserID, GroupID) +ErrorOr> ISO9660Inode::create_child(StringView, mode_t, dev_t, UserID, GroupID) { return EROFS; } -KResult ISO9660Inode::add_child(Inode&, const StringView&, mode_t) +ErrorOr ISO9660Inode::add_child(Inode&, const StringView&, mode_t) { return EROFS; } -KResult ISO9660Inode::remove_child(const StringView&) +ErrorOr ISO9660Inode::remove_child(const StringView&) { return EROFS; } -KResult ISO9660Inode::chmod(mode_t) +ErrorOr ISO9660Inode::chmod(mode_t) { return EROFS; } -KResult ISO9660Inode::chown(UserID, GroupID) +ErrorOr ISO9660Inode::chown(UserID, GroupID) { return EROFS; } -KResult ISO9660Inode::truncate(u64) +ErrorOr ISO9660Inode::truncate(u64) { return EROFS; } -KResult ISO9660Inode::set_atime(time_t) +ErrorOr ISO9660Inode::set_atime(time_t) { return EROFS; } -KResult ISO9660Inode::set_ctime(time_t) +ErrorOr ISO9660Inode::set_ctime(time_t) { return EROFS; } -KResult ISO9660Inode::set_mtime(time_t) +ErrorOr ISO9660Inode::set_mtime(time_t) { return EROFS; } @@ -570,7 +570,7 @@ ISO9660Inode::~ISO9660Inode() { } -KResultOr> ISO9660Inode::try_create_from_directory_record(ISO9660FS& fs, ISO::DirectoryRecordHeader const& record, StringView const& name) +ErrorOr> ISO9660Inode::try_create_from_directory_record(ISO9660FS& fs, ISO::DirectoryRecordHeader const& record, StringView const& name) { return adopt_nonnull_ref_or_enomem(new (nothrow) ISO9660Inode(fs, record, name)); } diff --git a/Kernel/FileSystem/ISO9660FileSystem.h b/Kernel/FileSystem/ISO9660FileSystem.h index 5db938cdd8b..d5789661176 100644 --- a/Kernel/FileSystem/ISO9660FileSystem.h +++ b/Kernel/FileSystem/ISO9660FileSystem.h @@ -7,12 +7,12 @@ #pragma once #include +#include #include #include #include #include #include -#include #include #include #include @@ -291,7 +291,7 @@ public: // We need it as an OwnPtr to default-construct this struct. OwnPtr blocks; - static KResultOr> try_create(u32 extent, u32 length, OwnPtr blocks) + static ErrorOr> try_create(u32 extent, u32 length, OwnPtr blocks) { return adopt_nonnull_ref_or_enomem(new (nothrow) DirectoryEntry(extent, length, move(blocks))); } @@ -305,10 +305,10 @@ public: } }; - static KResultOr> try_create(OpenFileDescription&); + static ErrorOr> try_create(OpenFileDescription&); virtual ~ISO9660FS() override; - virtual KResult initialize() override; + virtual ErrorOr initialize() override; virtual StringView class_name() const override { return "ISO9660FS"sv; } virtual Inode& root_inode() override; @@ -317,18 +317,18 @@ public: virtual u8 internal_file_type_to_directory_entry_type(DirectoryEntryView const& entry) const override; - KResultOr> directory_entry_for_record(Badge, ISO::DirectoryRecordHeader const* record); + ErrorOr> directory_entry_for_record(Badge, ISO::DirectoryRecordHeader const* record); private: ISO9660FS(OpenFileDescription&); - KResult parse_volume_set(); - KResult create_root_inode(); - KResult calculate_inode_count() const; + ErrorOr parse_volume_set(); + ErrorOr create_root_inode(); + ErrorOr calculate_inode_count() const; u32 calculate_directory_entry_cache_key(ISO::DirectoryRecordHeader const&); - KResult visit_directory_record(ISO::DirectoryRecordHeader const& record, Function(ISO::DirectoryRecordHeader const*)> const& visitor) const; + ErrorOr visit_directory_record(ISO::DirectoryRecordHeader const& record, Function(ISO::DirectoryRecordHeader const*)> const& visitor) const; OwnPtr m_primary_volume; RefPtr m_root_inode; @@ -347,21 +347,21 @@ public: ISO9660FS const& fs() const { return static_cast(Inode::fs()); } // ^Inode - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; - virtual KResult flush_metadata() override; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual KResult add_child(Inode&, const StringView& name, mode_t) override; - virtual KResult remove_child(const StringView& name) override; - virtual KResult chmod(mode_t) override; - virtual KResult chown(UserID, GroupID) override; - virtual KResult truncate(u64) override; - virtual KResult set_atime(time_t) override; - virtual KResult set_ctime(time_t) override; - virtual KResult set_mtime(time_t) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; + virtual ErrorOr flush_metadata() override; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; + virtual ErrorOr add_child(Inode&, const StringView& name, mode_t) override; + virtual ErrorOr remove_child(const StringView& name) override; + virtual ErrorOr chmod(mode_t) override; + virtual ErrorOr chown(UserID, GroupID) override; + virtual ErrorOr truncate(u64) override; + virtual ErrorOr set_atime(time_t) override; + virtual ErrorOr set_ctime(time_t) override; + virtual ErrorOr set_mtime(time_t) override; virtual void one_ref_left() override; private: @@ -371,7 +371,7 @@ private: static constexpr size_t max_file_identifier_length = 256 - sizeof(ISO::DirectoryRecordHeader); ISO9660Inode(ISO9660FS&, ISO::DirectoryRecordHeader const& record, StringView const& name); - static KResultOr> try_create_from_directory_record(ISO9660FS&, ISO::DirectoryRecordHeader const& record, StringView const& name); + static ErrorOr> try_create_from_directory_record(ISO9660FS&, ISO::DirectoryRecordHeader const& record, StringView const& name); static InodeIndex get_inode_index(ISO::DirectoryRecordHeader const& record, StringView const& name); static StringView get_normalized_filename(ISO::DirectoryRecordHeader const& record, Bytes buffer); diff --git a/Kernel/FileSystem/Inode.cpp b/Kernel/FileSystem/Inode.cpp index dd53e593d9f..78a3d7db6bd 100644 --- a/Kernel/FileSystem/Inode.cpp +++ b/Kernel/FileSystem/Inode.cpp @@ -52,7 +52,7 @@ void Inode::sync() fs().flush_writes(); } -KResultOr> Inode::read_entire(OpenFileDescription* description) const +ErrorOr> Inode::read_entire(OpenFileDescription* description) const { auto builder = TRY(KBufferBuilder::try_create()); @@ -76,7 +76,7 @@ KResultOr> Inode::read_entire(OpenFileDescription* descri return entire_file.release_nonnull(); } -KResultOr> Inode::resolve_as_link(Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) const +ErrorOr> Inode::resolve_as_link(Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) const { // The default implementation simply treats the stored // contents as a path and resolves that. That is, it @@ -106,27 +106,27 @@ void Inode::will_be_destroyed() (void)flush_metadata(); } -KResult Inode::set_atime(time_t) +ErrorOr Inode::set_atime(time_t) { return ENOTIMPL; } -KResult Inode::set_ctime(time_t) +ErrorOr Inode::set_ctime(time_t) { return ENOTIMPL; } -KResult Inode::set_mtime(time_t) +ErrorOr Inode::set_mtime(time_t) { return ENOTIMPL; } -KResult Inode::increment_link_count() +ErrorOr Inode::increment_link_count() { return ENOTIMPL; } -KResult Inode::decrement_link_count() +ErrorOr Inode::decrement_link_count() { return ENOTIMPL; } @@ -169,7 +169,7 @@ void Inode::unregister_watcher(Badge, InodeWatcher& watcher) m_watchers.remove(&watcher); } -KResultOr> Inode::fifo() +ErrorOr> Inode::fifo() { MutexLocker locker(m_inode_lock); VERIFY(metadata().is_fifo()); @@ -178,7 +178,7 @@ KResultOr> Inode::fifo() if (!m_fifo) m_fifo = TRY(FIFO::try_create(metadata().uid)); - return *m_fifo; + return NonnullRefPtr { *m_fifo }; } void Inode::set_metadata_dirty(bool metadata_dirty) @@ -242,7 +242,7 @@ void Inode::did_delete_self() } } -KResult Inode::prepare_to_write_data() +ErrorOr Inode::prepare_to_write_data() { // FIXME: It's a poor design that filesystems are expected to call this before writing out data. // We should funnel everything through an interface at the VirtualFileSystem layer so this can happen from a single place. @@ -254,7 +254,7 @@ KResult Inode::prepare_to_write_data() dbgln("Inode::prepare_to_write_data(): Stripping SUID/SGID bits from {}", identifier()); return chmod(metadata.mode & ~(04000 | 02000)); } - return KSuccess; + return {}; } RefPtr Inode::shared_vmobject() const @@ -269,7 +269,7 @@ static inline bool range_overlap(T start1, T len1, T start2, T len2) return ((start1 < start2 + len2) || len2 == 0) && ((start2 < start1 + len1) || len1 == 0); } -static inline KResult normalize_flock(OpenFileDescription const& description, flock& lock) +static inline ErrorOr normalize_flock(OpenFileDescription const& description, flock& lock) { off_t start; switch (lock.l_whence) { @@ -286,10 +286,10 @@ static inline KResult normalize_flock(OpenFileDescription const& description, fl return EINVAL; } lock = { lock.l_type, SEEK_SET, start, lock.l_len, 0 }; - return KSuccess; + return {}; } -KResult Inode::can_apply_flock(OpenFileDescription const& description, flock const& new_lock) const +ErrorOr Inode::can_apply_flock(OpenFileDescription const& description, flock const& new_lock) const { VERIFY(new_lock.l_whence == SEEK_SET); @@ -298,7 +298,7 @@ KResult Inode::can_apply_flock(OpenFileDescription const& description, flock con if (new_lock.l_type == F_UNLCK) { for (auto& lock : m_flocks) { if (&description == lock.owner && lock.start == new_lock.l_start && lock.len == new_lock.l_len) - return KSuccess; + return {}; } return EINVAL; } @@ -313,10 +313,10 @@ KResult Inode::can_apply_flock(OpenFileDescription const& description, flock con if (new_lock.l_type == F_WRLCK) return EAGAIN; } - return KSuccess; + return {}; } -KResult Inode::apply_flock(Process const& process, OpenFileDescription const& description, Userspace input_lock) +ErrorOr Inode::apply_flock(Process const& process, OpenFileDescription const& description, Userspace input_lock) { flock new_lock = {}; TRY(copy_from_user(&new_lock, input_lock)); @@ -330,17 +330,17 @@ KResult Inode::apply_flock(Process const& process, OpenFileDescription const& de for (size_t i = 0; i < m_flocks.size(); ++i) { if (&description == m_flocks[i].owner && m_flocks[i].start == new_lock.l_start && m_flocks[i].len == new_lock.l_len) { m_flocks.remove(i); - return KSuccess; + return {}; } } return EINVAL; } m_flocks.append(Flock { new_lock.l_start, new_lock.l_len, &description, process.pid().value(), new_lock.l_type }); - return KSuccess; + return {}; } -KResult Inode::get_flock(OpenFileDescription const& description, Userspace reference_lock) const +ErrorOr Inode::get_flock(OpenFileDescription const& description, Userspace reference_lock) const { flock lookup = {}; TRY(copy_from_user(&lookup, reference_lock)); diff --git a/Kernel/FileSystem/Inode.h b/Kernel/FileSystem/Inode.h index eddb3030c1b..e0ede5b0925 100644 --- a/Kernel/FileSystem/Inode.h +++ b/Kernel/FileSystem/Inode.h @@ -7,12 +7,12 @@ #pragma once +#include #include #include #include #include #include -#include #include #include #include @@ -47,24 +47,24 @@ public: InodeIdentifier identifier() const { return { fsid(), index() }; } virtual InodeMetadata metadata() const = 0; - KResultOr> read_entire(OpenFileDescription* = nullptr) const; + ErrorOr> read_entire(OpenFileDescription* = nullptr) const; - virtual KResult attach(OpenFileDescription&) { return KSuccess; } + virtual ErrorOr attach(OpenFileDescription&) { return {}; } virtual void detach(OpenFileDescription&) { } virtual void did_seek(OpenFileDescription&, off_t) { } - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const = 0; - virtual KResult traverse_as_directory(Function) const = 0; - virtual KResultOr> lookup(StringView name) = 0; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& data, OpenFileDescription*) = 0; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) = 0; - virtual KResult add_child(Inode&, const StringView& name, mode_t) = 0; - virtual KResult remove_child(const StringView& name) = 0; - virtual KResult chmod(mode_t) = 0; - virtual KResult chown(UserID, GroupID) = 0; - virtual KResult truncate(u64) { return KSuccess; } - virtual KResultOr> resolve_as_link(Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) const; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const = 0; + virtual ErrorOr traverse_as_directory(Function) const = 0; + virtual ErrorOr> lookup(StringView name) = 0; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& data, OpenFileDescription*) = 0; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) = 0; + virtual ErrorOr add_child(Inode&, const StringView& name, mode_t) = 0; + virtual ErrorOr remove_child(const StringView& name) = 0; + virtual ErrorOr chmod(mode_t) = 0; + virtual ErrorOr chown(UserID, GroupID) = 0; + virtual ErrorOr truncate(u64) { return {}; } + virtual ErrorOr> resolve_as_link(Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) const; - virtual KResultOr get_block_address(int) { return ENOTSUP; } + virtual ErrorOr get_block_address(int) { return ENOTSUP; } LocalSocket* socket() { return m_socket.ptr(); } const LocalSocket* socket() const { return m_socket.ptr(); } @@ -75,13 +75,13 @@ public: bool is_metadata_dirty() const { return m_metadata_dirty; } - virtual KResult set_atime(time_t); - virtual KResult set_ctime(time_t); - virtual KResult set_mtime(time_t); - virtual KResult increment_link_count(); - virtual KResult decrement_link_count(); + virtual ErrorOr set_atime(time_t); + virtual ErrorOr set_ctime(time_t); + virtual ErrorOr set_mtime(time_t); + virtual ErrorOr increment_link_count(); + virtual ErrorOr decrement_link_count(); - virtual KResult flush_metadata() = 0; + virtual ErrorOr flush_metadata() = 0; void will_be_destroyed(); @@ -96,17 +96,17 @@ public: void register_watcher(Badge, InodeWatcher&); void unregister_watcher(Badge, InodeWatcher&); - KResultOr> fifo(); + ErrorOr> fifo(); - KResult can_apply_flock(OpenFileDescription const&, flock const&) const; - KResult apply_flock(Process const&, OpenFileDescription const&, Userspace); - KResult get_flock(OpenFileDescription const&, Userspace) const; + ErrorOr can_apply_flock(OpenFileDescription const&, flock const&) const; + ErrorOr apply_flock(Process const&, OpenFileDescription const&, Userspace); + ErrorOr get_flock(OpenFileDescription const&, Userspace) const; void remove_flocks_for_description(OpenFileDescription const&); protected: Inode(FileSystem&, InodeIndex); void set_metadata_dirty(bool); - KResult prepare_to_write_data(); + ErrorOr prepare_to_write_data(); void did_add_child(InodeIdentifier const& child_id, String const& name); void did_remove_child(InodeIdentifier const& child_id, String const& name); diff --git a/Kernel/FileSystem/InodeFile.cpp b/Kernel/FileSystem/InodeFile.cpp index 009de243c7c..f72f7ffd12d 100644 --- a/Kernel/FileSystem/InodeFile.cpp +++ b/Kernel/FileSystem/InodeFile.cpp @@ -26,7 +26,7 @@ InodeFile::~InodeFile() { } -KResultOr InodeFile::read(OpenFileDescription& description, u64 offset, UserOrKernelBuffer& buffer, size_t count) +ErrorOr InodeFile::read(OpenFileDescription& description, u64 offset, UserOrKernelBuffer& buffer, size_t count) { if (Checked::addition_would_overflow(offset, count)) return EOVERFLOW; @@ -39,7 +39,7 @@ KResultOr InodeFile::read(OpenFileDescription& description, u64 offset, return nread; } -KResultOr InodeFile::write(OpenFileDescription& description, u64 offset, const UserOrKernelBuffer& data, size_t count) +ErrorOr InodeFile::write(OpenFileDescription& description, u64 offset, const UserOrKernelBuffer& data, size_t count) { if (Checked::addition_would_overflow(offset, count)) return EOVERFLOW; @@ -50,12 +50,12 @@ KResultOr InodeFile::write(OpenFileDescription& description, u64 offset, Thread::current()->did_file_write(nwritten); evaluate_block_conditions(); if (mtime_result.is_error()) - return mtime_result; + return mtime_result.release_error(); } return nwritten; } -KResult InodeFile::ioctl(OpenFileDescription& description, unsigned request, Userspace arg) +ErrorOr InodeFile::ioctl(OpenFileDescription& description, unsigned request, Userspace arg) { switch (request) { case FIBMAP: { @@ -81,7 +81,7 @@ KResult InodeFile::ioctl(OpenFileDescription& description, unsigned request, Use } } -KResultOr InodeFile::mmap(Process& process, OpenFileDescription& description, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) +ErrorOr InodeFile::mmap(Process& process, OpenFileDescription& description, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) { // FIXME: If PROT_EXEC, check that the underlying file system isn't mounted noexec. RefPtr vmobject; @@ -93,33 +93,33 @@ KResultOr InodeFile::mmap(Process& process, OpenFileDescription return process.address_space().allocate_region_with_vmobject(range, vmobject.release_nonnull(), offset, path->view(), prot, shared); } -KResultOr> InodeFile::pseudo_path(const OpenFileDescription&) const +ErrorOr> InodeFile::pseudo_path(const OpenFileDescription&) const { // If it has an inode, then it has a path, and therefore the caller should have been able to get a custody at some point. VERIFY_NOT_REACHED(); } -KResult InodeFile::truncate(u64 size) +ErrorOr InodeFile::truncate(u64 size) { TRY(m_inode->truncate(size)); TRY(m_inode->set_mtime(kgettimeofday().to_truncated_seconds())); - return KSuccess; + return {}; } -KResult InodeFile::sync() +ErrorOr InodeFile::sync() { m_inode->sync(); - return KSuccess; + return {}; } -KResult InodeFile::chown(OpenFileDescription& description, UserID uid, GroupID gid) +ErrorOr InodeFile::chown(OpenFileDescription& description, UserID uid, GroupID gid) { VERIFY(description.inode() == m_inode); VERIFY(description.custody()); return VirtualFileSystem::the().chown(*description.custody(), uid, gid); } -KResult InodeFile::chmod(OpenFileDescription& description, mode_t mode) +ErrorOr InodeFile::chmod(OpenFileDescription& description, mode_t mode) { VERIFY(description.inode() == m_inode); VERIFY(description.custody()); diff --git a/Kernel/FileSystem/InodeFile.h b/Kernel/FileSystem/InodeFile.h index ec220bbf991..1701d41c065 100644 --- a/Kernel/FileSystem/InodeFile.h +++ b/Kernel/FileSystem/InodeFile.h @@ -14,7 +14,7 @@ class Inode; class InodeFile final : public File { public: - static KResultOr> create(NonnullRefPtr&& inode) + static ErrorOr> create(NonnullRefPtr&& inode) { auto file = adopt_ref_if_nonnull(new (nothrow) InodeFile(move(inode))); if (!file) @@ -30,18 +30,18 @@ public: virtual bool can_read(const OpenFileDescription&, size_t) const override { return true; } virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; - virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; - virtual KResultOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; - virtual KResult stat(::stat& buffer) const override { return inode().metadata().stat(buffer); } + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; + virtual ErrorOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; + virtual ErrorOr stat(::stat& buffer) const override { return inode().metadata().stat(buffer); } - virtual KResultOr> pseudo_path(const OpenFileDescription&) const override; + virtual ErrorOr> pseudo_path(const OpenFileDescription&) const override; - virtual KResult truncate(u64) override; - virtual KResult sync() override; - virtual KResult chown(OpenFileDescription&, UserID, GroupID) override; - virtual KResult chmod(OpenFileDescription&, mode_t) override; + virtual ErrorOr truncate(u64) override; + virtual ErrorOr sync() override; + virtual ErrorOr chown(OpenFileDescription&, UserID, GroupID) override; + virtual ErrorOr chmod(OpenFileDescription&, mode_t) override; virtual StringView class_name() const override { return "InodeFile"sv; } diff --git a/Kernel/FileSystem/InodeMetadata.h b/Kernel/FileSystem/InodeMetadata.h index ba61a762348..8c14562ae8d 100644 --- a/Kernel/FileSystem/InodeMetadata.h +++ b/Kernel/FileSystem/InodeMetadata.h @@ -6,8 +6,8 @@ #pragma once +#include #include -#include #include #include #include @@ -86,7 +86,7 @@ struct InodeMetadata { bool is_setuid() const { return Kernel::is_setuid(mode); } bool is_setgid() const { return Kernel::is_setgid(mode); } - KResult stat(stat& buffer) const + ErrorOr stat(stat& buffer) const { if (!is_valid()) return EIO; @@ -106,7 +106,7 @@ struct InodeMetadata { buffer.st_mtim.tv_nsec = 0; buffer.st_ctim.tv_sec = ctime; buffer.st_ctim.tv_nsec = 0; - return KSuccess; + return {}; } InodeIdentifier inode; diff --git a/Kernel/FileSystem/InodeWatcher.cpp b/Kernel/FileSystem/InodeWatcher.cpp index bd750c53651..69a9af80163 100644 --- a/Kernel/FileSystem/InodeWatcher.cpp +++ b/Kernel/FileSystem/InodeWatcher.cpp @@ -12,7 +12,7 @@ namespace Kernel { -KResultOr> InodeWatcher::try_create() +ErrorOr> InodeWatcher::try_create() { return adopt_nonnull_ref_or_enomem(new (nothrow) InodeWatcher); } @@ -28,7 +28,7 @@ bool InodeWatcher::can_read(const OpenFileDescription&, size_t) const return !m_queue.is_empty(); } -KResultOr InodeWatcher::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t buffer_size) +ErrorOr InodeWatcher::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t buffer_size) { MutexLocker locker(m_lock); if (m_queue.is_empty()) @@ -67,7 +67,7 @@ KResultOr InodeWatcher::read(OpenFileDescription&, u64, UserOrKernelBuff return result; } -KResult InodeWatcher::close() +ErrorOr InodeWatcher::close() { MutexLocker locker(m_lock); @@ -78,10 +78,10 @@ KResult InodeWatcher::close() m_wd_to_watches.clear(); m_inode_to_watches.clear(); - return KSuccess; + return {}; } -KResultOr> InodeWatcher::pseudo_path(const OpenFileDescription&) const +ErrorOr> InodeWatcher::pseudo_path(const OpenFileDescription&) const { return KString::try_create(String::formatted("InodeWatcher:({})", m_wd_to_watches.size())); } @@ -102,7 +102,7 @@ void InodeWatcher::notify_inode_event(Badge, InodeIdentifier inode_id, In evaluate_block_conditions(); } -KResultOr InodeWatcher::register_inode(Inode& inode, unsigned event_mask) +ErrorOr InodeWatcher::register_inode(Inode& inode, unsigned event_mask) { MutexLocker locker(m_lock); @@ -118,11 +118,8 @@ KResultOr InodeWatcher::register_inode(Inode& inode, unsigned event_mask) m_wd_counter = 1; } while (m_wd_to_watches.find(wd) != m_wd_to_watches.end()); - auto description_or_error = WatchDescription::create(wd, inode, event_mask); - if (description_or_error.is_error()) - return description_or_error.error(); + auto description = TRY(WatchDescription::create(wd, inode, event_mask)); - auto description = description_or_error.release_value(); m_inode_to_watches.set(inode.identifier(), description.ptr()); m_wd_to_watches.set(wd, move(description)); @@ -130,7 +127,7 @@ KResultOr InodeWatcher::register_inode(Inode& inode, unsigned event_mask) return wd; } -KResult InodeWatcher::unregister_by_wd(int wd) +ErrorOr InodeWatcher::unregister_by_wd(int wd) { MutexLocker locker(m_lock); @@ -144,7 +141,7 @@ KResult InodeWatcher::unregister_by_wd(int wd) m_inode_to_watches.remove(inode.identifier()); m_wd_to_watches.remove(it); - return KSuccess; + return {}; } void InodeWatcher::unregister_by_inode(Badge, InodeIdentifier identifier) diff --git a/Kernel/FileSystem/InodeWatcher.h b/Kernel/FileSystem/InodeWatcher.h index 99840da740d..9c3bd0a8099 100644 --- a/Kernel/FileSystem/InodeWatcher.h +++ b/Kernel/FileSystem/InodeWatcher.h @@ -24,7 +24,7 @@ struct WatchDescription { Inode& inode; unsigned event_mask; - static KResultOr> create(int wd, Inode& inode, unsigned event_mask) + static ErrorOr> create(int wd, Inode& inode, unsigned event_mask) { return adopt_nonnull_own_or_enomem(new (nothrow) WatchDescription(wd, inode, event_mask)); } @@ -40,24 +40,24 @@ private: class InodeWatcher final : public File { public: - static KResultOr> try_create(); + static ErrorOr> try_create(); virtual ~InodeWatcher() override; virtual bool can_read(const OpenFileDescription&, size_t) const override; - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; // Can't write to an inode watcher. virtual bool can_write(const OpenFileDescription&, size_t) const override { return true; } - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EIO; } - virtual KResult close() override; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EIO; } + virtual ErrorOr close() override; - virtual KResultOr> pseudo_path(const OpenFileDescription&) const override; + virtual ErrorOr> pseudo_path(const OpenFileDescription&) const override; virtual StringView class_name() const override { return "InodeWatcher"sv; }; virtual bool is_inode_watcher() const override { return true; } void notify_inode_event(Badge, InodeIdentifier, InodeWatcherEvent::Type, String const& name = {}); - KResultOr register_inode(Inode&, unsigned event_mask); - KResult unregister_by_wd(int); + ErrorOr register_inode(Inode&, unsigned event_mask); + ErrorOr unregister_by_wd(int); void unregister_by_inode(Badge, InodeIdentifier); private: diff --git a/Kernel/FileSystem/OpenFileDescription.cpp b/Kernel/FileSystem/OpenFileDescription.cpp index 327d0d37e09..a408e085e52 100644 --- a/Kernel/FileSystem/OpenFileDescription.cpp +++ b/Kernel/FileSystem/OpenFileDescription.cpp @@ -24,7 +24,7 @@ namespace Kernel { -KResultOr> OpenFileDescription::try_create(Custody& custody) +ErrorOr> OpenFileDescription::try_create(Custody& custody) { auto inode_file = TRY(InodeFile::create(custody.inode())); auto description = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) OpenFileDescription(move(inode_file)))); @@ -34,7 +34,7 @@ KResultOr> OpenFileDescription::try_create(Cu return description; } -KResultOr> OpenFileDescription::try_create(File& file) +ErrorOr> OpenFileDescription::try_create(File& file) { auto description = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) OpenFileDescription(file))); TRY(description->attach()); @@ -64,7 +64,7 @@ OpenFileDescription::~OpenFileDescription() m_inode->remove_flocks_for_description(*this); } -KResult OpenFileDescription::attach() +ErrorOr OpenFileDescription::attach() { if (m_inode) TRY(m_inode->attach(*this)); @@ -97,7 +97,7 @@ Thread::FileBlocker::BlockFlags OpenFileDescription::should_unblock(Thread::File return unblock_flags; } -KResult OpenFileDescription::stat(::stat& buffer) +ErrorOr OpenFileDescription::stat(::stat& buffer) { MutexLocker locker(m_lock); // FIXME: This is due to the Device class not overriding File::stat(). @@ -106,7 +106,7 @@ KResult OpenFileDescription::stat(::stat& buffer) return m_file->stat(buffer); } -KResultOr OpenFileDescription::seek(off_t offset, int whence) +ErrorOr OpenFileDescription::seek(off_t offset, int whence) { MutexLocker locker(m_lock); if (!m_file->is_seekable()) @@ -147,21 +147,21 @@ KResultOr OpenFileDescription::seek(off_t offset, int whence) return m_current_offset; } -KResultOr OpenFileDescription::read(UserOrKernelBuffer& buffer, u64 offset, size_t count) +ErrorOr OpenFileDescription::read(UserOrKernelBuffer& buffer, u64 offset, size_t count) { if (Checked::addition_would_overflow(offset, count)) return EOVERFLOW; return m_file->read(*this, offset, buffer, count); } -KResultOr OpenFileDescription::write(u64 offset, UserOrKernelBuffer const& data, size_t data_size) +ErrorOr OpenFileDescription::write(u64 offset, UserOrKernelBuffer const& data, size_t data_size) { if (Checked::addition_would_overflow(offset, data_size)) return EOVERFLOW; return m_file->write(*this, offset, data, data_size); } -KResultOr OpenFileDescription::read(UserOrKernelBuffer& buffer, size_t count) +ErrorOr OpenFileDescription::read(UserOrKernelBuffer& buffer, size_t count) { MutexLocker locker(m_lock); if (Checked::addition_would_overflow(m_current_offset, count)) @@ -173,7 +173,7 @@ KResultOr OpenFileDescription::read(UserOrKernelBuffer& buffer, size_t c return nread; } -KResultOr OpenFileDescription::write(const UserOrKernelBuffer& data, size_t size) +ErrorOr OpenFileDescription::write(const UserOrKernelBuffer& data, size_t size) { MutexLocker locker(m_lock); if (Checked::addition_would_overflow(m_current_offset, size)) @@ -195,7 +195,7 @@ bool OpenFileDescription::can_read() const return m_file->can_read(*this, offset()); } -KResultOr> OpenFileDescription::read_entire_file() +ErrorOr> OpenFileDescription::read_entire_file() { // HACK ALERT: (This entire function) VERIFY(m_file->is_inode()); @@ -203,7 +203,7 @@ KResultOr> OpenFileDescription::read_entire_file() return m_inode->read_entire(this); } -KResultOr OpenFileDescription::get_dir_entries(UserOrKernelBuffer& output_buffer, size_t size) +ErrorOr OpenFileDescription::get_dir_entries(UserOrKernelBuffer& output_buffer, size_t size) { MutexLocker locker(m_lock, Mutex::Mode::Shared); if (!is_directory()) @@ -214,7 +214,7 @@ KResultOr OpenFileDescription::get_dir_entries(UserOrKernelBuffer& outpu return EIO; size_t remaining = size; - KResult error = KSuccess; + ErrorOr error; u8 stack_buffer[PAGE_SIZE]; Bytes temp_buffer(stack_buffer, sizeof(stack_buffer)); OutputMemoryStream stream { temp_buffer }; @@ -238,7 +238,7 @@ KResultOr OpenFileDescription::get_dir_entries(UserOrKernelBuffer& outpu return true; }; - KResult result = VirtualFileSystem::the().traverse_directory_inode(*m_inode, [&flush_stream_to_output_buffer, &stream, this](auto& entry) { + ErrorOr result = VirtualFileSystem::the().traverse_directory_inode(*m_inode, [&flush_stream_to_output_buffer, &stream, this](auto& entry) { size_t serialized_size = sizeof(ino_t) + sizeof(u8) + sizeof(size_t) + sizeof(char) * entry.name.length(); if (serialized_size > stream.remaining()) { if (!flush_stream_to_output_buffer()) { @@ -257,12 +257,12 @@ KResultOr OpenFileDescription::get_dir_entries(UserOrKernelBuffer& outpu // We should only return EFAULT when the userspace buffer is too small, // so that userspace can reliably use it as a signal to increase its // buffer size. - VERIFY(result != EFAULT); - return result; + VERIFY(result.error().code() != EFAULT); + return result.release_error(); } if (error.is_error()) - return error; + return error.release_error(); return size - remaining; } @@ -342,21 +342,21 @@ MasterPTY* OpenFileDescription::master_pty() return static_cast(m_file.ptr()); } -KResult OpenFileDescription::close() +ErrorOr OpenFileDescription::close() { if (m_file->attach_count() > 0) - return KSuccess; + return {}; return m_file->close(); } -KResultOr> OpenFileDescription::original_absolute_path() const +ErrorOr> OpenFileDescription::original_absolute_path() const { if (!m_custody) return ENOENT; return m_custody->try_serialize_absolute_path(); } -KResultOr> OpenFileDescription::pseudo_path() const +ErrorOr> OpenFileDescription::pseudo_path() const { if (m_custody) return m_custody->try_serialize_absolute_path(); @@ -370,19 +370,19 @@ InodeMetadata OpenFileDescription::metadata() const return {}; } -KResultOr OpenFileDescription::mmap(Process& process, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) +ErrorOr OpenFileDescription::mmap(Process& process, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) { MutexLocker locker(m_lock); return m_file->mmap(process, *this, range, offset, prot, shared); } -KResult OpenFileDescription::truncate(u64 length) +ErrorOr OpenFileDescription::truncate(u64 length) { MutexLocker locker(m_lock); return m_file->truncate(length); } -KResult OpenFileDescription::sync() +ErrorOr OpenFileDescription::sync() { MutexLocker locker(m_lock); return m_file->sync(); @@ -428,13 +428,13 @@ void OpenFileDescription::set_file_flags(u32 flags) m_file_flags = flags; } -KResult OpenFileDescription::chmod(mode_t mode) +ErrorOr OpenFileDescription::chmod(mode_t mode) { MutexLocker locker(m_lock); return m_file->chmod(*this, mode); } -KResult OpenFileDescription::chown(UserID uid, GroupID gid) +ErrorOr OpenFileDescription::chown(UserID uid, GroupID gid) { MutexLocker locker(m_lock); return m_file->chown(*this, uid, gid); @@ -445,7 +445,7 @@ FileBlockerSet& OpenFileDescription::blocker_set() return m_file->blocker_set(); } -KResult OpenFileDescription::apply_flock(Process const& process, Userspace lock) +ErrorOr OpenFileDescription::apply_flock(Process const& process, Userspace lock) { if (!m_inode) return EBADF; @@ -453,7 +453,7 @@ KResult OpenFileDescription::apply_flock(Process const& process, Userspaceapply_flock(process, *this, lock); } -KResult OpenFileDescription::get_flock(Userspace lock) const +ErrorOr OpenFileDescription::get_flock(Userspace lock) const { if (!m_inode) return EBADF; diff --git a/Kernel/FileSystem/OpenFileDescription.h b/Kernel/FileSystem/OpenFileDescription.h index 72ec1534397..47247d50a0a 100644 --- a/Kernel/FileSystem/OpenFileDescription.h +++ b/Kernel/FileSystem/OpenFileDescription.h @@ -26,8 +26,8 @@ public: class OpenFileDescription : public RefCounted { MAKE_SLAB_ALLOCATED(OpenFileDescription) public: - static KResultOr> try_create(Custody&); - static KResultOr> try_create(File&); + static ErrorOr> try_create(Custody&); + static ErrorOr> try_create(File&); ~OpenFileDescription(); Thread::FileBlocker::BlockFlags should_unblock(Thread::FileBlocker::BlockFlags) const; @@ -44,28 +44,28 @@ public: set_writable(options & O_WRONLY); } - KResult close(); + ErrorOr close(); - KResultOr seek(off_t, int whence); - KResultOr read(UserOrKernelBuffer&, size_t); - KResultOr write(const UserOrKernelBuffer& data, size_t); - KResult stat(::stat&); + ErrorOr seek(off_t, int whence); + ErrorOr read(UserOrKernelBuffer&, size_t); + ErrorOr write(const UserOrKernelBuffer& data, size_t); + ErrorOr stat(::stat&); // NOTE: These ignore the current offset of this file description. - KResultOr read(UserOrKernelBuffer&, u64 offset, size_t); - KResultOr write(u64 offset, UserOrKernelBuffer const&, size_t); + ErrorOr read(UserOrKernelBuffer&, u64 offset, size_t); + ErrorOr write(u64 offset, UserOrKernelBuffer const&, size_t); - KResult chmod(mode_t); + ErrorOr chmod(mode_t); bool can_read() const; bool can_write() const; - KResultOr get_dir_entries(UserOrKernelBuffer& buffer, size_t); + ErrorOr get_dir_entries(UserOrKernelBuffer& buffer, size_t); - KResultOr> read_entire_file(); + ErrorOr> read_entire_file(); - KResultOr> original_absolute_path() const; - KResultOr> pseudo_path() const; + ErrorOr> original_absolute_path() const; + ErrorOr> pseudo_path() const; bool is_direct() const { return m_direct; } @@ -97,7 +97,7 @@ public: Custody* custody() { return m_custody.ptr(); } const Custody* custody() const { return m_custody.ptr(); } - KResultOr mmap(Process&, Memory::VirtualRange const&, u64 offset, int prot, bool shared); + ErrorOr mmap(Process&, Memory::VirtualRange const&, u64 offset, int prot, bool shared); bool is_blocking() const { return m_is_blocking; } void set_blocking(bool b) { m_is_blocking = b; } @@ -121,23 +121,23 @@ public: void set_original_inode(Badge, NonnullRefPtr&& inode) { m_inode = move(inode); } void set_original_custody(Badge, Custody& custody); - KResult truncate(u64); - KResult sync(); + ErrorOr truncate(u64); + ErrorOr sync(); off_t offset() const { return m_current_offset; } - KResult chown(UserID, GroupID); + ErrorOr chown(UserID, GroupID); FileBlockerSet& blocker_set(); - KResult apply_flock(Process const&, Userspace); - KResult get_flock(Userspace) const; + ErrorOr apply_flock(Process const&, Userspace); + ErrorOr get_flock(Userspace) const; private: friend class VirtualFileSystem; explicit OpenFileDescription(File&); - KResult attach(); + ErrorOr attach(); void evaluate_block_conditions() { diff --git a/Kernel/FileSystem/Plan9FileSystem.cpp b/Kernel/FileSystem/Plan9FileSystem.cpp index 3ad1158577e..95439a93569 100644 --- a/Kernel/FileSystem/Plan9FileSystem.cpp +++ b/Kernel/FileSystem/Plan9FileSystem.cpp @@ -9,7 +9,7 @@ namespace Kernel { -KResultOr> Plan9FS::try_create(OpenFileDescription& file_description) +ErrorOr> Plan9FS::try_create(OpenFileDescription& file_description) { return adopt_nonnull_ref_or_enomem(new (nothrow) Plan9FS(file_description)); } @@ -196,7 +196,7 @@ private: bool m_have_been_built { false }; }; -KResult Plan9FS::initialize() +ErrorOr Plan9FS::initialize() { ensure_thread(); @@ -225,7 +225,7 @@ KResult Plan9FS::initialize() TRY(post_message_and_wait_for_a_reply(attach_message)); m_root_inode = TRY(Plan9FSInode::try_create(*this, root_fid)); - return KSuccess; + return {}; } Plan9FS::ProtocolVersion Plan9FS::parse_protocol_version(const StringView& s) const @@ -489,7 +489,7 @@ bool Plan9FS::is_complete(const ReceiveCompletion& completion) return true; } -KResult Plan9FS::post_message(Message& message, RefPtr completion) +ErrorOr Plan9FS::post_message(Message& message, RefPtr completion) { auto& buffer = message.build(); const u8* data = buffer.data(); @@ -521,10 +521,10 @@ KResult Plan9FS::post_message(Message& message, RefPtr comple size -= nwritten; } - return KSuccess; + return {}; } -KResult Plan9FS::do_read(u8* data, size_t size) +ErrorOr Plan9FS::do_read(u8* data, size_t size) { auto& description = file_description(); while (size > 0) { @@ -540,10 +540,10 @@ KResult Plan9FS::do_read(u8* data, size_t size) data += nread; size -= nread; } - return KSuccess; + return {}; } -KResult Plan9FS::read_and_dispatch_one_message() +ErrorOr Plan9FS::read_and_dispatch_one_message() { struct [[gnu::packed]] Header { u32 size; @@ -564,7 +564,7 @@ KResult Plan9FS::read_and_dispatch_one_message() if (optional_completion.has_value()) { auto completion = optional_completion.value(); SpinlockLocker lock(completion->lock); - completion->result = KSuccess; + completion->result = {}; completion->message = adopt_own_if_nonnull(new (nothrow) Message { move(buffer) }); completion->completed = true; @@ -574,15 +574,15 @@ KResult Plan9FS::read_and_dispatch_one_message() dbgln("Received a 9p message of type {} with an unexpected tag {}, dropping", header.type, header.tag); } - return KSuccess; + return {}; } -KResult Plan9FS::post_message_and_explicitly_ignore_reply(Message& message) +ErrorOr Plan9FS::post_message_and_explicitly_ignore_reply(Message& message) { return post_message(message, {}); } -KResult Plan9FS::post_message_and_wait_for_a_reply(Message& message) +ErrorOr Plan9FS::post_message_and_wait_for_a_reply(Message& message) { auto request_type = message.type(); auto tag = message.tag(); @@ -602,7 +602,7 @@ KResult Plan9FS::post_message_and_wait_for_a_reply(Message& message) // Contains a numerical Linux errno; hopefully our errno numbers match. u32 error_code; message >> error_code; - return KResult((ErrnoCode)error_code); + return Error::from_errno((ErrnoCode)error_code); } else if (reply_type == Message::Type::Rerror) { // Contains an error message. We could attempt to parse it, but for now // we simply return EIO instead. In 9P200.u, it can also contain a @@ -617,7 +617,7 @@ KResult Plan9FS::post_message_and_wait_for_a_reply(Message& message) dbgln("Plan9FS: Received unexpected message type {} in response to {}", (u8)reply_type, (u8)request_type); return EIO; } else { - return KSuccess; + return {}; } } @@ -668,7 +668,7 @@ Plan9FSInode::Plan9FSInode(Plan9FS& fs, u32 fid) { } -KResultOr> Plan9FSInode::try_create(Plan9FS& fs, u32 fid) +ErrorOr> Plan9FSInode::try_create(Plan9FS& fs, u32 fid) { return adopt_nonnull_ref_or_enomem(new (nothrow) Plan9FSInode(fs, fid)); } @@ -681,7 +681,7 @@ Plan9FSInode::~Plan9FSInode() [[maybe_unused]] auto rc = fs().post_message_and_explicitly_ignore_reply(clunk_request); } -KResult Plan9FSInode::ensure_open_for_mode(int mode) +ErrorOr Plan9FSInode::ensure_open_for_mode(int mode) { bool use_lopen = fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L; u32 l_mode = 0; @@ -692,7 +692,7 @@ KResult Plan9FSInode::ensure_open_for_mode(int mode) // If it's already open in this mode, we're done. if ((m_open_mode & mode) == mode) - return KSuccess; + return {}; m_open_mode |= mode; @@ -718,7 +718,7 @@ KResult Plan9FSInode::ensure_open_for_mode(int mode) } } -KResultOr Plan9FSInode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const +ErrorOr Plan9FSInode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const { TRY(const_cast(*this).ensure_open_for_mode(O_RDONLY)); @@ -731,7 +731,7 @@ KResultOr Plan9FSInode::read_bytes(off_t offset, size_t size, UserOrKern bool readlink_succeded = false; if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L && offset == 0) { message << fid(); - if (auto result = fs().post_message_and_wait_for_a_reply(message); result.is_success()) { + if (auto result = fs().post_message_and_wait_for_a_reply(message); !result.is_error()) { readlink_succeded = true; message >> data; } @@ -750,7 +750,7 @@ KResultOr Plan9FSInode::read_bytes(off_t offset, size_t size, UserOrKern return nread; } -KResultOr Plan9FSInode::write_bytes(off_t offset, size_t size, const UserOrKernelBuffer& data, OpenFileDescription*) +ErrorOr Plan9FSInode::write_bytes(off_t offset, size_t size, const UserOrKernelBuffer& data, OpenFileDescription*) { TRY(ensure_open_for_mode(O_WRONLY)); size = fs().adjust_buffer_size(size); @@ -822,13 +822,13 @@ InodeMetadata Plan9FSInode::metadata() const return metadata; } -KResult Plan9FSInode::flush_metadata() +ErrorOr Plan9FSInode::flush_metadata() { // Do nothing. - return KSuccess; + return {}; } -KResult Plan9FSInode::traverse_as_directory(Function callback) const +ErrorOr Plan9FSInode::traverse_as_directory(Function callback) const { // TODO: Should we synthesize "." and ".." here? @@ -853,7 +853,7 @@ KResult Plan9FSInode::traverse_as_directory(Function result; while (true) { Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treaddir }; @@ -888,7 +888,7 @@ KResult Plan9FSInode::traverse_as_directory(Function> Plan9FSInode::lookup(StringView name) +ErrorOr> Plan9FSInode::lookup(StringView name) { u32 newfid = fs().allocate_fid(); Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twalk }; @@ -897,37 +897,37 @@ KResultOr> Plan9FSInode::lookup(StringView name) return TRY(Plan9FSInode::try_create(fs(), newfid)); } -KResultOr> Plan9FSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) +ErrorOr> Plan9FSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) { // TODO return ENOTIMPL; } -KResult Plan9FSInode::add_child(Inode&, const StringView&, mode_t) +ErrorOr Plan9FSInode::add_child(Inode&, const StringView&, mode_t) { // TODO return ENOTIMPL; } -KResult Plan9FSInode::remove_child(const StringView&) +ErrorOr Plan9FSInode::remove_child(const StringView&) { // TODO return ENOTIMPL; } -KResult Plan9FSInode::chmod(mode_t) +ErrorOr Plan9FSInode::chmod(mode_t) { // TODO return ENOTIMPL; } -KResult Plan9FSInode::chown(UserID, GroupID) +ErrorOr Plan9FSInode::chown(UserID, GroupID) { // TODO return ENOTIMPL; } -KResult Plan9FSInode::truncate(u64 new_size) +ErrorOr Plan9FSInode::truncate(u64 new_size) { if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) { Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tsetattr }; @@ -943,7 +943,7 @@ KResult Plan9FSInode::truncate(u64 new_size) return fs().post_message_and_wait_for_a_reply(message); } else { // TODO: wstat version - return KSuccess; + return {}; } } diff --git a/Kernel/FileSystem/Plan9FileSystem.h b/Kernel/FileSystem/Plan9FileSystem.h index 835432b44e2..bf21e3f672f 100644 --- a/Kernel/FileSystem/Plan9FileSystem.h +++ b/Kernel/FileSystem/Plan9FileSystem.h @@ -20,9 +20,9 @@ class Plan9FS final : public FileBackedFileSystem { public: virtual ~Plan9FS() override; - static KResultOr> try_create(OpenFileDescription&); + static ErrorOr> try_create(OpenFileDescription&); - virtual KResult initialize() override; + virtual ErrorOr initialize() override; virtual bool supports_watchers() const override { return false; } @@ -74,7 +74,7 @@ private: bool completed { false }; const u16 tag; OwnPtr message; - KResult result { KSuccess }; + ErrorOr result; ReceiveCompletion(u16 tag); ~ReceiveCompletion(); @@ -116,11 +116,11 @@ private: virtual StringView class_name() const override { return "Plan9FS"sv; } bool is_complete(const ReceiveCompletion&); - KResult post_message(Message&, RefPtr); - KResult do_read(u8* buffer, size_t); - KResult read_and_dispatch_one_message(); - KResult post_message_and_wait_for_a_reply(Message&); - KResult post_message_and_explicitly_ignore_reply(Message&); + ErrorOr post_message(Message&, RefPtr); + ErrorOr do_read(u8* buffer, size_t); + ErrorOr read_and_dispatch_one_message(); + ErrorOr post_message_and_wait_for_a_reply(Message&); + ErrorOr post_message_and_explicitly_ignore_reply(Message&); ProtocolVersion parse_protocol_version(const StringView&) const; size_t adjust_buffer_size(size_t size) const; @@ -155,21 +155,21 @@ public: // ^Inode virtual InodeMetadata metadata() const override; - virtual KResult flush_metadata() override; - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& data, OpenFileDescription*) override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual KResult add_child(Inode&, const StringView& name, mode_t) override; - virtual KResult remove_child(const StringView& name) override; - virtual KResult chmod(mode_t) override; - virtual KResult chown(UserID, GroupID) override; - virtual KResult truncate(u64) override; + virtual ErrorOr flush_metadata() override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& data, OpenFileDescription*) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; + virtual ErrorOr add_child(Inode&, const StringView& name, mode_t) override; + virtual ErrorOr remove_child(const StringView& name) override; + virtual ErrorOr chmod(mode_t) override; + virtual ErrorOr chown(UserID, GroupID) override; + virtual ErrorOr truncate(u64) override; private: Plan9FSInode(Plan9FS&, u32 fid); - static KResultOr> try_create(Plan9FS&, u32 fid); + static ErrorOr> try_create(Plan9FS&, u32 fid); enum class GetAttrMask : u64 { Mode = 0x1, @@ -206,7 +206,7 @@ private: // Mode in which the file is already open, using SerenityOS constants. int m_open_mode { 0 }; - KResult ensure_open_for_mode(int mode); + ErrorOr ensure_open_for_mode(int mode); Plan9FS& fs() { return reinterpret_cast(Inode::fs()); } Plan9FS& fs() const diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 62c101b3fa6..675541e4b8e 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -37,7 +37,7 @@ UNMAP_AFTER_INIT ProcFSComponentRegistry::ProcFSComponentRegistry() { } -KResultOr> ProcFS::try_create() +ErrorOr> ProcFS::try_create() { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFS()); } @@ -50,10 +50,10 @@ ProcFS::~ProcFS() { } -KResult ProcFS::initialize() +ErrorOr ProcFS::initialize() { m_root_inode = static_ptr_cast(TRY(ProcFSComponentRegistry::the().root_directory().to_inode(*this))); - return KSuccess; + return {}; } Inode& ProcFS::root_inode() @@ -70,37 +70,37 @@ ProcFSInode::~ProcFSInode() { } -KResult ProcFSInode::flush_metadata() +ErrorOr ProcFSInode::flush_metadata() { - return KSuccess; + return {}; } -KResult ProcFSInode::add_child(Inode&, const StringView&, mode_t) +ErrorOr ProcFSInode::add_child(Inode&, const StringView&, mode_t) { return EROFS; } -KResultOr> ProcFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) +ErrorOr> ProcFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) { return EROFS; } -KResult ProcFSInode::remove_child(const StringView&) +ErrorOr ProcFSInode::remove_child(const StringView&) { return EROFS; } -KResult ProcFSInode::chmod(mode_t) +ErrorOr ProcFSInode::chmod(mode_t) { return EPERM; } -KResult ProcFSInode::chown(UserID, GroupID) +ErrorOr ProcFSInode::chown(UserID, GroupID) { return EPERM; } -KResultOr> ProcFSGlobalInode::try_create(const ProcFS& fs, const ProcFSExposedComponent& component) +ErrorOr> ProcFSGlobalInode::try_create(const ProcFS& fs, const ProcFSExposedComponent& component) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSGlobalInode(fs, component)); } @@ -122,12 +122,12 @@ void ProcFSGlobalInode::did_seek(OpenFileDescription& description, off_t new_off } } -KResult ProcFSGlobalInode::attach(OpenFileDescription& description) +ErrorOr ProcFSGlobalInode::attach(OpenFileDescription& description) { return m_associated_component->refresh_data(description); } -KResultOr ProcFSGlobalInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* fd) const +ErrorOr ProcFSGlobalInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* fd) const { return m_associated_component->read_bytes(offset, count, buffer, fd); } @@ -137,22 +137,22 @@ StringView ProcFSGlobalInode::name() const return m_associated_component->name(); } -KResult ProcFSGlobalInode::traverse_as_directory(Function) const +ErrorOr ProcFSGlobalInode::traverse_as_directory(Function) const { VERIFY_NOT_REACHED(); } -KResultOr> ProcFSGlobalInode::lookup(StringView) +ErrorOr> ProcFSGlobalInode::lookup(StringView) { VERIFY_NOT_REACHED(); } -KResult ProcFSGlobalInode::truncate(u64 size) +ErrorOr ProcFSGlobalInode::truncate(u64 size) { return m_associated_component->truncate(size); } -KResult ProcFSGlobalInode::set_mtime(time_t time) +ErrorOr ProcFSGlobalInode::set_mtime(time_t time) { return m_associated_component->set_mtime(time); } @@ -170,12 +170,12 @@ InodeMetadata ProcFSGlobalInode::metadata() const return metadata; } -KResultOr ProcFSGlobalInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& buffer, OpenFileDescription* fd) +ErrorOr ProcFSGlobalInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& buffer, OpenFileDescription* fd) { return m_associated_component->write_bytes(offset, count, buffer, fd); } -KResultOr> ProcFSDirectoryInode::try_create(const ProcFS& procfs, const ProcFSExposedComponent& component) +ErrorOr> ProcFSDirectoryInode::try_create(const ProcFS& procfs, const ProcFSExposedComponent& component) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSDirectoryInode(procfs, component)); } @@ -200,20 +200,20 @@ InodeMetadata ProcFSDirectoryInode::metadata() const metadata.mtime = m_associated_component->modified_time(); return metadata; } -KResult ProcFSDirectoryInode::traverse_as_directory(Function callback) const +ErrorOr ProcFSDirectoryInode::traverse_as_directory(Function callback) const { MutexLocker locker(procfs().m_lock); return m_associated_component->traverse_as_directory(procfs().fsid(), move(callback)); } -KResultOr> ProcFSDirectoryInode::lookup(StringView name) +ErrorOr> ProcFSDirectoryInode::lookup(StringView name) { MutexLocker locker(procfs().m_lock); auto component = TRY(m_associated_component->lookup(name)); return component->to_inode(procfs()); } -KResultOr> ProcFSLinkInode::try_create(const ProcFS& procfs, const ProcFSExposedComponent& component) +ErrorOr> ProcFSLinkInode::try_create(const ProcFS& procfs, const ProcFSExposedComponent& component) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSLinkInode(procfs, component)); } @@ -242,12 +242,12 @@ ProcFSProcessAssociatedInode::ProcFSProcessAssociatedInode(const ProcFS& fs, Pro { } -KResultOr ProcFSProcessAssociatedInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) +ErrorOr ProcFSProcessAssociatedInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) { return ENOTSUP; } -KResultOr> ProcFSProcessDirectoryInode::try_create(const ProcFS& procfs, ProcessID pid) +ErrorOr> ProcFSProcessDirectoryInode::try_create(const ProcFS& procfs, ProcessID pid) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessDirectoryInode(procfs, pid)); } @@ -257,9 +257,9 @@ ProcFSProcessDirectoryInode::ProcFSProcessDirectoryInode(const ProcFS& procfs, P { } -KResult ProcFSProcessDirectoryInode::attach(OpenFileDescription&) +ErrorOr ProcFSProcessDirectoryInode::attach(OpenFileDescription&) { - return KSuccess; + return {}; } InodeMetadata ProcFSProcessDirectoryInode::metadata() const @@ -280,12 +280,12 @@ InodeMetadata ProcFSProcessDirectoryInode::metadata() const return metadata; } -KResultOr ProcFSProcessDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const +ErrorOr ProcFSProcessDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { VERIFY_NOT_REACHED(); } -KResult ProcFSProcessDirectoryInode::traverse_as_directory(Function callback) const +ErrorOr ProcFSProcessDirectoryInode::traverse_as_directory(Function callback) const { MutexLocker locker(procfs().m_lock); auto process = Process::from_pid(associated_pid()); @@ -294,7 +294,7 @@ KResult ProcFSProcessDirectoryInode::traverse_as_directory(Functionprocfs_traits()->traverse_as_directory(procfs().fsid(), move(callback)); } -KResultOr> ProcFSProcessDirectoryInode::lookup(StringView name) +ErrorOr> ProcFSProcessDirectoryInode::lookup(StringView name) { MutexLocker locker(procfs().m_lock); auto process = Process::from_pid(associated_pid()); @@ -321,7 +321,7 @@ KResultOr> ProcFSProcessDirectoryInode::lookup(StringView n return ENOENT; } -KResultOr> ProcFSProcessSubDirectoryInode::try_create(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid) +ErrorOr> ProcFSProcessSubDirectoryInode::try_create(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessSubDirectoryInode(procfs, sub_directory_type, pid)); } @@ -332,14 +332,14 @@ ProcFSProcessSubDirectoryInode::ProcFSProcessSubDirectoryInode(const ProcFS& pro { } -KResultOr ProcFSProcessSubDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const +ErrorOr ProcFSProcessSubDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { VERIFY_NOT_REACHED(); } -KResult ProcFSProcessSubDirectoryInode::attach(OpenFileDescription&) +ErrorOr ProcFSProcessSubDirectoryInode::attach(OpenFileDescription&) { - return KSuccess; + return {}; } void ProcFSProcessSubDirectoryInode::did_seek(OpenFileDescription&, off_t) @@ -365,7 +365,7 @@ InodeMetadata ProcFSProcessSubDirectoryInode::metadata() const return metadata; } -KResult ProcFSProcessSubDirectoryInode::traverse_as_directory(Function callback) const +ErrorOr ProcFSProcessSubDirectoryInode::traverse_as_directory(Function callback) const { MutexLocker locker(procfs().m_lock); auto process = Process::from_pid(associated_pid()); @@ -382,7 +382,7 @@ KResult ProcFSProcessSubDirectoryInode::traverse_as_directory(Function> ProcFSProcessSubDirectoryInode::lookup(StringView name) +ErrorOr> ProcFSProcessSubDirectoryInode::lookup(StringView name) { MutexLocker locker(procfs().m_lock); auto process = Process::from_pid(associated_pid()); @@ -398,15 +398,15 @@ KResultOr> ProcFSProcessSubDirectoryInode::lookup(StringVie } } -KResultOr> ProcFSProcessPropertyInode::try_create_for_file_description_link(const ProcFS& procfs, unsigned file_description_index, ProcessID pid) +ErrorOr> ProcFSProcessPropertyInode::try_create_for_file_description_link(const ProcFS& procfs, unsigned file_description_index, ProcessID pid) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, file_description_index, pid)); } -KResultOr> ProcFSProcessPropertyInode::try_create_for_thread_stack(const ProcFS& procfs, ThreadID stack_thread_index, ProcessID pid) +ErrorOr> ProcFSProcessPropertyInode::try_create_for_thread_stack(const ProcFS& procfs, ThreadID stack_thread_index, ProcessID pid) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, stack_thread_index, pid)); } -KResultOr> ProcFSProcessPropertyInode::try_create_for_pid_property(const ProcFS& procfs, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid) +ErrorOr> ProcFSProcessPropertyInode::try_create_for_pid_property(const ProcFS& procfs, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, main_property_type, pid)); } @@ -432,7 +432,7 @@ ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, Thr m_possible_data.property_index = thread_stack_index.value(); } -KResult ProcFSProcessPropertyInode::attach(OpenFileDescription& description) +ErrorOr ProcFSProcessPropertyInode::attach(OpenFileDescription& description) { return refresh_data(description); } @@ -474,11 +474,11 @@ InodeMetadata ProcFSProcessPropertyInode::metadata() const metadata.mtime = traits->modified_time(); return metadata; } -KResult ProcFSProcessPropertyInode::traverse_as_directory(Function) const +ErrorOr ProcFSProcessPropertyInode::traverse_as_directory(Function) const { VERIFY_NOT_REACHED(); } -KResultOr ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const +ErrorOr ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const { dbgln_if(PROCFS_DEBUG, "ProcFS ProcessInformation: read_bytes offset: {} count: {}", offset, count); @@ -489,18 +489,18 @@ KResultOr ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t co auto builder = TRY(KBufferBuilder::try_create()); auto process = Process::from_pid(associated_pid()); if (!process) - return KResult(ESRCH); + return Error::from_errno(ESRCH); TRY(try_to_acquire_data(*process, builder)); auto data_buffer = builder.build(); if (!data_buffer) - return KResult(EFAULT); + return Error::from_errno(EFAULT); ssize_t nread = min(static_cast(data_buffer->size() - offset), static_cast(count)); TRY(buffer.write(data_buffer->data() + offset, nread)); return nread; } if (!description->data()) { dbgln("ProcFS Process Information: Do not have cached data!"); - return KResult(EIO); + return Error::from_errno(EIO); } MutexLocker locker(m_refresh_lock); @@ -516,29 +516,29 @@ KResultOr ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t co return nread; } -KResultOr> ProcFSProcessPropertyInode::lookup(StringView) +ErrorOr> ProcFSProcessPropertyInode::lookup(StringView) { return EINVAL; } -static KResult build_from_cached_data(KBufferBuilder& builder, ProcFSInodeData& cached_data) +static ErrorOr build_from_cached_data(KBufferBuilder& builder, ProcFSInodeData& cached_data) { cached_data.buffer = builder.build(); if (!cached_data.buffer) return ENOMEM; - return KSuccess; + return {}; } -KResult ProcFSProcessPropertyInode::try_to_acquire_data(Process& process, KBufferBuilder& builder) const +ErrorOr ProcFSProcessPropertyInode::try_to_acquire_data(Process& process, KBufferBuilder& builder) const { // FIXME: Verify process is already ref-counted if (m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions) { TRY(process.procfs_get_file_description_link(m_possible_data.property_index, builder)); - return KSuccess; + return {}; } if (m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks) { TRY(process.procfs_get_thread_stack(m_possible_data.property_index, builder)); - return KSuccess; + return {}; } VERIFY(m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved); @@ -562,14 +562,14 @@ KResult ProcFSProcessPropertyInode::try_to_acquire_data(Process& process, KBuffe } } -KResult ProcFSProcessPropertyInode::refresh_data(OpenFileDescription& description) +ErrorOr ProcFSProcessPropertyInode::refresh_data(OpenFileDescription& description) { // For process-specific inodes, hold the process's ptrace lock across refresh // and refuse to load data if the process is not dumpable. // Without this, files opened before a process went non-dumpable could still be used for dumping. auto process = Process::from_pid(associated_pid()); if (!process) - return KResult(ESRCH); + return Error::from_errno(ESRCH); process->ptrace_lock().lock(); if (!process->is_dumpable()) { process->ptrace_lock().unlock(); diff --git a/Kernel/FileSystem/ProcFS.h b/Kernel/FileSystem/ProcFS.h index abd525ea5b2..7801ddbf729 100644 --- a/Kernel/FileSystem/ProcFS.h +++ b/Kernel/FileSystem/ProcFS.h @@ -28,9 +28,9 @@ class ProcFS final : public FileSystem { public: virtual ~ProcFS() override; - static KResultOr> try_create(); + static ErrorOr> try_create(); - virtual KResult initialize() override; + virtual ErrorOr initialize() override; virtual StringView class_name() const override { return "ProcFS"sv; } virtual Inode& root_inode() override; @@ -54,21 +54,21 @@ protected: ProcFS const& procfs() const { return static_cast(Inode::fs()); } // ^Inode - virtual KResult attach(OpenFileDescription& description) = 0; + virtual ErrorOr attach(OpenFileDescription& description) = 0; virtual void did_seek(OpenFileDescription&, off_t) = 0; - virtual KResult flush_metadata() override final; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override final; - virtual KResult add_child(Inode&, const StringView& name, mode_t) override final; - virtual KResult remove_child(const StringView& name) override final; - virtual KResult chmod(mode_t) override final; - virtual KResult chown(UserID, GroupID) override final; + virtual ErrorOr flush_metadata() override final; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override final; + virtual ErrorOr add_child(Inode&, const StringView& name, mode_t) override final; + virtual ErrorOr remove_child(const StringView& name) override final; + virtual ErrorOr chmod(mode_t) override final; + virtual ErrorOr chown(UserID, GroupID) override final; }; class ProcFSGlobalInode : public ProcFSInode { friend class ProcFS; public: - static KResultOr> try_create(const ProcFS&, const ProcFSExposedComponent&); + static ErrorOr> try_create(const ProcFS&, const ProcFSExposedComponent&); virtual ~ProcFSGlobalInode() override {}; StringView name() const; @@ -76,15 +76,15 @@ protected: ProcFSGlobalInode(const ProcFS&, const ProcFSExposedComponent&); // ^Inode - virtual KResult attach(OpenFileDescription& description) override final; - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override final; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override final; + virtual ErrorOr attach(OpenFileDescription& description) override final; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override final; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override final; virtual void did_seek(OpenFileDescription&, off_t) override final; virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView) override; - virtual KResult truncate(u64) override final; - virtual KResult set_mtime(time_t) override final; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView) override; + virtual ErrorOr truncate(u64) override final; + virtual ErrorOr set_mtime(time_t) override final; NonnullRefPtr m_associated_component; }; @@ -93,7 +93,7 @@ class ProcFSLinkInode : public ProcFSGlobalInode { friend class ProcFS; public: - static KResultOr> try_create(const ProcFS&, const ProcFSExposedComponent&); + static ErrorOr> try_create(const ProcFS&, const ProcFSExposedComponent&); protected: ProcFSLinkInode(const ProcFS&, const ProcFSExposedComponent&); @@ -104,15 +104,15 @@ class ProcFSDirectoryInode final : public ProcFSGlobalInode { friend class ProcFS; public: - static KResultOr> try_create(const ProcFS&, const ProcFSExposedComponent&); + static ErrorOr> try_create(const ProcFS&, const ProcFSExposedComponent&); virtual ~ProcFSDirectoryInode() override; protected: ProcFSDirectoryInode(const ProcFS&, const ProcFSExposedComponent&); // ^Inode virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; }; class ProcFSProcessAssociatedInode : public ProcFSInode { @@ -123,7 +123,7 @@ protected: ProcessID associated_pid() const { return m_pid; } // ^Inode - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override final; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override final; private: const ProcessID m_pid; @@ -133,34 +133,34 @@ class ProcFSProcessDirectoryInode final : public ProcFSProcessAssociatedInode { friend class ProcFS; public: - static KResultOr> try_create(const ProcFS&, ProcessID); + static ErrorOr> try_create(const ProcFS&, ProcessID); private: ProcFSProcessDirectoryInode(const ProcFS&, ProcessID); // ^Inode - virtual KResult attach(OpenFileDescription& description) override; + virtual ErrorOr attach(OpenFileDescription& description) override; virtual void did_seek(OpenFileDescription&, off_t) override { } virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override final; - virtual KResultOr> lookup(StringView name) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override final; + virtual ErrorOr> lookup(StringView name) override; }; class ProcFSProcessSubDirectoryInode final : public ProcFSProcessAssociatedInode { friend class ProcFS; public: - static KResultOr> try_create(const ProcFS&, SegmentedProcFSIndex::ProcessSubDirectory, ProcessID); + static ErrorOr> try_create(const ProcFS&, SegmentedProcFSIndex::ProcessSubDirectory, ProcessID); private: ProcFSProcessSubDirectoryInode(const ProcFS&, SegmentedProcFSIndex::ProcessSubDirectory, ProcessID); // ^Inode - virtual KResult attach(OpenFileDescription& description) override; + virtual ErrorOr attach(OpenFileDescription& description) override; virtual void did_seek(OpenFileDescription&, off_t) override; virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override final; - virtual KResultOr> lookup(StringView name) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override final; + virtual ErrorOr> lookup(StringView name) override; const SegmentedProcFSIndex::ProcessSubDirectory m_sub_directory_type; }; @@ -169,24 +169,24 @@ class ProcFSProcessPropertyInode final : public ProcFSProcessAssociatedInode { friend class ProcFS; public: - static KResultOr> try_create_for_file_description_link(const ProcFS&, unsigned, ProcessID); - static KResultOr> try_create_for_thread_stack(const ProcFS&, ThreadID, ProcessID); - static KResultOr> try_create_for_pid_property(const ProcFS&, SegmentedProcFSIndex::MainProcessProperty, ProcessID); + static ErrorOr> try_create_for_file_description_link(const ProcFS&, unsigned, ProcessID); + static ErrorOr> try_create_for_thread_stack(const ProcFS&, ThreadID, ProcessID); + static ErrorOr> try_create_for_pid_property(const ProcFS&, SegmentedProcFSIndex::MainProcessProperty, ProcessID); private: ProcFSProcessPropertyInode(const ProcFS&, SegmentedProcFSIndex::MainProcessProperty, ProcessID); ProcFSProcessPropertyInode(const ProcFS&, ThreadID, ProcessID); ProcFSProcessPropertyInode(const ProcFS&, unsigned, ProcessID); // ^Inode - virtual KResult attach(OpenFileDescription& description) override; + virtual ErrorOr attach(OpenFileDescription& description) override; virtual void did_seek(OpenFileDescription&, off_t) override; virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override final; - virtual KResultOr> lookup(StringView name) override final; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override final; + virtual ErrorOr> lookup(StringView name) override final; - KResult refresh_data(OpenFileDescription& description); - KResult try_to_acquire_data(Process& process, KBufferBuilder& builder) const; + ErrorOr refresh_data(OpenFileDescription& description); + ErrorOr try_to_acquire_data(Process& process, KBufferBuilder& builder) const; const SegmentedProcFSIndex::ProcessSubDirectory m_parent_sub_directory_type; union { diff --git a/Kernel/FileSystem/SysFS.cpp b/Kernel/FileSystem/SysFS.cpp index e45da8a7337..b4a9614822e 100644 --- a/Kernel/FileSystem/SysFS.cpp +++ b/Kernel/FileSystem/SysFS.cpp @@ -46,7 +46,7 @@ NonnullRefPtr SysFSRootDirectory::create() return adopt_ref(*new (nothrow) SysFSRootDirectory); } -KResult SysFSRootDirectory::traverse_as_directory(unsigned fsid, Function callback) const +ErrorOr SysFSRootDirectory::traverse_as_directory(unsigned fsid, Function callback) const { MutexLocker locker(SysFSComponentRegistry::the().get_lock()); callback({ ".", { fsid, component_index() }, 0 }); @@ -56,7 +56,7 @@ KResult SysFSRootDirectory::traverse_as_directory(unsigned fsid, Function> SysFS::try_create() +ErrorOr> SysFS::try_create() { return adopt_nonnull_ref_or_enomem(new (nothrow) SysFS); } @@ -82,10 +82,10 @@ SysFS::~SysFS() { } -KResult SysFS::initialize() +ErrorOr SysFS::initialize() { m_root_inode = TRY(SysFSComponentRegistry::the().root_directory().to_inode(*this)); - return KSuccess; + return {}; } Inode& SysFS::root_inode() @@ -93,7 +93,7 @@ Inode& SysFS::root_inode() return *m_root_inode; } -KResultOr> SysFSInode::try_create(SysFS const& fs, SysFSComponent const& component) +ErrorOr> SysFSInode::try_create(SysFS const& fs, SysFSComponent const& component) { return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSInode(fs, component)); } @@ -115,22 +115,22 @@ void SysFSInode::did_seek(OpenFileDescription& description, off_t new_offset) } } -KResult SysFSInode::attach(OpenFileDescription& description) +ErrorOr SysFSInode::attach(OpenFileDescription& description) { return m_associated_component->refresh_data(description); } -KResultOr SysFSInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* fd) const +ErrorOr SysFSInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* fd) const { return m_associated_component->read_bytes(offset, count, buffer, fd); } -KResult SysFSInode::traverse_as_directory(Function) const +ErrorOr SysFSInode::traverse_as_directory(Function) const { VERIFY_NOT_REACHED(); } -KResultOr> SysFSInode::lookup(StringView) +ErrorOr> SysFSInode::lookup(StringView) { VERIFY_NOT_REACHED(); } @@ -148,52 +148,52 @@ InodeMetadata SysFSInode::metadata() const return metadata; } -KResult SysFSInode::flush_metadata() +ErrorOr SysFSInode::flush_metadata() { - return KSuccess; + return {}; } -KResultOr SysFSInode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription* fd) +ErrorOr SysFSInode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription* fd) { return m_associated_component->write_bytes(offset, count, buffer, fd); } -KResultOr> SysFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) +ErrorOr> SysFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) { return EROFS; } -KResult SysFSInode::add_child(Inode&, StringView const&, mode_t) +ErrorOr SysFSInode::add_child(Inode&, StringView const&, mode_t) { return EROFS; } -KResult SysFSInode::remove_child(StringView const&) +ErrorOr SysFSInode::remove_child(StringView const&) { return EROFS; } -KResult SysFSInode::chmod(mode_t) +ErrorOr SysFSInode::chmod(mode_t) { return EPERM; } -KResult SysFSInode::chown(UserID, GroupID) +ErrorOr SysFSInode::chown(UserID, GroupID) { return EPERM; } -KResult SysFSInode::set_mtime(time_t time) +ErrorOr SysFSInode::set_mtime(time_t time) { return m_associated_component->set_mtime(time); } -KResult SysFSInode::truncate(u64 size) +ErrorOr SysFSInode::truncate(u64 size) { return m_associated_component->truncate(size); } -KResultOr> SysFSDirectoryInode::try_create(SysFS const& sysfs, SysFSComponent const& component) +ErrorOr> SysFSDirectoryInode::try_create(SysFS const& sysfs, SysFSComponent const& component) { return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSDirectoryInode(sysfs, component)); } @@ -219,13 +219,13 @@ InodeMetadata SysFSDirectoryInode::metadata() const metadata.mtime = mepoch; return metadata; } -KResult SysFSDirectoryInode::traverse_as_directory(Function callback) const +ErrorOr SysFSDirectoryInode::traverse_as_directory(Function callback) const { MutexLocker locker(fs().m_lock); return m_associated_component->traverse_as_directory(fs().fsid(), move(callback)); } -KResultOr> SysFSDirectoryInode::lookup(StringView name) +ErrorOr> SysFSDirectoryInode::lookup(StringView name) { MutexLocker locker(fs().m_lock); auto component = m_associated_component->lookup(name); diff --git a/Kernel/FileSystem/SysFS.h b/Kernel/FileSystem/SysFS.h index d62781f48c4..2a8a8a45182 100644 --- a/Kernel/FileSystem/SysFS.h +++ b/Kernel/FileSystem/SysFS.h @@ -19,7 +19,7 @@ class SysFSRootDirectory final : public SysFSDirectory { public: static NonnullRefPtr create(); - virtual KResult traverse_as_directory(unsigned, Function) const override; + virtual ErrorOr traverse_as_directory(unsigned, Function) const override; private: SysFSRootDirectory(); @@ -53,7 +53,7 @@ private: class SysFSBlockDevicesDirectory final : public SysFSDirectory { public: static NonnullRefPtr must_create(SysFSDevicesDirectory const&); - virtual KResult traverse_as_directory(unsigned, Function) const override; + virtual ErrorOr traverse_as_directory(unsigned, Function) const override; virtual RefPtr lookup(StringView name) override; private: @@ -63,7 +63,7 @@ private: class SysFSCharacterDevicesDirectory final : public SysFSDirectory { public: static NonnullRefPtr must_create(SysFSDevicesDirectory const&); - virtual KResult traverse_as_directory(unsigned, Function) const override; + virtual ErrorOr traverse_as_directory(unsigned, Function) const override; virtual RefPtr lookup(StringView name) override; private: @@ -111,9 +111,9 @@ class SysFS final : public FileSystem { public: virtual ~SysFS() override; - static KResultOr> try_create(); + static ErrorOr> try_create(); - virtual KResult initialize() override; + virtual ErrorOr initialize() override; virtual StringView class_name() const override { return "SysFS"sv; } virtual Inode& root_inode() override; @@ -129,26 +129,26 @@ class SysFSInode : public Inode { friend class SysFSDirectoryInode; public: - static KResultOr> try_create(SysFS const&, SysFSComponent const&); + static ErrorOr> try_create(SysFS const&, SysFSComponent const&); StringView name() const { return m_associated_component->name(); } protected: SysFSInode(SysFS const&, SysFSComponent const&); - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; - virtual KResult flush_metadata() override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; + virtual ErrorOr flush_metadata() override; virtual InodeMetadata metadata() const override; - virtual KResultOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) override; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual KResult add_child(Inode&, StringView const& name, mode_t) override; - virtual KResult remove_child(StringView const& name) override; - virtual KResult chmod(mode_t) override; - virtual KResult chown(UserID, GroupID) override; - virtual KResult truncate(u64) override; - virtual KResult set_mtime(time_t); + virtual ErrorOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) override; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; + virtual ErrorOr add_child(Inode&, StringView const& name, mode_t) override; + virtual ErrorOr remove_child(StringView const& name) override; + virtual ErrorOr chmod(mode_t) override; + virtual ErrorOr chown(UserID, GroupID) override; + virtual ErrorOr truncate(u64) override; + virtual ErrorOr set_mtime(time_t); - virtual KResult attach(OpenFileDescription& description) override final; + virtual ErrorOr attach(OpenFileDescription& description) override final; virtual void did_seek(OpenFileDescription&, off_t) override final; NonnullRefPtr m_associated_component; @@ -158,7 +158,7 @@ class SysFSDirectoryInode : public SysFSInode { friend class SysFS; public: - static KResultOr> try_create(SysFS const&, SysFSComponent const&); + static ErrorOr> try_create(SysFS const&, SysFSComponent const&); virtual ~SysFSDirectoryInode() override; SysFS& fs() { return static_cast(Inode::fs()); } @@ -168,8 +168,8 @@ protected: SysFSDirectoryInode(SysFS const&, SysFSComponent const&); // ^Inode virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; }; } diff --git a/Kernel/FileSystem/SysFSComponent.cpp b/Kernel/FileSystem/SysFSComponent.cpp index a2cd1db4e60..807a129ab7b 100644 --- a/Kernel/FileSystem/SysFSComponent.cpp +++ b/Kernel/FileSystem/SysFSComponent.cpp @@ -31,7 +31,7 @@ mode_t SysFSComponent::permissions() const return S_IRUSR | S_IRGRP | S_IROTH; } -KResult SysFSDirectory::traverse_as_directory(unsigned fsid, Function callback) const +ErrorOr SysFSDirectory::traverse_as_directory(unsigned fsid, Function callback) const { MutexLocker locker(SysFSComponentRegistry::the().get_lock()); VERIFY(m_parent_directory); @@ -42,7 +42,7 @@ KResult SysFSDirectory::traverse_as_directory(unsigned fsid, Function SysFSDirectory::lookup(StringView name) @@ -66,12 +66,12 @@ SysFSDirectory::SysFSDirectory(StringView name, SysFSDirectory const& parent_dir { } -KResultOr> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const +ErrorOr> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const { return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this)); } -KResultOr> SysFSComponent::to_inode(SysFS const& sysfs_instance) const +ErrorOr> SysFSComponent::to_inode(SysFS const& sysfs_instance) const { return SysFSInode::try_create(sysfs_instance, *this); } diff --git a/Kernel/FileSystem/SysFSComponent.h b/Kernel/FileSystem/SysFSComponent.h index 6e29c1ed82e..e42620d419c 100644 --- a/Kernel/FileSystem/SysFSComponent.h +++ b/Kernel/FileSystem/SysFSComponent.h @@ -6,12 +6,12 @@ #pragma once +#include #include #include #include #include #include -#include #include #include #include @@ -26,16 +26,16 @@ struct SysFSInodeData : public OpenFileDescriptionData { class SysFSComponent : public RefCounted { public: virtual StringView name() const { return m_name->view(); } - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { return KResult(ENOTIMPL); } - virtual KResult traverse_as_directory(unsigned, Function) const { VERIFY_NOT_REACHED(); } + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { return Error::from_errno(ENOTIMPL); } + virtual ErrorOr traverse_as_directory(unsigned, Function) const { VERIFY_NOT_REACHED(); } virtual RefPtr lookup(StringView) { VERIFY_NOT_REACHED(); }; virtual mode_t permissions() const; - virtual KResult truncate(u64) { return EPERM; } - virtual KResult set_mtime(time_t) { return ENOTIMPL; } - virtual KResultOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) { return EROFS; } - virtual KResult refresh_data(OpenFileDescription&) const { return KSuccess; } + virtual ErrorOr truncate(u64) { return EPERM; } + virtual ErrorOr set_mtime(time_t) { return ENOTIMPL; } + virtual ErrorOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) { return EROFS; } + virtual ErrorOr refresh_data(OpenFileDescription&) const { return {}; } - virtual KResultOr> to_inode(SysFS const&) const; + virtual ErrorOr> to_inode(SysFS const&) const; InodeIndex component_index() const { return m_component_index; }; @@ -51,10 +51,10 @@ private: class SysFSDirectory : public SysFSComponent { public: - virtual KResult traverse_as_directory(unsigned, Function) const override; + virtual ErrorOr traverse_as_directory(unsigned, Function) const override; virtual RefPtr lookup(StringView name) override; - virtual KResultOr> to_inode(SysFS const& sysfs_instance) const override final; + virtual ErrorOr> to_inode(SysFS const& sysfs_instance) const override final; protected: explicit SysFSDirectory(StringView name); diff --git a/Kernel/FileSystem/TmpFS.cpp b/Kernel/FileSystem/TmpFS.cpp index 17115a093a9..824c04a1064 100644 --- a/Kernel/FileSystem/TmpFS.cpp +++ b/Kernel/FileSystem/TmpFS.cpp @@ -10,7 +10,7 @@ namespace Kernel { -KResultOr> TmpFS::try_create() +ErrorOr> TmpFS::try_create() { return adopt_nonnull_ref_or_enomem(new (nothrow) TmpFS); } @@ -23,10 +23,10 @@ TmpFS::~TmpFS() { } -KResult TmpFS::initialize() +ErrorOr TmpFS::initialize() { m_root_inode = TRY(TmpFSInode::try_create_root(*this)); - return KSuccess; + return {}; } Inode& TmpFS::root_inode() @@ -59,7 +59,7 @@ unsigned TmpFS::next_inode_index() return m_next_inode_index++; } -KResultOr> TmpFS::get_inode(InodeIdentifier identifier) const +ErrorOr> TmpFS::get_inode(InodeIdentifier identifier) const { MutexLocker locker(m_lock, Mutex::Mode::Shared); VERIFY(identifier.fsid() == fsid()); @@ -82,14 +82,14 @@ TmpFSInode::~TmpFSInode() { } -KResultOr> TmpFSInode::try_create(TmpFS& fs, InodeMetadata const& metadata, InodeIdentifier parent) +ErrorOr> TmpFSInode::try_create(TmpFS& fs, InodeMetadata const& metadata, InodeIdentifier parent) { auto inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TmpFSInode(fs, metadata, parent))); fs.register_inode(inode); return inode; } -KResultOr> TmpFSInode::try_create_root(TmpFS& fs) +ErrorOr> TmpFSInode::try_create_root(TmpFS& fs) { InodeMetadata metadata; auto now = kgettimeofday().to_truncated_seconds(); @@ -107,7 +107,7 @@ InodeMetadata TmpFSInode::metadata() const return m_metadata; } -KResult TmpFSInode::traverse_as_directory(Function callback) const +ErrorOr TmpFSInode::traverse_as_directory(Function callback) const { MutexLocker locker(m_inode_lock, Mutex::Mode::Shared); @@ -120,10 +120,10 @@ KResult TmpFSInode::traverse_as_directory(Functionview(), child.inode->identifier(), 0 }); } - return KSuccess; + return {}; } -KResultOr TmpFSInode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const +ErrorOr TmpFSInode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const { MutexLocker locker(m_inode_lock, Mutex::Mode::Shared); VERIFY(!is_directory()); @@ -142,7 +142,7 @@ KResultOr TmpFSInode::read_bytes(off_t offset, size_t size, UserOrKernel return size; } -KResultOr TmpFSInode::write_bytes(off_t offset, size_t size, const UserOrKernelBuffer& buffer, OpenFileDescription*) +ErrorOr TmpFSInode::write_bytes(off_t offset, size_t size, const UserOrKernelBuffer& buffer, OpenFileDescription*) { MutexLocker locker(m_inode_lock); VERIFY(!is_directory()); @@ -185,7 +185,7 @@ KResultOr TmpFSInode::write_bytes(off_t offset, size_t size, const UserO return size; } -KResultOr> TmpFSInode::lookup(StringView name) +ErrorOr> TmpFSInode::lookup(StringView name) { MutexLocker locker(m_inode_lock, Mutex::Mode::Shared); VERIFY(is_directory()); @@ -216,7 +216,7 @@ void TmpFSInode::notify_watchers() set_metadata_dirty(false); } -KResult TmpFSInode::flush_metadata() +ErrorOr TmpFSInode::flush_metadata() { // We don't really have any metadata that could become dirty. // The only reason we even call set_metadata_dirty() is @@ -224,29 +224,29 @@ KResult TmpFSInode::flush_metadata() // switched to a different mechanism, we can stop ever marking // our metadata as dirty at all. set_metadata_dirty(false); - return KSuccess; + return {}; } -KResult TmpFSInode::chmod(mode_t mode) +ErrorOr TmpFSInode::chmod(mode_t mode) { MutexLocker locker(m_inode_lock); m_metadata.mode = mode; notify_watchers(); - return KSuccess; + return {}; } -KResult TmpFSInode::chown(UserID uid, GroupID gid) +ErrorOr TmpFSInode::chown(UserID uid, GroupID gid) { MutexLocker locker(m_inode_lock); m_metadata.uid = uid; m_metadata.gid = gid; notify_watchers(); - return KSuccess; + return {}; } -KResultOr> TmpFSInode::create_child(StringView name, mode_t mode, dev_t dev, UserID uid, GroupID gid) +ErrorOr> TmpFSInode::create_child(StringView name, mode_t mode, dev_t dev, UserID uid, GroupID gid) { MutexLocker locker(m_inode_lock); @@ -269,7 +269,7 @@ KResultOr> TmpFSInode::create_child(StringView name, mode_t return child; } -KResult TmpFSInode::add_child(Inode& child, StringView const& name, mode_t) +ErrorOr TmpFSInode::add_child(Inode& child, StringView const& name, mode_t) { VERIFY(is_directory()); VERIFY(child.fsid() == fsid()); @@ -286,16 +286,16 @@ KResult TmpFSInode::add_child(Inode& child, StringView const& name, mode_t) MutexLocker locker(m_inode_lock); m_children.append(*child_entry); did_add_child(child.identifier(), name); - return KSuccess; + return {}; } -KResult TmpFSInode::remove_child(StringView const& name) +ErrorOr TmpFSInode::remove_child(StringView const& name) { MutexLocker locker(m_inode_lock); VERIFY(is_directory()); if (name == "." || name == "..") - return KSuccess; + return {}; auto* child = find_child_by_name(name); if (!child) @@ -307,10 +307,10 @@ KResult TmpFSInode::remove_child(StringView const& name) did_remove_child(child_id, name); // Balanced by `new` in add_child() delete child; - return KSuccess; + return {}; } -KResult TmpFSInode::truncate(u64 size) +ErrorOr TmpFSInode::truncate(u64 size) { MutexLocker locker(m_inode_lock); VERIFY(!is_directory()); @@ -333,34 +333,34 @@ KResult TmpFSInode::truncate(u64 size) m_metadata.size = size; notify_watchers(); - return KSuccess; + return {}; } -KResult TmpFSInode::set_atime(time_t time) +ErrorOr TmpFSInode::set_atime(time_t time) { MutexLocker locker(m_inode_lock); m_metadata.atime = time; notify_watchers(); - return KSuccess; + return {}; } -KResult TmpFSInode::set_ctime(time_t time) +ErrorOr TmpFSInode::set_ctime(time_t time) { MutexLocker locker(m_inode_lock); m_metadata.ctime = time; notify_watchers(); - return KSuccess; + return {}; } -KResult TmpFSInode::set_mtime(time_t t) +ErrorOr TmpFSInode::set_mtime(time_t t) { MutexLocker locker(m_inode_lock); m_metadata.mtime = t; notify_watchers(); - return KSuccess; + return {}; } void TmpFSInode::one_ref_left() diff --git a/Kernel/FileSystem/TmpFS.h b/Kernel/FileSystem/TmpFS.h index c13cc908f27..91529647299 100644 --- a/Kernel/FileSystem/TmpFS.h +++ b/Kernel/FileSystem/TmpFS.h @@ -19,8 +19,8 @@ class TmpFS final : public FileSystem { public: virtual ~TmpFS() override; - static KResultOr> try_create(); - virtual KResult initialize() override; + static ErrorOr> try_create(); + virtual ErrorOr initialize() override; virtual StringView class_name() const override { return "TmpFS"sv; } @@ -34,7 +34,7 @@ private: RefPtr m_root_inode; HashMap> m_inodes; - KResultOr> get_inode(InodeIdentifier identifier) const; + ErrorOr> get_inode(InodeIdentifier identifier) const; void register_inode(TmpFSInode&); void unregister_inode(InodeIdentifier); @@ -52,27 +52,27 @@ public: const TmpFS& fs() const { return static_cast(Inode::fs()); } // ^Inode - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function) const override; - virtual KResultOr> lookup(StringView name) override; - virtual KResult flush_metadata() override; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; - virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual KResult add_child(Inode&, const StringView& name, mode_t) override; - virtual KResult remove_child(const StringView& name) override; - virtual KResult chmod(mode_t) override; - virtual KResult chown(UserID, GroupID) override; - virtual KResult truncate(u64) override; - virtual KResult set_atime(time_t) override; - virtual KResult set_ctime(time_t) override; - virtual KResult set_mtime(time_t) override; + virtual ErrorOr traverse_as_directory(Function) const override; + virtual ErrorOr> lookup(StringView name) override; + virtual ErrorOr flush_metadata() override; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override; + virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; + virtual ErrorOr add_child(Inode&, const StringView& name, mode_t) override; + virtual ErrorOr remove_child(const StringView& name) override; + virtual ErrorOr chmod(mode_t) override; + virtual ErrorOr chown(UserID, GroupID) override; + virtual ErrorOr truncate(u64) override; + virtual ErrorOr set_atime(time_t) override; + virtual ErrorOr set_ctime(time_t) override; + virtual ErrorOr set_mtime(time_t) override; virtual void one_ref_left() override; private: TmpFSInode(TmpFS& fs, const InodeMetadata& metadata, InodeIdentifier parent); - static KResultOr> try_create(TmpFS&, InodeMetadata const& metadata, InodeIdentifier parent); - static KResultOr> try_create_root(TmpFS&); + static ErrorOr> try_create(TmpFS&, InodeMetadata const& metadata, InodeIdentifier parent); + static ErrorOr> try_create_root(TmpFS&); void notify_watchers(); struct Child { diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index b7c8d908266..319cb91d9c0 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -51,9 +51,9 @@ InodeIdentifier VirtualFileSystem::root_inode_id() const return m_root_inode->identifier(); } -KResult VirtualFileSystem::mount(FileSystem& fs, Custody& mount_point, int flags) +ErrorOr VirtualFileSystem::mount(FileSystem& fs, Custody& mount_point, int flags) { - return m_mounts.with_exclusive([&](auto& mounts) -> KResult { + return m_mounts.with_exclusive([&](auto& mounts) -> ErrorOr { auto& inode = mount_point.inode(); dbgln("VirtualFileSystem: Mounting {} at inode {} with flags {}", fs.class_name(), @@ -62,22 +62,22 @@ KResult VirtualFileSystem::mount(FileSystem& fs, Custody& mount_point, int flags // FIXME: check that this is not already a mount point Mount mount { fs, &mount_point, flags }; mounts.append(move(mount)); - return KSuccess; + return {}; }); } -KResult VirtualFileSystem::bind_mount(Custody& source, Custody& mount_point, int flags) +ErrorOr VirtualFileSystem::bind_mount(Custody& source, Custody& mount_point, int flags) { - return m_mounts.with_exclusive([&](auto& mounts) -> KResult { + return m_mounts.with_exclusive([&](auto& mounts) -> ErrorOr { dbgln("VirtualFileSystem: Bind-mounting inode {} at inode {}", source.inode().identifier(), mount_point.inode().identifier()); // FIXME: check that this is not already a mount point Mount mount { source.inode(), mount_point, flags }; mounts.append(move(mount)); - return KSuccess; + return {}; }); } -KResult VirtualFileSystem::remount(Custody& mount_point, int new_flags) +ErrorOr VirtualFileSystem::remount(Custody& mount_point, int new_flags) { dbgln("VirtualFileSystem: Remounting inode {}", mount_point.inode().identifier()); @@ -86,14 +86,14 @@ KResult VirtualFileSystem::remount(Custody& mount_point, int new_flags) return ENODEV; mount->set_flags(new_flags); - return KSuccess; + return {}; } -KResult VirtualFileSystem::unmount(Inode& guest_inode) +ErrorOr VirtualFileSystem::unmount(Inode& guest_inode) { dbgln("VirtualFileSystem: unmount called with inode {}", guest_inode.identifier()); - return m_mounts.with_exclusive([&](auto& mounts) -> KResult { + return m_mounts.with_exclusive([&](auto& mounts) -> ErrorOr { for (size_t i = 0; i < mounts.size(); ++i) { auto& mount = mounts[i]; if (&mount.guest() != &guest_inode) @@ -101,14 +101,14 @@ KResult VirtualFileSystem::unmount(Inode& guest_inode) TRY(mount.guest_fs().prepare_to_unmount()); dbgln("VirtualFileSystem: Unmounting file system {}...", mount.guest_fs().fsid()); mounts.unstable_take(i); - return KSuccess; + return {}; } dbgln("VirtualFileSystem: Nothing mounted on inode {}", guest_inode.identifier()); return ENODEV; }); } -KResult VirtualFileSystem::mount_root(FileSystem& fs) +ErrorOr VirtualFileSystem::mount_root(FileSystem& fs) { if (m_root_inode) { dmesgln("VirtualFileSystem: mount_root can't mount another root"); @@ -132,7 +132,7 @@ KResult VirtualFileSystem::mount_root(FileSystem& fs) }); m_root_custody = TRY(Custody::try_create(nullptr, "", *m_root_inode, root_mount_flags)); - return KSuccess; + return {}; } auto VirtualFileSystem::find_mount_for_host(InodeIdentifier id) -> Mount* @@ -162,7 +162,7 @@ bool VirtualFileSystem::is_vfs_root(InodeIdentifier inode) const return inode == root_inode_id(); } -KResult VirtualFileSystem::traverse_directory_inode(Inode& dir_inode, Function callback) +ErrorOr VirtualFileSystem::traverse_directory_inode(Inode& dir_inode, Function callback) { return dir_inode.traverse_as_directory([&](auto& entry) { InodeIdentifier resolved_inode; @@ -184,7 +184,7 @@ KResult VirtualFileSystem::traverse_directory_inode(Inode& dir_inode, Function VirtualFileSystem::utime(StringView path, Custody& base, time_t atime, time_t mtime) { auto custody = TRY(resolve_path(path, base)); auto& inode = custody->inode(); @@ -196,16 +196,16 @@ KResult VirtualFileSystem::utime(StringView path, Custody& base, time_t atime, t TRY(inode.set_atime(atime)); TRY(inode.set_mtime(mtime)); - return KSuccess; + return {}; } -KResultOr VirtualFileSystem::lookup_metadata(StringView path, Custody& base, int options) +ErrorOr VirtualFileSystem::lookup_metadata(StringView path, Custody& base, int options) { auto custody = TRY(resolve_path(path, base, nullptr, options)); return custody->inode().metadata(); } -KResultOr> VirtualFileSystem::open(StringView path, int options, mode_t mode, Custody& base, Optional owner) +ErrorOr> VirtualFileSystem::open(StringView path, int options, mode_t mode, Custody& base, Optional owner) { if ((options & O_CREAT) && (options & O_DIRECTORY)) return EINVAL; @@ -215,9 +215,9 @@ KResultOr> VirtualFileSystem::open(StringView if (custody_or_error.is_error()) { // NOTE: ENOENT with a non-null parent custody signals us that the immediate parent // of the file exists, but the file itself does not. - if ((options & O_CREAT) && custody_or_error.error() == ENOENT && parent_custody) + if ((options & O_CREAT) && custody_or_error.error().code() == ENOENT && parent_custody) return create(path, options, mode, *parent_custody, move(owner)); - return custody_or_error.error(); + return custody_or_error.release_error(); } if ((options & O_CREAT) && (options & O_EXCL)) @@ -297,7 +297,7 @@ KResultOr> VirtualFileSystem::open(StringView return description; } -KResult VirtualFileSystem::mknod(StringView path, mode_t mode, dev_t dev, Custody& base) +ErrorOr VirtualFileSystem::mknod(StringView path, mode_t mode, dev_t dev, Custody& base) { if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode)) return EINVAL; @@ -308,8 +308,8 @@ KResult VirtualFileSystem::mknod(StringView path, mode_t mode, dev_t dev, Custod return EEXIST; if (!parent_custody) return ENOENT; - if (existing_file_or_error.error() != ENOENT) - return existing_file_or_error.error(); + if (existing_file_or_error.error().code() != ENOENT) + return existing_file_or_error.release_error(); auto& parent_inode = parent_custody->inode(); auto& current_process = Process::current(); if (!parent_inode.metadata().may_write(current_process)) @@ -319,10 +319,11 @@ KResult VirtualFileSystem::mknod(StringView path, mode_t mode, dev_t dev, Custod auto basename = KLexicalPath::basename(path); dbgln_if(VFS_DEBUG, "VirtualFileSystem::mknod: '{}' mode={} dev={} in {}", basename, mode, dev, parent_inode.identifier()); - return parent_inode.create_child(basename, mode, dev, current_process.euid(), current_process.egid()).result(); + TRY(parent_inode.create_child(basename, mode, dev, current_process.euid(), current_process.egid())); + return {}; } -KResultOr> VirtualFileSystem::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional owner) +ErrorOr> VirtualFileSystem::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional owner) { auto basename = KLexicalPath::basename(path); auto parent_path = TRY(parent_custody.try_serialize_absolute_path()); @@ -354,7 +355,7 @@ KResultOr> VirtualFileSystem::create(StringVi return description; } -KResult VirtualFileSystem::mkdir(StringView path, mode_t mode, Custody& base) +ErrorOr VirtualFileSystem::mkdir(StringView path, mode_t mode, Custody& base) { // Unlike in basically every other case, where it's only the last // path component (the one being created) that is allowed not to @@ -371,9 +372,9 @@ KResult VirtualFileSystem::mkdir(StringView path, mode_t mode, Custody& base) if (!result.is_error()) return EEXIST; else if (!parent_custody) - return result.error(); + return result.release_error(); // NOTE: If resolve_path fails with a non-null parent custody, the error should be ENOENT. - VERIFY(result.error() == ENOENT); + VERIFY(result.error().code() == ENOENT); auto& parent_inode = parent_custody->inode(); auto& current_process = Process::current(); @@ -384,10 +385,11 @@ KResult VirtualFileSystem::mkdir(StringView path, mode_t mode, Custody& base) auto basename = KLexicalPath::basename(path); dbgln_if(VFS_DEBUG, "VirtualFileSystem::mkdir: '{}' in {}", basename, parent_inode.identifier()); - return parent_inode.create_child(basename, S_IFDIR | mode, 0, current_process.euid(), current_process.egid()).result(); + TRY(parent_inode.create_child(basename, S_IFDIR | mode, 0, current_process.euid(), current_process.egid())); + return {}; } -KResult VirtualFileSystem::access(StringView path, int mode, Custody& base) +ErrorOr VirtualFileSystem::access(StringView path, int mode, Custody& base) { auto custody = TRY(resolve_path(path, base)); @@ -408,10 +410,10 @@ KResult VirtualFileSystem::access(StringView path, int mode, Custody& base) if (!metadata.may_execute(current_process)) return EACCES; } - return KSuccess; + return {}; } -KResultOr> VirtualFileSystem::open_directory(StringView path, Custody& base) +ErrorOr> VirtualFileSystem::open_directory(StringView path, Custody& base) { auto custody = TRY(resolve_path(path, base)); auto& inode = custody->inode(); @@ -422,7 +424,7 @@ KResultOr> VirtualFileSystem::open_directory(StringView p return custody; } -KResult VirtualFileSystem::chmod(Custody& custody, mode_t mode) +ErrorOr VirtualFileSystem::chmod(Custody& custody, mode_t mode) { auto& inode = custody.inode(); @@ -437,13 +439,13 @@ KResult VirtualFileSystem::chmod(Custody& custody, mode_t mode) return inode.chmod(mode); } -KResult VirtualFileSystem::chmod(StringView path, mode_t mode, Custody& base) +ErrorOr VirtualFileSystem::chmod(StringView path, mode_t mode, Custody& base) { auto custody = TRY(resolve_path(path, base)); return chmod(custody, mode); } -KResult VirtualFileSystem::rename(StringView old_path, StringView new_path, Custody& base) +ErrorOr VirtualFileSystem::rename(StringView old_path, StringView new_path, Custody& base) { RefPtr old_parent_custody; auto old_custody = TRY(resolve_path(old_path, base, &old_parent_custody, O_NOFOLLOW_NOERROR)); @@ -452,8 +454,8 @@ KResult VirtualFileSystem::rename(StringView old_path, StringView new_path, Cust RefPtr new_parent_custody; auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody); if (new_custody_or_error.is_error()) { - if (new_custody_or_error.error() != ENOENT || !new_parent_custody) - return new_custody_or_error.error(); + if (new_custody_or_error.error().code() != ENOENT || !new_parent_custody) + return new_custody_or_error.release_error(); } if (!old_parent_custody || !new_parent_custody) { @@ -509,14 +511,14 @@ KResult VirtualFileSystem::rename(StringView old_path, StringView new_path, Cust return EINVAL; if (old_basename == new_basename && old_parent_inode.index() == new_parent_inode.index()) - return KSuccess; + return {}; if (!new_custody_or_error.is_error()) { auto& new_custody = *new_custody_or_error.value(); auto& new_inode = new_custody.inode(); // FIXME: Is this really correct? Check what other systems do. if (&new_inode == &old_inode) - return KSuccess; + return {}; if (new_parent_inode.metadata().is_sticky()) { if (!current_process.is_superuser() && new_inode.metadata().uid != current_process.euid()) return EACCES; @@ -528,10 +530,10 @@ KResult VirtualFileSystem::rename(StringView old_path, StringView new_path, Cust TRY(new_parent_inode.add_child(old_inode, new_basename, old_inode.mode())); TRY(old_parent_inode.remove_child(old_basename)); - return KSuccess; + return {}; } -KResult VirtualFileSystem::chown(Custody& custody, UserID a_uid, GroupID a_gid) +ErrorOr VirtualFileSystem::chown(Custody& custody, UserID a_uid, GroupID a_gid) { auto& inode = custody.inode(); auto metadata = inode.metadata(); @@ -567,7 +569,7 @@ KResult VirtualFileSystem::chown(Custody& custody, UserID a_uid, GroupID a_gid) return inode.chown(new_uid, new_gid); } -KResult VirtualFileSystem::chown(StringView path, UserID a_uid, GroupID a_gid, Custody& base) +ErrorOr VirtualFileSystem::chown(StringView path, UserID a_uid, GroupID a_gid, Custody& base) { auto custody = TRY(resolve_path(path, base)); return chown(custody, a_uid, a_gid); @@ -590,7 +592,7 @@ static bool hard_link_allowed(const Inode& inode) return false; } -KResult VirtualFileSystem::link(StringView old_path, StringView new_path, Custody& base) +ErrorOr VirtualFileSystem::link(StringView old_path, StringView new_path, Custody& base) { auto old_custody = TRY(resolve_path(old_path, base)); auto& old_inode = old_custody->inode(); @@ -623,7 +625,7 @@ KResult VirtualFileSystem::link(StringView old_path, StringView new_path, Custod return parent_inode.add_child(old_inode, KLexicalPath::basename(new_path), old_inode.mode()); } -KResult VirtualFileSystem::unlink(StringView path, Custody& base) +ErrorOr VirtualFileSystem::unlink(StringView path, Custody& base) { RefPtr parent_custody; auto custody = TRY(resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR | O_UNLINK_INTERNAL)); @@ -653,7 +655,7 @@ KResult VirtualFileSystem::unlink(StringView path, Custody& base) return parent_inode.remove_child(KLexicalPath::basename(path)); } -KResult VirtualFileSystem::symlink(StringView target, StringView linkpath, Custody& base) +ErrorOr VirtualFileSystem::symlink(StringView target, StringView linkpath, Custody& base) { RefPtr parent_custody; auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody); @@ -661,8 +663,8 @@ KResult VirtualFileSystem::symlink(StringView target, StringView linkpath, Custo return EEXIST; if (!parent_custody) return ENOENT; - if (existing_custody_or_error.is_error() && existing_custody_or_error.error() != ENOENT) - return existing_custody_or_error.error(); + if (existing_custody_or_error.is_error() && existing_custody_or_error.error().code() != ENOENT) + return existing_custody_or_error.release_error(); auto& parent_inode = parent_custody->inode(); auto& current_process = Process::current(); if (!parent_inode.metadata().may_write(current_process)) @@ -677,10 +679,10 @@ KResult VirtualFileSystem::symlink(StringView target, StringView linkpath, Custo auto target_buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast((const u8*)target.characters_without_null_termination())); TRY(inode->write_bytes(0, target.length(), target_buffer, nullptr)); - return KSuccess; + return {}; } -KResult VirtualFileSystem::rmdir(StringView path, Custody& base) +ErrorOr VirtualFileSystem::rmdir(StringView path, Custody& base) { RefPtr parent_custody; auto custody = TRY(resolve_path(path, base, &parent_custody)); @@ -755,20 +757,20 @@ UnveilNode const& VirtualFileSystem::find_matching_unveiled_path(StringView path return unveil_root.traverse_until_last_accessible_node(path_parts.begin(), path_parts.end()); } -KResult VirtualFileSystem::validate_path_against_process_veil(Custody const& custody, int options) +ErrorOr VirtualFileSystem::validate_path_against_process_veil(Custody const& custody, int options) { if (Process::current().veil_state() == VeilState::None) - return KSuccess; + return {}; auto absolute_path = TRY(custody.try_serialize_absolute_path()); return validate_path_against_process_veil(absolute_path->view(), options); } -KResult VirtualFileSystem::validate_path_against_process_veil(StringView path, int options) +ErrorOr VirtualFileSystem::validate_path_against_process_veil(StringView path, int options) { if (Process::current().veil_state() == VeilState::None) - return KSuccess; + return {}; if (options == O_EXEC && path == "/usr/lib/Loader.so") - return KSuccess; + return {}; VERIFY(path.starts_with('/')); VERIFY(!path.contains("/../"sv) && !path.ends_with("/.."sv)); @@ -794,7 +796,7 @@ KResult VirtualFileSystem::validate_path_against_process_veil(StringView path, i dump_backtrace(); return EACCES; } - return KSuccess; + return {}; } if (options & O_RDONLY) { if (options & O_DIRECTORY) { @@ -825,10 +827,10 @@ KResult VirtualFileSystem::validate_path_against_process_veil(StringView path, i return EACCES; } } - return KSuccess; + return {}; } -KResultOr> VirtualFileSystem::resolve_path(StringView path, Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) +ErrorOr> VirtualFileSystem::resolve_path(StringView path, Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) { auto custody = TRY(resolve_path_without_veil(path, base, out_parent, options, symlink_recursion_level)); TRY(validate_path_against_process_veil(*custody, options)); @@ -850,7 +852,7 @@ static bool safe_to_follow_symlink(const Inode& inode, const InodeMetadata& pare return false; } -KResultOr> VirtualFileSystem::resolve_path_without_veil(StringView path, Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) +ErrorOr> VirtualFileSystem::resolve_path_without_veil(StringView path, Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) { if (symlink_recursion_level >= symlink_recursion_limit) return ELOOP; @@ -898,7 +900,7 @@ KResultOr> VirtualFileSystem::resolve_path_without_veil(S // does not exist yet. *out_parent = have_more_parts ? nullptr : &parent; } - return child_or_error.error(); + return child_or_error.release_error(); } auto child_inode = child_or_error.release_value(); diff --git a/Kernel/FileSystem/VirtualFileSystem.h b/Kernel/FileSystem/VirtualFileSystem.h index 09b4fd05de9..bbf2d77ec0c 100644 --- a/Kernel/FileSystem/VirtualFileSystem.h +++ b/Kernel/FileSystem/VirtualFileSystem.h @@ -7,13 +7,13 @@ #pragma once #include +#include #include #include #include #include #include #include -#include #include #include #include @@ -42,29 +42,29 @@ public: VirtualFileSystem(); ~VirtualFileSystem(); - KResult mount_root(FileSystem&); - KResult mount(FileSystem&, Custody& mount_point, int flags); - KResult bind_mount(Custody& source, Custody& mount_point, int flags); - KResult remount(Custody& mount_point, int new_flags); - KResult unmount(Inode& guest_inode); + ErrorOr mount_root(FileSystem&); + ErrorOr mount(FileSystem&, Custody& mount_point, int flags); + ErrorOr bind_mount(Custody& source, Custody& mount_point, int flags); + ErrorOr remount(Custody& mount_point, int new_flags); + ErrorOr unmount(Inode& guest_inode); - KResultOr> open(StringView path, int options, mode_t mode, Custody& base, Optional = {}); - KResultOr> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional = {}); - KResult mkdir(StringView path, mode_t mode, Custody& base); - KResult link(StringView old_path, StringView new_path, Custody& base); - KResult unlink(StringView path, Custody& base); - KResult symlink(StringView target, StringView linkpath, Custody& base); - KResult rmdir(StringView path, Custody& base); - KResult chmod(StringView path, mode_t, Custody& base); - KResult chmod(Custody&, mode_t); - KResult chown(StringView path, UserID, GroupID, Custody& base); - KResult chown(Custody&, UserID, GroupID); - KResult access(StringView path, int mode, Custody& base); - KResultOr lookup_metadata(StringView path, Custody& base, int options = 0); - KResult utime(StringView path, Custody& base, time_t atime, time_t mtime); - KResult rename(StringView oldpath, StringView newpath, Custody& base); - KResult mknod(StringView path, mode_t, dev_t, Custody& base); - KResultOr> open_directory(StringView path, Custody& base); + ErrorOr> open(StringView path, int options, mode_t mode, Custody& base, Optional = {}); + ErrorOr> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional = {}); + ErrorOr mkdir(StringView path, mode_t mode, Custody& base); + ErrorOr link(StringView old_path, StringView new_path, Custody& base); + ErrorOr unlink(StringView path, Custody& base); + ErrorOr symlink(StringView target, StringView linkpath, Custody& base); + ErrorOr rmdir(StringView path, Custody& base); + ErrorOr chmod(StringView path, mode_t, Custody& base); + ErrorOr chmod(Custody&, mode_t); + ErrorOr chown(StringView path, UserID, GroupID, Custody& base); + ErrorOr chown(Custody&, UserID, GroupID); + ErrorOr access(StringView path, int mode, Custody& base); + ErrorOr lookup_metadata(StringView path, Custody& base, int options = 0); + ErrorOr utime(StringView path, Custody& base, time_t atime, time_t mtime); + ErrorOr rename(StringView oldpath, StringView newpath, Custody& base); + ErrorOr mknod(StringView path, mode_t, dev_t, Custody& base); + ErrorOr> open_directory(StringView path, Custody& base); void for_each_mount(Function) const; @@ -73,19 +73,19 @@ public: static void sync(); Custody& root_custody(); - KResultOr> resolve_path(StringView path, Custody& base, RefPtr* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); - KResultOr> resolve_path_without_veil(StringView path, Custody& base, RefPtr* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); + ErrorOr> resolve_path(StringView path, Custody& base, RefPtr* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); + ErrorOr> resolve_path_without_veil(StringView path, Custody& base, RefPtr* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); private: friend class OpenFileDescription; UnveilNode const& find_matching_unveiled_path(StringView path); - KResult validate_path_against_process_veil(Custody const& path, int options); - KResult validate_path_against_process_veil(StringView path, int options); + ErrorOr validate_path_against_process_veil(Custody const& path, int options); + ErrorOr validate_path_against_process_veil(StringView path, int options); bool is_vfs_root(InodeIdentifier) const; - KResult traverse_directory_inode(Inode&, Function); + ErrorOr traverse_directory_inode(Inode&, Function); Mount* find_mount_for_host(InodeIdentifier); Mount* find_mount_for_guest(InodeIdentifier); diff --git a/Kernel/Firmware/ACPI/Parser.cpp b/Kernel/Firmware/ACPI/Parser.cpp index 7d6a632b40f..ef19a8661c8 100644 --- a/Kernel/Firmware/ACPI/Parser.cpp +++ b/Kernel/Firmware/ACPI/Parser.cpp @@ -39,19 +39,19 @@ UNMAP_AFTER_INIT NonnullRefPtr ACPISysFSComponent::create(St return adopt_ref(*new (nothrow) ACPISysFSComponent(name, paddr, table_size)); } -KResultOr ACPISysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const +ErrorOr ACPISysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const { auto blob = TRY(try_to_generate_buffer()); if ((size_t)offset >= blob->size()) - return KSuccess; + return 0; ssize_t nread = min(static_cast(blob->size() - offset), static_cast(count)); TRY(buffer.write(blob->data() + offset, nread)); return nread; } -KResultOr> ACPISysFSComponent::try_to_generate_buffer() const +ErrorOr> ACPISysFSComponent::try_to_generate_buffer() const { auto acpi_blob = Memory::map_typed((m_paddr), m_length); return KBuffer::try_create_with_bytes(Span { acpi_blob.ptr(), m_length }); @@ -64,7 +64,7 @@ UNMAP_AFTER_INIT ACPISysFSComponent::ACPISysFSComponent(String name, PhysicalAdd { } -UNMAP_AFTER_INIT KResultOr> ACPISysFSDirectory::try_create(FirmwareSysFSDirectory& firmware_directory) +UNMAP_AFTER_INIT ErrorOr> ACPISysFSDirectory::try_create(FirmwareSysFSDirectory& firmware_directory) { auto acpi_directory = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ACPISysFSDirectory(firmware_directory))); return acpi_directory; diff --git a/Kernel/Firmware/ACPI/Parser.h b/Kernel/Firmware/ACPI/Parser.h index 9c11b2100b1..903d6d5b495 100644 --- a/Kernel/Firmware/ACPI/Parser.h +++ b/Kernel/Firmware/ACPI/Parser.h @@ -22,7 +22,7 @@ namespace Kernel::ACPI { class ACPISysFSDirectory : public SysFSDirectory { public: - static KResultOr> try_create(FirmwareSysFSDirectory& firmware_directory); + static ErrorOr> try_create(FirmwareSysFSDirectory& firmware_directory); private: explicit ACPISysFSDirectory(FirmwareSysFSDirectory& firmware_directory); @@ -32,10 +32,10 @@ class ACPISysFSComponent : public SysFSComponent { public: static NonnullRefPtr create(String name, PhysicalAddress, size_t table_size); - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override; protected: - KResultOr> try_to_generate_buffer() const; + ErrorOr> try_to_generate_buffer() const; ACPISysFSComponent(String name, PhysicalAddress, size_t table_size); PhysicalAddress m_paddr; diff --git a/Kernel/Firmware/BIOS.cpp b/Kernel/Firmware/BIOS.cpp index 9bf9f5905c5..dd99a92b6d3 100644 --- a/Kernel/Firmware/BIOS.cpp +++ b/Kernel/Firmware/BIOS.cpp @@ -29,12 +29,12 @@ UNMAP_AFTER_INIT BIOSSysFSComponent::BIOSSysFSComponent(StringView name) { } -KResultOr BIOSSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const +ErrorOr BIOSSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const { auto blob = TRY(try_to_generate_buffer()); if ((size_t)offset >= blob->size()) - return KSuccess; + return 0; ssize_t nread = min(static_cast(blob->size() - offset), static_cast(count)); TRY(buffer.write(blob->data() + offset, nread)); @@ -48,7 +48,7 @@ UNMAP_AFTER_INIT DMIEntryPointExposedBlob::DMIEntryPointExposedBlob(PhysicalAddr { } -KResultOr> DMIEntryPointExposedBlob::try_to_generate_buffer() const +ErrorOr> DMIEntryPointExposedBlob::try_to_generate_buffer() const { auto dmi_blob = Memory::map_typed((m_dmi_entry_point), m_dmi_entry_point_length); return KBuffer::try_create_with_bytes(Span { dmi_blob.ptr(), m_dmi_entry_point_length }); @@ -66,7 +66,7 @@ UNMAP_AFTER_INIT SMBIOSExposedTable::SMBIOSExposedTable(PhysicalAddress smbios_s { } -KResultOr> SMBIOSExposedTable::try_to_generate_buffer() const +ErrorOr> SMBIOSExposedTable::try_to_generate_buffer() const { auto dmi_blob = Memory::map_typed((m_smbios_structure_table), m_smbios_structure_table_length); return KBuffer::try_create_with_bytes(Span { dmi_blob.ptr(), m_smbios_structure_table_length }); @@ -90,7 +90,7 @@ UNMAP_AFTER_INIT void BIOSSysFSDirectory::set_dmi_32_bit_entry_initialization_va m_smbios_structure_table_length = smbios_entry.ptr()->legacy_structure.smboios_table_length; } -UNMAP_AFTER_INIT KResultOr> BIOSSysFSDirectory::try_create(FirmwareSysFSDirectory& firmware_directory) +UNMAP_AFTER_INIT ErrorOr> BIOSSysFSDirectory::try_create(FirmwareSysFSDirectory& firmware_directory) { auto bios_directory = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) BIOSSysFSDirectory(firmware_directory))); bios_directory->create_components(); diff --git a/Kernel/Firmware/BIOS.h b/Kernel/Firmware/BIOS.h index 8766d4a7962..55618393f8b 100644 --- a/Kernel/Firmware/BIOS.h +++ b/Kernel/Firmware/BIOS.h @@ -62,10 +62,10 @@ Memory::MappedROM map_ebda(); class BIOSSysFSComponent : public SysFSComponent { public: - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override; + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override; protected: - virtual KResultOr> try_to_generate_buffer() const = 0; + virtual ErrorOr> try_to_generate_buffer() const = 0; explicit BIOSSysFSComponent(StringView name); }; @@ -75,7 +75,7 @@ public: private: DMIEntryPointExposedBlob(PhysicalAddress dmi_entry_point, size_t blob_size); - virtual KResultOr> try_to_generate_buffer() const override; + virtual ErrorOr> try_to_generate_buffer() const override; PhysicalAddress m_dmi_entry_point; size_t m_dmi_entry_point_length; }; @@ -86,7 +86,7 @@ public: private: SMBIOSExposedTable(PhysicalAddress dmi_entry_point, size_t blob_size); - virtual KResultOr> try_to_generate_buffer() const override; + virtual ErrorOr> try_to_generate_buffer() const override; PhysicalAddress m_smbios_structure_table; size_t m_smbios_structure_table_length; @@ -94,7 +94,7 @@ private: class BIOSSysFSDirectory : public SysFSDirectory { public: - static KResultOr> try_create(FirmwareSysFSDirectory&); + static ErrorOr> try_create(FirmwareSysFSDirectory&); void create_components(); diff --git a/Kernel/Firmware/PowerStateSwitch.cpp b/Kernel/Firmware/PowerStateSwitch.cpp index e0f9b53dbea..f76b415f07c 100644 --- a/Kernel/Firmware/PowerStateSwitch.cpp +++ b/Kernel/Firmware/PowerStateSwitch.cpp @@ -30,16 +30,16 @@ UNMAP_AFTER_INIT PowerStateSwitchNode::PowerStateSwitchNode(FirmwareSysFSDirecto { } -KResult PowerStateSwitchNode::truncate(u64 size) +ErrorOr PowerStateSwitchNode::truncate(u64 size) { // Note: This node doesn't store any useful data anyway, so we can safely // truncate this to zero (essentially ignoring the request without failing). if (size != 0) return EPERM; - return KSuccess; + return {}; } -KResultOr PowerStateSwitchNode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& data, OpenFileDescription*) +ErrorOr PowerStateSwitchNode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& data, OpenFileDescription*) { if (Checked::addition_would_overflow(offset, count)) return EOVERFLOW; diff --git a/Kernel/Firmware/PowerStateSwitch.h b/Kernel/Firmware/PowerStateSwitch.h index c35bd85ff43..e02db5138fd 100644 --- a/Kernel/Firmware/PowerStateSwitch.h +++ b/Kernel/Firmware/PowerStateSwitch.h @@ -24,9 +24,9 @@ class PowerStateSwitchNode final : public SysFSComponent { public: static NonnullRefPtr must_create(FirmwareSysFSDirectory&); virtual mode_t permissions() const override; - virtual KResultOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) override; - virtual KResult truncate(u64) override; - virtual KResult set_mtime(time_t) { return KSuccess; } + virtual ErrorOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) override; + virtual ErrorOr truncate(u64) override; + virtual ErrorOr set_mtime(time_t) { return {}; } private: PowerStateSwitchNode(FirmwareSysFSDirectory&); diff --git a/Kernel/Forward.h b/Kernel/Forward.h index 302b5f635ba..f80ca4d5ec9 100644 --- a/Kernel/Forward.h +++ b/Kernel/Forward.h @@ -32,7 +32,6 @@ class Inode; class InodeIdentifier; class InodeWatcher; class KBuffer; -class KResult; class LocalSocket; class Mutex; class MasterPTY; @@ -87,8 +86,6 @@ class VirtualRangeAllocator; class Spinlock; template class SpinlockLocker; -template -class KResultOr; struct InodeMetadata; struct TrapFrame; diff --git a/Kernel/GlobalProcessExposed.cpp b/Kernel/GlobalProcessExposed.cpp index d17d96c2f94..782a9405791 100644 --- a/Kernel/GlobalProcessExposed.cpp +++ b/Kernel/GlobalProcessExposed.cpp @@ -40,7 +40,7 @@ public: private: ProcFSAdapters(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; NetworkingManagement::the().for_each([&array](auto& adapter) { @@ -64,7 +64,7 @@ private: obj.add("mtu", adapter.mtu()); }); array.finish(); - return KSuccess; + return {}; } }; @@ -74,7 +74,7 @@ public: private: ProcFSARP(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; arp_table().for_each_shared([&](const auto& it) { @@ -83,7 +83,7 @@ private: obj.add("ip_address", it.key.to_string()); }); array.finish(); - return KSuccess; + return {}; } }; @@ -93,7 +93,7 @@ public: private: ProcFSTCP(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; TCPSocket::for_each([&array](auto& socket) { @@ -116,7 +116,7 @@ private: } }); array.finish(); - return KSuccess; + return {}; } }; @@ -126,7 +126,7 @@ public: private: ProcFSLocalNet(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; LocalSocket::for_each([&array](auto& socket) { @@ -140,7 +140,7 @@ private: obj.add("acceptor_gid", socket.acceptor_gid().value()); }); array.finish(); - return KSuccess; + return {}; } }; @@ -150,7 +150,7 @@ public: private: ProcFSUDP(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; UDPSocket::for_each([&array](auto& socket) { @@ -166,7 +166,7 @@ private: } }); array.finish(); - return KSuccess; + return {}; } }; @@ -348,10 +348,10 @@ public: private: ProcFSDiskUsage(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; - KResult result = KSuccess; + ErrorOr result; VirtualFileSystem::the().for_each_mount([&array, &result](auto& mount) { auto& fs = mount.guest_fs(); auto fs_object = array.add_object(); @@ -369,7 +369,7 @@ private: auto pseudo_path_or_error = static_cast(fs).file_description().pseudo_path(); if (pseudo_path_or_error.is_error()) { // We're probably out of memory and should not attempt to continue. - result = pseudo_path_or_error.error(); + result = pseudo_path_or_error.release_error(); return IterationDecision::Break; } fs_object.add("source", pseudo_path_or_error.value()->characters()); @@ -379,7 +379,7 @@ private: return IterationDecision::Continue; }); - if (result == KSuccess) + if (!result.is_error()) array.finish(); return result; } @@ -391,7 +391,7 @@ public: private: ProcFSMemoryStatus(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { InterruptDisabler disabler; @@ -418,7 +418,7 @@ private: json.add(String::formatted("{}_num_free", prefix), num_free); }); json.finish(); - return KSuccess; + return {}; } }; @@ -428,7 +428,7 @@ public: private: ProcFSSystemStatistics(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonObjectSerializer json { builder }; auto total_time_scheduled = Scheduler::get_total_time_scheduled(); @@ -441,7 +441,7 @@ private: }); json.add("idle_time", idle_time); json.finish(); - return KSuccess; + return {}; } }; @@ -451,7 +451,7 @@ public: private: ProcFSOverallProcesses(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonObjectSerializer json { builder }; @@ -549,7 +549,7 @@ private: json.add("total_time", total_time_scheduled.total); json.add("total_time_kernel", total_time_scheduled.total_kernel); } - return KSuccess; + return {}; } }; class ProcFSCPUInformation final : public ProcFSGlobalInformation { @@ -558,7 +558,7 @@ public: private: ProcFSCPUInformation(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; Processor::for_each( @@ -580,7 +580,7 @@ private: obj.add("brandstr", info.brandstr()); }); array.finish(); - return KSuccess; + return {}; } }; class ProcFSDmesg final : public ProcFSGlobalInformation { @@ -591,14 +591,14 @@ public: private: ProcFSDmesg(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { VERIFY(DeviceManagement::the().is_console_device_attached()); InterruptDisabler disabler; for (char ch : DeviceManagement::the().console_device().logbuffer()) { TRY(builder.append(ch)); } - return KSuccess; + return {}; } }; class ProcFSInterrupts final : public ProcFSGlobalInformation { @@ -607,7 +607,7 @@ public: private: ProcFSInterrupts(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; InterruptManagement::the().enumerate_interrupt_handlers([&array](GenericInterruptHandler& handler) { @@ -620,7 +620,7 @@ private: obj.add("call_count", (unsigned)handler.get_invoking_count()); }); array.finish(); - return KSuccess; + return {}; } }; class ProcFSKeymap final : public ProcFSGlobalInformation { @@ -629,12 +629,12 @@ public: private: ProcFSKeymap(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonObjectSerializer json { builder }; json.add("keymap", HIDManagement::the().keymap_name()); json.finish(); - return KSuccess; + return {}; } }; @@ -645,7 +645,7 @@ public: private: ProcFSPCI(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; PCI::enumerate([&array](PCI::DeviceIdentifier const& device_identifier) { @@ -663,7 +663,7 @@ private: obj.add("subsystem_vendor_id", device_identifier.subsystem_vendor_id().value()); }); array.finish(); - return KSuccess; + return {}; } }; @@ -673,7 +673,7 @@ public: private: ProcFSDevices(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { JsonArraySerializer array { builder }; DeviceManagement::the().for_each([&array](auto& device) { @@ -690,7 +690,7 @@ private: VERIFY_NOT_REACHED(); }); array.finish(); - return KSuccess; + return {}; } }; class ProcFSUptime final : public ProcFSGlobalInformation { @@ -699,7 +699,7 @@ public: private: ProcFSUptime(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { return builder.appendff("{}\n", TimeManagement::the().uptime_ms() / 1000); } @@ -710,11 +710,11 @@ public: private: ProcFSCommandLine(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { TRY(builder.append(kernel_command_line().string())); TRY(builder.append('\n')); - return KSuccess; + return {}; } }; class ProcFSSystemMode final : public ProcFSGlobalInformation { @@ -723,11 +723,11 @@ public: private: ProcFSSystemMode(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { TRY(builder.append(kernel_command_line().system_mode())); TRY(builder.append('\n')); - return KSuccess; + return {}; } }; @@ -739,12 +739,12 @@ public: private: ProcFSProfile(); - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { if (!g_global_perf_events) return ENOENT; TRY(g_global_perf_events->to_json(builder)); - return KSuccess; + return {}; } }; @@ -757,7 +757,7 @@ private: virtual mode_t required_mode() const override { return 0400; } - virtual KResult try_generate(KBufferBuilder& builder) override + virtual ErrorOr try_generate(KBufferBuilder& builder) override { if (!Process::current().is_superuser()) return EPERM; @@ -936,7 +936,7 @@ UNMAP_AFTER_INIT NonnullRefPtr ProcFSRootDirectory::must_cr return directory; } -KResult ProcFSRootDirectory::traverse_as_directory(unsigned fsid, Function callback) const +ErrorOr ProcFSRootDirectory::traverse_as_directory(unsigned fsid, Function callback) const { MutexLocker locker(ProcFSComponentRegistry::the().get_lock()); callback({ ".", { fsid, component_index() }, 0 }); @@ -953,15 +953,15 @@ KResult ProcFSRootDirectory::traverse_as_directory(unsigned fsid, Function> ProcFSRootDirectory::lookup(StringView name) +ErrorOr> ProcFSRootDirectory::lookup(StringView name) { auto maybe_candidate = ProcFSExposedDirectory::lookup(name); if (maybe_candidate.is_error()) { - if (maybe_candidate.error() != ENOENT) { - return maybe_candidate.error(); + if (maybe_candidate.error().code() != ENOENT) { + return maybe_candidate.release_error(); } } else { return maybe_candidate.release_value(); @@ -973,10 +973,9 @@ KResultOr> ProcFSRootDirectory::lookup(Str return ESRCH; auto actual_pid = pid.value(); - auto maybe_process = Process::from_pid(actual_pid); - if (maybe_process) { + if (auto maybe_process = Process::from_pid(actual_pid)) return maybe_process->procfs_traits(); - } + return ENOENT; } diff --git a/Kernel/Graphics/Bochs/GraphicsAdapter.cpp b/Kernel/Graphics/Bochs/GraphicsAdapter.cpp index 5bca91d48f6..9fd46427f1a 100644 --- a/Kernel/Graphics/Bochs/GraphicsAdapter.cpp +++ b/Kernel/Graphics/Bochs/GraphicsAdapter.cpp @@ -128,7 +128,7 @@ UNMAP_AFTER_INIT void BochsGraphicsAdapter::initialize_framebuffer_devices() { // FIXME: Find a better way to determine default resolution... m_framebuffer_device = FramebufferDevice::create(*this, PhysicalAddress(PCI::get_BAR0(pci_address()) & 0xfffffff0), 1024, 768, 1024 * sizeof(u32)); - // FIXME: Would be nice to be able to return a KResult here. + // FIXME: Would be nice to be able to return a ErrorOr here. VERIFY(!m_framebuffer_device->try_to_initialize().is_error()); } diff --git a/Kernel/Graphics/FramebufferDevice.cpp b/Kernel/Graphics/FramebufferDevice.cpp index fae6bf5f0d8..8fa82ab28eb 100644 --- a/Kernel/Graphics/FramebufferDevice.cpp +++ b/Kernel/Graphics/FramebufferDevice.cpp @@ -27,7 +27,7 @@ NonnullRefPtr FramebufferDevice::create(const GenericGraphics return framebuffer_device_or_error.release_value(); } -KResultOr FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) +ErrorOr FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) { SpinlockLocker lock(m_activation_lock); REQUIRE_PROMISE(video); @@ -92,7 +92,7 @@ void FramebufferDevice::activate_writes() m_graphical_writes_enabled = true; } -UNMAP_AFTER_INIT KResult FramebufferDevice::try_to_initialize() +UNMAP_AFTER_INIT ErrorOr FramebufferDevice::try_to_initialize() { // FIXME: Would be nice to be able to unify this with mmap above, but this // function is UNMAP_AFTER_INIT for the time being. @@ -101,7 +101,7 @@ UNMAP_AFTER_INIT KResult FramebufferDevice::try_to_initialize() m_swapped_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(Memory::page_round_up(framebuffer_length), AllocationStrategy::AllocateNow)); m_real_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_real_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer", Memory::Region::Access::ReadWrite)); m_swapped_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_swapped_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer Swap (Blank)", Memory::Region::Access::ReadWrite)); - return KSuccess; + return {}; } UNMAP_AFTER_INIT FramebufferDevice::FramebufferDevice(const GenericGraphicsAdapter& adapter, PhysicalAddress addr, size_t width, size_t height, size_t pitch) @@ -118,7 +118,7 @@ UNMAP_AFTER_INIT FramebufferDevice::FramebufferDevice(const GenericGraphicsAdapt dbgln("Framebuffer {}: address={}, pitch={}, width={}, height={}", minor(), addr, pitch, width, height); } -KResultOr FramebufferDevice::buffer_length(size_t head) const +ErrorOr FramebufferDevice::buffer_length(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -127,13 +127,13 @@ KResultOr FramebufferDevice::buffer_length(size_t head) const MutexLocker locker(m_resolution_lock); auto adapter = m_graphics_adapter.strong_ref(); if (!adapter) - return KResult(EIO); + return Error::from_errno(EIO); if (adapter->double_framebuffering_capable()) return m_framebuffer_pitch * m_framebuffer_height * 2; return m_framebuffer_pitch * m_framebuffer_height; } -KResultOr FramebufferDevice::pitch(size_t head) const +ErrorOr FramebufferDevice::pitch(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -142,7 +142,7 @@ KResultOr FramebufferDevice::pitch(size_t head) const MutexLocker locker(m_resolution_lock); return m_framebuffer_pitch; } -KResultOr FramebufferDevice::height(size_t head) const +ErrorOr FramebufferDevice::height(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -151,7 +151,7 @@ KResultOr FramebufferDevice::height(size_t head) const MutexLocker locker(m_resolution_lock); return m_framebuffer_height; } -KResultOr FramebufferDevice::width(size_t head) const +ErrorOr FramebufferDevice::width(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -160,7 +160,7 @@ KResultOr FramebufferDevice::width(size_t head) const MutexLocker locker(m_resolution_lock); return m_framebuffer_width; } -KResultOr FramebufferDevice::vertical_offset(size_t head) const +ErrorOr FramebufferDevice::vertical_offset(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -169,7 +169,7 @@ KResultOr FramebufferDevice::vertical_offset(size_t head) const MutexLocker locker(m_buffer_offset_lock); return m_y_offset; } -KResultOr FramebufferDevice::vertical_offseted(size_t head) const +ErrorOr FramebufferDevice::vertical_offseted(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -179,7 +179,7 @@ KResultOr FramebufferDevice::vertical_offseted(size_t head) const return m_y_offset == 0 ? 0 : 1; } -KResult FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t height, size_t) +ErrorOr FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t height, size_t) { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -189,17 +189,17 @@ KResult FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t MutexLocker resolution_locker(m_resolution_lock); auto adapter = m_graphics_adapter.strong_ref(); if (!adapter) - return KResult(EIO); + return Error::from_errno(EIO); auto result = adapter->try_to_set_resolution(0, width, height); - // FIXME: Find a better way to return here a KResult. + // FIXME: Find a better way to return here a ErrorOr. if (!result) - return KResult(ENOTSUP); + return Error::from_errno(ENOTSUP); m_framebuffer_width = width; m_framebuffer_height = height; m_framebuffer_pitch = width * sizeof(u32); - return KSuccess; + return {}; } -KResult FramebufferDevice::set_head_buffer(size_t head, bool second_buffer) +ErrorOr FramebufferDevice::set_head_buffer(size_t head, bool second_buffer) { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -208,30 +208,30 @@ KResult FramebufferDevice::set_head_buffer(size_t head, bool second_buffer) MutexLocker locker(m_buffer_offset_lock); auto adapter = m_graphics_adapter.strong_ref(); if (!adapter) - return KResult(EIO); + return Error::from_errno(EIO); if (second_buffer) { if (!adapter->set_y_offset(0, m_framebuffer_height)) { - // FIXME: Find a better KResult here. - return KResult(ENOTSUP); + // FIXME: Find a better ErrorOr here. + return Error::from_errno(ENOTSUP); } m_y_offset = m_framebuffer_height; } else { if (!adapter->set_y_offset(0, 0)) { - // FIXME: Find a better KResult here. - return KResult(ENOTSUP); + // FIXME: Find a better ErrorOr here. + return Error::from_errno(ENOTSUP); } m_y_offset = 0; } - return KSuccess; + return {}; } -KResult FramebufferDevice::flush_head_buffer(size_t) +ErrorOr FramebufferDevice::flush_head_buffer(size_t) { // Note: This FramebufferDevice class doesn't support flushing. // We take care to verify this at the GenericFramebufferDevice::ioctl method // so if we happen to accidentally reach this code, assert. VERIFY_NOT_REACHED(); } -KResult FramebufferDevice::flush_rectangle(size_t, FBRect const&) +ErrorOr FramebufferDevice::flush_rectangle(size_t, FBRect const&) { // Note: This FramebufferDevice class doesn't support partial flushing. // We take care to verify this at the GenericFramebufferDevice::ioctl method diff --git a/Kernel/Graphics/FramebufferDevice.h b/Kernel/Graphics/FramebufferDevice.h index 4ad52883674..5b3785617d1 100644 --- a/Kernel/Graphics/FramebufferDevice.h +++ b/Kernel/Graphics/FramebufferDevice.h @@ -24,29 +24,29 @@ class FramebufferDevice final : public GenericFramebufferDevice { public: static NonnullRefPtr create(const GenericGraphicsAdapter&, PhysicalAddress, size_t, size_t, size_t); - virtual KResultOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; + virtual ErrorOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; virtual void deactivate_writes() override; virtual void activate_writes() override; - virtual KResult try_to_initialize() override; + virtual ErrorOr try_to_initialize() override; virtual bool multihead_support() const override { return false; } virtual bool flushing_support() const override { return false; } virtual bool partial_flushing_support() const override { return false; } virtual size_t heads_count() const override { return 1; } - virtual KResultOr buffer_length(size_t head) const override; - virtual KResultOr pitch(size_t head) const override; - virtual KResultOr height(size_t head) const override; - virtual KResultOr width(size_t head) const override; - virtual KResultOr vertical_offset(size_t head) const override; - virtual KResultOr vertical_offseted(size_t head) const override; + virtual ErrorOr buffer_length(size_t head) const override; + virtual ErrorOr pitch(size_t head) const override; + virtual ErrorOr height(size_t head) const override; + virtual ErrorOr width(size_t head) const override; + virtual ErrorOr vertical_offset(size_t head) const override; + virtual ErrorOr vertical_offseted(size_t head) const override; private: - virtual KResult set_head_resolution(size_t head, size_t width, size_t height, size_t pitch) override; - virtual KResult set_head_buffer(size_t head, bool second_buffer) override; - virtual KResult flush_head_buffer(size_t head) override; - virtual KResult flush_rectangle(size_t head, FBRect const&) override; + virtual ErrorOr set_head_resolution(size_t head, size_t width, size_t height, size_t pitch) override; + virtual ErrorOr set_head_buffer(size_t head, bool second_buffer) override; + virtual ErrorOr flush_head_buffer(size_t head) override; + virtual ErrorOr flush_rectangle(size_t head, FBRect const&) override; FramebufferDevice(const GenericGraphicsAdapter&, PhysicalAddress, size_t, size_t, size_t); diff --git a/Kernel/Graphics/GenericFramebufferDevice.cpp b/Kernel/Graphics/GenericFramebufferDevice.cpp index 3ac1a0a0eef..b241a1216e7 100644 --- a/Kernel/Graphics/GenericFramebufferDevice.cpp +++ b/Kernel/Graphics/GenericFramebufferDevice.cpp @@ -22,16 +22,16 @@ namespace Kernel { -KResult GenericFramebufferDevice::verify_head_index(int head_index) const +ErrorOr GenericFramebufferDevice::verify_head_index(int head_index) const { if (head_index < 0) - return KResult(EINVAL); + return Error::from_errno(EINVAL); if (!multihead_support() && head_index > 0) - return KResult(ENOTSUP); - return KSuccess; + return Error::from_errno(ENOTSUP); + return {}; } -KResult GenericFramebufferDevice::ioctl(OpenFileDescription&, unsigned request, Userspace arg) +ErrorOr GenericFramebufferDevice::ioctl(OpenFileDescription&, unsigned request, Userspace arg) { REQUIRE_PROMISE(video); switch (request) { @@ -40,7 +40,7 @@ KResult GenericFramebufferDevice::ioctl(OpenFileDescription&, unsigned request, FBProperties properties; auto adapter = m_graphics_adapter.strong_ref(); if (!adapter) - return KResult(EIO); + return Error::from_errno(EIO); properties.multihead_support = multihead_support(); properties.flushing_support = flushing_support(); properties.doublebuffer_support = adapter->double_framebuffering_capable(); @@ -67,13 +67,13 @@ KResult GenericFramebufferDevice::ioctl(OpenFileDescription&, unsigned request, TRY(verify_head_index(head_resolution.head_index)); if (head_resolution.pitch < 0) - return KResult(EINVAL); + return Error::from_errno(EINVAL); if (head_resolution.width < 0) - return KResult(EINVAL); + return Error::from_errno(EINVAL); if (head_resolution.height < 0) - return KResult(EINVAL); + return Error::from_errno(EINVAL); TRY(set_head_resolution(head_resolution.head_index, head_resolution.width, head_resolution.height, head_resolution.pitch)); - return KSuccess; + return {}; } case FB_IOCTL_SET_HEAD_VERTICAL_OFFSET_BUFFER: { auto user_head_vertical_buffer_offset = static_ptr_cast(arg); @@ -82,9 +82,9 @@ KResult GenericFramebufferDevice::ioctl(OpenFileDescription&, unsigned request, TRY(verify_head_index(head_vertical_buffer_offset.head_index)); if (head_vertical_buffer_offset.offseted < 0 || head_vertical_buffer_offset.offseted > 1) - return KResult(EINVAL); + return Error::from_errno(EINVAL); TRY(set_head_buffer(head_vertical_buffer_offset.head_index, head_vertical_buffer_offset.offseted)); - return KSuccess; + return {}; } case FB_IOCTL_GET_HEAD_VERTICAL_OFFSET_BUFFER: { auto user_head_vertical_buffer_offset = static_ptr_cast(arg); @@ -97,12 +97,12 @@ KResult GenericFramebufferDevice::ioctl(OpenFileDescription&, unsigned request, } case FB_IOCTL_FLUSH_HEAD_BUFFERS: { if (!partial_flushing_support()) - return KResult(ENOTSUP); + return Error::from_errno(ENOTSUP); auto user_flush_rects = static_ptr_cast(arg); FBFlushRects flush_rects; TRY(copy_from_user(&flush_rects, user_flush_rects)); if (Checked::multiplication_would_overflow(flush_rects.count, sizeof(FBRect))) - return KResult(EFAULT); + return Error::from_errno(EFAULT); MutexLocker locker(m_flushing_lock); if (flush_rects.count > 0) { for (unsigned i = 0; i < flush_rects.count; i++) { @@ -111,11 +111,11 @@ KResult GenericFramebufferDevice::ioctl(OpenFileDescription&, unsigned request, TRY(flush_rectangle(flush_rects.buffer_index, user_dirty_rect)); } } - return KSuccess; + return {}; }; case FB_IOCTL_FLUSH_HEAD: { if (!flushing_support()) - return KResult(ENOTSUP); + return Error::from_errno(ENOTSUP); // Note: We accept a FBRect, but we only really care about the head_index value. auto user_rect = static_ptr_cast(arg); FBRect rect; @@ -123,7 +123,7 @@ KResult GenericFramebufferDevice::ioctl(OpenFileDescription&, unsigned request, TRY(verify_head_index(rect.head_index)); TRY(flush_head_buffer(rect.head_index)); - return KSuccess; + return {}; } default: return EINVAL; diff --git a/Kernel/Graphics/GenericFramebufferDevice.h b/Kernel/Graphics/GenericFramebufferDevice.h index 6fbce789dd5..cee22157326 100644 --- a/Kernel/Graphics/GenericFramebufferDevice.h +++ b/Kernel/Graphics/GenericFramebufferDevice.h @@ -6,9 +6,9 @@ #pragma once +#include #include #include -#include #include #include #include @@ -21,7 +21,7 @@ class GenericFramebufferDevice : public BlockDevice { friend class DeviceManagement; public: - virtual KResult try_to_initialize() = 0; + virtual ErrorOr try_to_initialize() = 0; virtual void deactivate_writes() = 0; virtual void activate_writes() = 0; @@ -29,8 +29,8 @@ public: virtual ~GenericFramebufferDevice() = default; // ^File - virtual KResultOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) = 0; - virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace arg) override final; + virtual ErrorOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) = 0; + virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg) override final; virtual StringView class_name() const override final { return "FramebufferDevice"sv; } private: @@ -38,28 +38,28 @@ private: virtual bool can_read(const OpenFileDescription&, size_t) const override final { return true; } virtual bool can_write(const OpenFileDescription&, size_t) const override final { return true; } virtual void start_request(AsyncBlockDeviceRequest& request) override final { request.complete(AsyncDeviceRequest::Failure); } - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override { return EINVAL; } - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override { return EINVAL; } + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; } protected: virtual bool multihead_support() const = 0; virtual bool flushing_support() const = 0; virtual bool partial_flushing_support() const = 0; virtual size_t heads_count() const = 0; - virtual KResultOr buffer_length(size_t head) const = 0; - virtual KResultOr pitch(size_t head) const = 0; - virtual KResultOr height(size_t head) const = 0; - virtual KResultOr width(size_t head) const = 0; - virtual KResultOr vertical_offset(size_t head) const = 0; - virtual KResultOr vertical_offseted(size_t head) const = 0; + virtual ErrorOr buffer_length(size_t head) const = 0; + virtual ErrorOr pitch(size_t head) const = 0; + virtual ErrorOr height(size_t head) const = 0; + virtual ErrorOr width(size_t head) const = 0; + virtual ErrorOr vertical_offset(size_t head) const = 0; + virtual ErrorOr vertical_offseted(size_t head) const = 0; - virtual KResult set_head_resolution(size_t head, size_t width, size_t height, size_t pitch) = 0; - virtual KResult set_head_buffer(size_t head, bool second_buffer) = 0; - virtual KResult flush_head_buffer(size_t head) = 0; + virtual ErrorOr set_head_resolution(size_t head, size_t width, size_t height, size_t pitch) = 0; + virtual ErrorOr set_head_buffer(size_t head, bool second_buffer) = 0; + virtual ErrorOr flush_head_buffer(size_t head) = 0; // FIXME: This method is too much specific to the VirtIO implementation (especially the buffer_index parameter) - virtual KResult flush_rectangle(size_t buffer_index, FBRect const&) = 0; + virtual ErrorOr flush_rectangle(size_t buffer_index, FBRect const&) = 0; - KResult verify_head_index(int head_index) const; + ErrorOr verify_head_index(int head_index) const; GenericFramebufferDevice(const GenericGraphicsAdapter&); mutable WeakPtr m_graphics_adapter; diff --git a/Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp b/Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp index 71d79dcf0ff..2ef73ab4c9c 100644 --- a/Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp +++ b/Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp @@ -643,7 +643,7 @@ void IntelNativeGraphicsAdapter::initialize_framebuffer_devices() VERIFY(m_framebuffer_height != 0); VERIFY(m_framebuffer_width != 0); m_framebuffer_device = FramebufferDevice::create(*this, address, m_framebuffer_width, m_framebuffer_height, m_framebuffer_pitch); - // FIXME: Would be nice to be able to return a KResult here. + // FIXME: Would be nice to be able to return a ErrorOr here. auto framebuffer_result = m_framebuffer_device->try_to_initialize(); VERIFY(!framebuffer_result.is_error()); } diff --git a/Kernel/Graphics/VGACompatibleAdapter.cpp b/Kernel/Graphics/VGACompatibleAdapter.cpp index 49500519151..d64da3897ba 100644 --- a/Kernel/Graphics/VGACompatibleAdapter.cpp +++ b/Kernel/Graphics/VGACompatibleAdapter.cpp @@ -32,7 +32,7 @@ UNMAP_AFTER_INIT void VGACompatibleAdapter::initialize_framebuffer_devices() VERIFY(m_framebuffer_height != 0); VERIFY(m_framebuffer_pitch != 0); m_framebuffer_device = FramebufferDevice::create(*this, m_framebuffer_address, m_framebuffer_width, m_framebuffer_height, m_framebuffer_pitch); - // FIXME: Would be nice to be able to return KResult here. + // FIXME: Would be nice to be able to return ErrorOr here. VERIFY(!m_framebuffer_device->try_to_initialize().is_error()); } diff --git a/Kernel/Graphics/VirtIOGPU/FramebufferDevice.cpp b/Kernel/Graphics/VirtIOGPU/FramebufferDevice.cpp index 0c20aa655f6..7072bc828d5 100644 --- a/Kernel/Graphics/VirtIOGPU/FramebufferDevice.cpp +++ b/Kernel/Graphics/VirtIOGPU/FramebufferDevice.cpp @@ -19,7 +19,7 @@ RefPtr FramebufferDevice::adapter() const return static_cast(*adapter); } -KResultOr FramebufferDevice::buffer_length(size_t head) const +ErrorOr FramebufferDevice::buffer_length(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -28,7 +28,7 @@ KResultOr FramebufferDevice::buffer_length(size_t head) const MutexLocker locker(m_resolution_lock); return display_info().rect.width * display_info().rect.height * 4; } -KResultOr FramebufferDevice::pitch(size_t head) const +ErrorOr FramebufferDevice::pitch(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -37,7 +37,7 @@ KResultOr FramebufferDevice::pitch(size_t head) const MutexLocker locker(m_resolution_lock); return display_info().rect.width * 4; } -KResultOr FramebufferDevice::height(size_t head) const +ErrorOr FramebufferDevice::height(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -46,7 +46,7 @@ KResultOr FramebufferDevice::height(size_t head) const MutexLocker locker(m_resolution_lock); return display_info().rect.height; } -KResultOr FramebufferDevice::width(size_t head) const +ErrorOr FramebufferDevice::width(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -55,7 +55,7 @@ KResultOr FramebufferDevice::width(size_t head) const MutexLocker locker(m_resolution_lock); return display_info().rect.width; } -KResultOr FramebufferDevice::vertical_offset(size_t head) const +ErrorOr FramebufferDevice::vertical_offset(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -63,7 +63,7 @@ KResultOr FramebufferDevice::vertical_offset(size_t head) const VERIFY(head == 0); return 0; } -KResultOr FramebufferDevice::vertical_offseted(size_t head) const +ErrorOr FramebufferDevice::vertical_offseted(size_t head) const { // Note: This FramebufferDevice class doesn't support multihead setup. // We take care to verify this at the GenericFramebufferDevice::ioctl method @@ -72,14 +72,14 @@ KResultOr FramebufferDevice::vertical_offseted(size_t head) const return false; } -KResult FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t height, size_t) +ErrorOr FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t height, size_t) { // Note: This class doesn't support multihead setup (yet!). // We take care to verify this at the GenericFramebufferDevice::ioctl method // so if we happen to accidentally have a value different than 0, assert. VERIFY(head == 0); if (width > MAX_VIRTIOGPU_RESOLUTION_WIDTH || height > MAX_VIRTIOGPU_RESOLUTION_HEIGHT) - return KResult(ENOTSUP); + return Error::from_errno(ENOTSUP); auto& info = display_info(); @@ -92,22 +92,22 @@ KResult FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t .height = (u32)height, }; - // FIXME: Would be nice to be able to return KResultOr here. + // FIXME: Would be nice to be able to return ErrorOr here. TRY(create_framebuffer()); - return KSuccess; + return {}; } -KResult FramebufferDevice::set_head_buffer(size_t, bool) +ErrorOr FramebufferDevice::set_head_buffer(size_t, bool) { - return KResult(ENOTSUP); + return Error::from_errno(ENOTSUP); } -KResult FramebufferDevice::flush_head_buffer(size_t) +ErrorOr FramebufferDevice::flush_head_buffer(size_t) { // Note: This class doesn't support flushing. // We take care to verify this at the GenericFramebufferDevice::ioctl method // so if we happen to accidentally reach this code, assert. VERIFY_NOT_REACHED(); } -KResult FramebufferDevice::flush_rectangle(size_t buffer_index, FBRect const& rect) +ErrorOr FramebufferDevice::flush_rectangle(size_t buffer_index, FBRect const& rect) { MutexLocker locker(adapter()->operation_lock()); Protocol::Rect dirty_rect { @@ -116,9 +116,9 @@ KResult FramebufferDevice::flush_rectangle(size_t buffer_index, FBRect const& re .width = rect.width, .height = rect.height }; - // FIXME: Find a better KResult here. + // FIXME: Find a better ErrorOr here. if (!m_are_writes_active) - return KResult(EIO); + return Error::from_errno(EIO); auto& buffer = buffer_from_index(buffer_index); transfer_framebuffer_data_to_host(dirty_rect, buffer); if (&buffer == m_current_buffer) { @@ -137,7 +137,7 @@ KResult FramebufferDevice::flush_rectangle(size_t buffer_index, FBRect const& re buffer.dirty_rect.height = max(current_dirty_bottom, dirty_rect.y + dirty_rect.height) - buffer.dirty_rect.y; } } - return KSuccess; + return {}; } FramebufferDevice::FramebufferDevice(GraphicsAdapter const& adapter, ScanoutID scanout) @@ -155,7 +155,7 @@ FramebufferDevice::~FramebufferDevice() { } -KResult FramebufferDevice::create_framebuffer() +ErrorOr FramebufferDevice::create_framebuffer() { // First delete any existing framebuffers to free the memory first m_framebuffer = nullptr; @@ -179,7 +179,7 @@ KResult FramebufferDevice::create_framebuffer() create_buffer(m_main_buffer, 0, m_buffer_size); create_buffer(m_back_buffer, m_buffer_size, m_buffer_size); - return KSuccess; + return {}; } void FramebufferDevice::create_buffer(Buffer& buffer, size_t framebuffer_offset, size_t framebuffer_size) @@ -255,7 +255,7 @@ void FramebufferDevice::set_buffer(int buffer_index) buffer.dirty_rect = {}; } -KResultOr FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) +ErrorOr FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) { REQUIRE_PROMISE(video); if (!shared) @@ -295,7 +295,7 @@ void FramebufferDevice::deactivate_writes() if (m_userspace_mmap_region) { auto* region = m_userspace_mmap_region.unsafe_ptr(); auto maybe_vm_object = m_framebuffer_sink_vmobject->try_clone(); - // FIXME: Would be nice to be able to return a KResult here. + // FIXME: Would be nice to be able to return a ErrorOr here. VERIFY(!maybe_vm_object.is_error()); region->set_vmobject(maybe_vm_object.release_value()); region->remap(); diff --git a/Kernel/Graphics/VirtIOGPU/FramebufferDevice.h b/Kernel/Graphics/VirtIOGPU/FramebufferDevice.h index b643691fb22..fd45d84e731 100644 --- a/Kernel/Graphics/VirtIOGPU/FramebufferDevice.h +++ b/Kernel/Graphics/VirtIOGPU/FramebufferDevice.h @@ -28,7 +28,7 @@ public: FramebufferDevice(GraphicsAdapter const&, ScanoutID); virtual ~FramebufferDevice() override; - virtual KResult try_to_initialize() override { return KSuccess; } + virtual ErrorOr try_to_initialize() override { return {}; } virtual void deactivate_writes(); virtual void activate_writes(); @@ -50,17 +50,17 @@ private: virtual bool flushing_support() const override { return false; } virtual bool partial_flushing_support() const override { return true; } virtual size_t heads_count() const override { return 1; } - virtual KResultOr buffer_length(size_t head) const override; - virtual KResultOr pitch(size_t head) const override; - virtual KResultOr height(size_t head) const override; - virtual KResultOr width(size_t head) const override; - virtual KResultOr vertical_offset(size_t head) const override; - virtual KResultOr vertical_offseted(size_t head) const override; + virtual ErrorOr buffer_length(size_t head) const override; + virtual ErrorOr pitch(size_t head) const override; + virtual ErrorOr height(size_t head) const override; + virtual ErrorOr width(size_t head) const override; + virtual ErrorOr vertical_offset(size_t head) const override; + virtual ErrorOr vertical_offseted(size_t head) const override; - virtual KResult set_head_resolution(size_t head, size_t width, size_t height, size_t pitch) override; - virtual KResult set_head_buffer(size_t head, bool second_buffer) override; - virtual KResult flush_head_buffer(size_t head) override; - virtual KResult flush_rectangle(size_t head, FBRect const&) override; + virtual ErrorOr set_head_resolution(size_t head, size_t width, size_t height, size_t pitch) override; + virtual ErrorOr set_head_buffer(size_t head, bool second_buffer) override; + virtual ErrorOr flush_head_buffer(size_t head) override; + virtual ErrorOr flush_rectangle(size_t head, FBRect const&) override; void flush_dirty_window(Protocol::Rect const&, Buffer&); void transfer_framebuffer_data_to_host(Protocol::Rect const&, Buffer&); @@ -73,11 +73,11 @@ private: void clear_to_black(Buffer&); - KResult create_framebuffer(); + ErrorOr create_framebuffer(); void create_buffer(Buffer&, size_t, size_t); void set_buffer(int); - virtual KResultOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; + virtual ErrorOr mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override; static bool is_valid_buffer_index(int buffer_index) { diff --git a/Kernel/Interrupts/APIC.cpp b/Kernel/Interrupts/APIC.cpp index 960fa6cf2b0..b7ad320c24e 100644 --- a/Kernel/Interrupts/APIC.cpp +++ b/Kernel/Interrupts/APIC.cpp @@ -309,7 +309,7 @@ UNMAP_AFTER_INIT bool APIC::init_bsp() UNMAP_AFTER_INIT static NonnullOwnPtr create_identity_mapped_region(PhysicalAddress paddr, size_t size) { auto maybe_vmobject = Memory::AnonymousVMObject::try_create_for_physical_range(paddr, size); - // FIXME: Would be nice to be able to return a KResultOr from here. + // FIXME: Would be nice to be able to return a ErrorOr from here. VERIFY(!maybe_vmobject.is_error()); auto region_or_error = MM.allocate_kernel_region_with_vmobject( diff --git a/Kernel/KBuffer.h b/Kernel/KBuffer.h index 2a564101781..0b4a6991743 100644 --- a/Kernel/KBuffer.h +++ b/Kernel/KBuffer.h @@ -24,13 +24,13 @@ namespace Kernel { class [[nodiscard]] KBuffer { public: - static KResultOr> try_create_with_size(size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) + static ErrorOr> try_create_with_size(size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) { auto region = TRY(MM.allocate_kernel_region(Memory::page_round_up(size), name, access, strategy)); return TRY(adopt_nonnull_own_or_enomem(new (nothrow) KBuffer { size, move(region) })); } - static KResultOr> try_create_with_bytes(ReadonlyBytes bytes, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) + static ErrorOr> try_create_with_bytes(ReadonlyBytes bytes, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) { auto buffer = TRY(try_create_with_size(bytes.size(), access, name, strategy)); memcpy(buffer->data(), bytes.data(), bytes.size()); diff --git a/Kernel/KBufferBuilder.cpp b/Kernel/KBufferBuilder.cpp index 4a2f9d1da06..cc944e93031 100644 --- a/Kernel/KBufferBuilder.cpp +++ b/Kernel/KBufferBuilder.cpp @@ -46,7 +46,7 @@ OwnPtr KBufferBuilder::build() return move(m_buffer); } -KResultOr KBufferBuilder::try_create() +ErrorOr KBufferBuilder::try_create() { auto buffer = TRY(KBuffer::try_create_with_size(4 * MiB, Memory::Region::Access::ReadWrite)); return KBufferBuilder { move(buffer) }; @@ -57,47 +57,47 @@ KBufferBuilder::KBufferBuilder(NonnullOwnPtr buffer) { } -KResult KBufferBuilder::append_bytes(ReadonlyBytes bytes) +ErrorOr KBufferBuilder::append_bytes(ReadonlyBytes bytes) { if (!check_expand(bytes.size())) return ENOMEM; memcpy(insertion_ptr(), bytes.data(), bytes.size()); m_size += bytes.size(); - return KSuccess; + return {}; } -KResult KBufferBuilder::append(const StringView& str) +ErrorOr KBufferBuilder::append(const StringView& str) { if (str.is_empty()) - return KSuccess; + return {}; if (!check_expand(str.length())) return ENOMEM; memcpy(insertion_ptr(), str.characters_without_null_termination(), str.length()); m_size += str.length(); - return KSuccess; + return {}; } -KResult KBufferBuilder::append(const char* characters, int length) +ErrorOr KBufferBuilder::append(const char* characters, int length) { if (!length) - return KSuccess; + return {}; if (!check_expand(length)) return ENOMEM; memcpy(insertion_ptr(), characters, length); m_size += length; - return KSuccess; + return {}; } -KResult KBufferBuilder::append(char ch) +ErrorOr KBufferBuilder::append(char ch) { if (!check_expand(1)) return ENOMEM; insertion_ptr()[0] = ch; m_size += 1; - return KSuccess; + return {}; } -KResult KBufferBuilder::append_escaped_for_json(const StringView& string) +ErrorOr KBufferBuilder::append_escaped_for_json(const StringView& string) { for (auto ch : string) { switch (ch) { @@ -123,7 +123,7 @@ KResult KBufferBuilder::append_escaped_for_json(const StringView& string) TRY(append(ch)); } } - return KSuccess; + return {}; } } diff --git a/Kernel/KBufferBuilder.h b/Kernel/KBufferBuilder.h index 06be3c0d6de..e46f4bc462f 100644 --- a/Kernel/KBufferBuilder.h +++ b/Kernel/KBufferBuilder.h @@ -18,21 +18,21 @@ class KBufferBuilder { public: using OutputType = KBuffer; - static KResultOr try_create(); + static ErrorOr try_create(); KBufferBuilder(KBufferBuilder&&) = default; KBufferBuilder& operator=(KBufferBuilder&&) = default; ~KBufferBuilder() = default; - KResult append(const StringView&); - KResult append(char); - KResult append(const char*, int); + ErrorOr append(const StringView&); + ErrorOr append(char); + ErrorOr append(const char*, int); - KResult append_escaped_for_json(const StringView&); - KResult append_bytes(ReadonlyBytes); + ErrorOr append_escaped_for_json(const StringView&); + ErrorOr append_bytes(ReadonlyBytes); template - KResult appendff(CheckedFormatString&& fmtstr, const Parameters&... parameters) + ErrorOr appendff(CheckedFormatString&& fmtstr, const Parameters&... parameters) { // FIXME: This really not ideal, but vformat expects StringBuilder. StringBuilder builder; diff --git a/Kernel/KLexicalPath.cpp b/Kernel/KLexicalPath.cpp index 1cdd9fb35fc..f3ed1b13354 100644 --- a/Kernel/KLexicalPath.cpp +++ b/Kernel/KLexicalPath.cpp @@ -62,7 +62,7 @@ Vector parts(StringView const& path) return path.split_view('/'); } -KResultOr> try_join(StringView const& first, StringView const& second) +ErrorOr> try_join(StringView const& first, StringView const& second) { VERIFY(is_canonical(first)); VERIFY(is_canonical(second)); diff --git a/Kernel/KLexicalPath.h b/Kernel/KLexicalPath.h index a8564fd81c2..e2cdc06df87 100644 --- a/Kernel/KLexicalPath.h +++ b/Kernel/KLexicalPath.h @@ -17,6 +17,6 @@ StringView basename(StringView const&); StringView dirname(StringView const&); Vector parts(StringView const&); -KResultOr> try_join(StringView const&, StringView const&); +ErrorOr> try_join(StringView const&, StringView const&); } diff --git a/Kernel/KString.cpp b/Kernel/KString.cpp index 8a6582c7cd1..d20fd6e374c 100644 --- a/Kernel/KString.cpp +++ b/Kernel/KString.cpp @@ -10,7 +10,7 @@ extern bool g_in_early_boot; namespace Kernel { -KResultOr> KString::try_create(StringView string) +ErrorOr> KString::try_create(StringView string) { char* characters = nullptr; size_t length = string.length(); @@ -28,7 +28,7 @@ NonnullOwnPtr KString::must_create(StringView string) return KString::try_create(string).release_value(); } -KResultOr> KString::try_create_uninitialized(size_t length, char*& characters) +ErrorOr> KString::try_create_uninitialized(size_t length, char*& characters) { size_t allocation_size = sizeof(KString) + (sizeof(char) * length) + sizeof(char); auto* slot = kmalloc(allocation_size); @@ -46,7 +46,7 @@ NonnullOwnPtr KString::must_create_uninitialized(size_t length, char*& return KString::try_create_uninitialized(length, characters).release_value(); } -KResultOr> KString::try_clone() const +ErrorOr> KString::try_clone() const { return try_create(view()); } diff --git a/Kernel/KString.h b/Kernel/KString.h index 44fc324d603..d92f400df26 100644 --- a/Kernel/KString.h +++ b/Kernel/KString.h @@ -16,14 +16,14 @@ class KString { AK_MAKE_NONMOVABLE(KString); public: - [[nodiscard]] static KResultOr> try_create_uninitialized(size_t, char*&); + [[nodiscard]] static ErrorOr> try_create_uninitialized(size_t, char*&); [[nodiscard]] static NonnullOwnPtr must_create_uninitialized(size_t, char*&); - [[nodiscard]] static KResultOr> try_create(StringView); + [[nodiscard]] static ErrorOr> try_create(StringView); [[nodiscard]] static NonnullOwnPtr must_create(StringView); void operator delete(void*); - KResultOr> try_clone() const; + ErrorOr> try_clone() const; [[nodiscard]] bool is_empty() const { return m_length == 0; } [[nodiscard]] size_t length() const { return m_length; } diff --git a/Kernel/Library/ThreadSafeRefPtr.h b/Kernel/Library/ThreadSafeRefPtr.h index 50971a53d95..8253c7b8bb7 100644 --- a/Kernel/Library/ThreadSafeRefPtr.h +++ b/Kernel/Library/ThreadSafeRefPtr.h @@ -8,13 +8,13 @@ #include #include +#include #include #include #include #include #include #ifdef KERNEL -# include # include # include #endif @@ -498,16 +498,14 @@ inline RefPtr try_make_ref_counted(Args&&... args) return adopt_ref_if_nonnull(new (nothrow) T { forward(args)... }); } -#ifdef KERNEL template -inline Kernel::KResultOr> adopt_nonnull_ref_or_enomem(T* object) +inline ErrorOr> adopt_nonnull_ref_or_enomem(T* object) { auto result = adopt_ref_if_nonnull(object); if (!result) - return ENOMEM; + return Error::from_errno(ENOMEM); return result.release_nonnull(); } -#endif } diff --git a/Kernel/Memory/AddressSpace.cpp b/Kernel/Memory/AddressSpace.cpp index 55795179109..f0ce03dc243 100644 --- a/Kernel/Memory/AddressSpace.cpp +++ b/Kernel/Memory/AddressSpace.cpp @@ -15,7 +15,7 @@ namespace Kernel::Memory { -KResultOr> AddressSpace::try_create(AddressSpace const* parent) +ErrorOr> AddressSpace::try_create(AddressSpace const* parent) { auto page_directory = TRY(PageDirectory::try_create_for_userspace(parent ? &parent->page_directory().range_allocator() : nullptr)); auto space = TRY(adopt_nonnull_own_or_enomem(new (nothrow) AddressSpace(page_directory))); @@ -32,7 +32,7 @@ AddressSpace::~AddressSpace() { } -KResult AddressSpace::unmap_mmap_range(VirtualAddress addr, size_t size) +ErrorOr AddressSpace::unmap_mmap_range(VirtualAddress addr, size_t size) { if (!size) return EINVAL; @@ -49,7 +49,7 @@ KResult AddressSpace::unmap_mmap_range(VirtualAddress addr, size_t size) PerformanceManager::add_unmap_perf_event(Process::current(), whole_region->range()); deallocate_region(*whole_region); - return KSuccess; + return {}; } if (auto* old_region = find_region_containing(range_to_unmap)) { @@ -77,13 +77,13 @@ KResult AddressSpace::unmap_mmap_range(VirtualAddress addr, size_t size) PerformanceManager::add_unmap_perf_event(Process::current(), range_to_unmap); - return KSuccess; + return {}; } // Try again while checking multiple regions at a time. auto const& regions = find_regions_intersecting(range_to_unmap); if (regions.is_empty()) - return KSuccess; + return {}; // Check if any of the regions is not mmap'ed, to not accidentally // error out with just half a region map left. @@ -126,10 +126,10 @@ KResult AddressSpace::unmap_mmap_range(VirtualAddress addr, size_t size) PerformanceManager::add_unmap_perf_event(Process::current(), range_to_unmap); - return KSuccess; + return {}; } -KResultOr AddressSpace::try_allocate_range(VirtualAddress vaddr, size_t size, size_t alignment) +ErrorOr AddressSpace::try_allocate_range(VirtualAddress vaddr, size_t size, size_t alignment) { vaddr.mask(PAGE_MASK); size = page_round_up(size); @@ -138,7 +138,7 @@ KResultOr AddressSpace::try_allocate_range(VirtualAddress vaddr, s return page_directory().range_allocator().try_allocate_specific(vaddr, size); } -KResultOr AddressSpace::try_allocate_split_region(Region const& source_region, VirtualRange const& range, size_t offset_in_vmobject) +ErrorOr AddressSpace::try_allocate_split_region(Region const& source_region, VirtualRange const& range, size_t offset_in_vmobject) { OwnPtr region_name; if (!source_region.name().is_null()) @@ -158,7 +158,7 @@ KResultOr AddressSpace::try_allocate_split_region(Region const& source_ return region; } -KResultOr AddressSpace::allocate_region(VirtualRange const& range, StringView name, int prot, AllocationStrategy strategy) +ErrorOr AddressSpace::allocate_region(VirtualRange const& range, StringView name, int prot, AllocationStrategy strategy) { VERIFY(range.is_valid()); OwnPtr region_name; @@ -170,7 +170,7 @@ KResultOr AddressSpace::allocate_region(VirtualRange const& range, Stri return add_region(move(region)); } -KResultOr AddressSpace::allocate_region_with_vmobject(VirtualRange const& range, NonnullRefPtr vmobject, size_t offset_in_vmobject, StringView name, int prot, bool shared) +ErrorOr AddressSpace::allocate_region_with_vmobject(VirtualRange const& range, NonnullRefPtr vmobject, size_t offset_in_vmobject, StringView name, int prot, bool shared) { VERIFY(range.is_valid()); size_t end_in_vmobject = offset_in_vmobject + range.size(); @@ -264,7 +264,7 @@ Vector AddressSpace::find_regions_intersecting(VirtualRange const& rang return regions; } -KResultOr AddressSpace::add_region(NonnullOwnPtr region) +ErrorOr AddressSpace::add_region(NonnullOwnPtr region) { auto* ptr = region.ptr(); SpinlockLocker lock(m_lock); @@ -274,13 +274,13 @@ KResultOr AddressSpace::add_region(NonnullOwnPtr region) } // Carve out a virtual address range from a region and return the two regions on either side -KResultOr> AddressSpace::try_split_region_around_range(const Region& source_region, VirtualRange const& desired_range) +ErrorOr> AddressSpace::try_split_region_around_range(const Region& source_region, VirtualRange const& desired_range) { VirtualRange old_region_range = source_region.range(); auto remaining_ranges_after_unmap = old_region_range.carve(desired_range); VERIFY(!remaining_ranges_after_unmap.is_empty()); - auto try_make_replacement_region = [&](VirtualRange const& new_range) -> KResultOr { + auto try_make_replacement_region = [&](VirtualRange const& new_range) -> ErrorOr { VERIFY(old_region_range.contains(new_range)); size_t new_range_offset_in_vmobject = source_region.offset_in_vmobject() + (new_range.base().get() - old_region_range.base().get()); return try_allocate_split_region(source_region, new_range, new_range_offset_in_vmobject); diff --git a/Kernel/Memory/AddressSpace.h b/Kernel/Memory/AddressSpace.h index 9617a54b1d8..5a96e0abb0c 100644 --- a/Kernel/Memory/AddressSpace.h +++ b/Kernel/Memory/AddressSpace.h @@ -18,13 +18,13 @@ namespace Kernel::Memory { class AddressSpace { public: - static KResultOr> try_create(AddressSpace const* parent); + static ErrorOr> try_create(AddressSpace const* parent); ~AddressSpace(); PageDirectory& page_directory() { return *m_page_directory; } const PageDirectory& page_directory() const { return *m_page_directory; } - KResultOr add_region(NonnullOwnPtr); + ErrorOr add_region(NonnullOwnPtr); size_t region_count() const { return m_regions.size(); } @@ -33,17 +33,17 @@ public: void dump_regions(); - KResult unmap_mmap_range(VirtualAddress, size_t); + ErrorOr unmap_mmap_range(VirtualAddress, size_t); - KResultOr try_allocate_range(VirtualAddress, size_t, size_t alignment = PAGE_SIZE); + ErrorOr try_allocate_range(VirtualAddress, size_t, size_t alignment = PAGE_SIZE); - KResultOr allocate_region_with_vmobject(VirtualRange const&, NonnullRefPtr, size_t offset_in_vmobject, StringView name, int prot, bool shared); - KResultOr allocate_region(VirtualRange const&, StringView name, int prot = PROT_READ | PROT_WRITE, AllocationStrategy strategy = AllocationStrategy::Reserve); + ErrorOr allocate_region_with_vmobject(VirtualRange const&, NonnullRefPtr, size_t offset_in_vmobject, StringView name, int prot, bool shared); + ErrorOr allocate_region(VirtualRange const&, StringView name, int prot = PROT_READ | PROT_WRITE, AllocationStrategy strategy = AllocationStrategy::Reserve); void deallocate_region(Region& region); NonnullOwnPtr take_region(Region& region); - KResultOr try_allocate_split_region(Region const& source_region, VirtualRange const&, size_t offset_in_vmobject); - KResultOr> try_split_region_around_range(Region const& source_region, VirtualRange const&); + ErrorOr try_allocate_split_region(Region const& source_region, VirtualRange const&, size_t offset_in_vmobject); + ErrorOr> try_split_region_around_range(Region const& source_region, VirtualRange const&); Region* find_region_from_range(VirtualRange const&); Region* find_region_containing(VirtualRange const&); diff --git a/Kernel/Memory/AnonymousVMObject.cpp b/Kernel/Memory/AnonymousVMObject.cpp index 582f3192ee5..9650f4b99e3 100644 --- a/Kernel/Memory/AnonymousVMObject.cpp +++ b/Kernel/Memory/AnonymousVMObject.cpp @@ -13,7 +13,7 @@ namespace Kernel::Memory { -KResultOr> AnonymousVMObject::try_clone() +ErrorOr> AnonymousVMObject::try_clone() { // We need to acquire our lock so we copy a sane state SpinlockLocker lock(m_lock); @@ -66,7 +66,7 @@ KResultOr> AnonymousVMObject::try_clone() return clone; } -KResultOr> AnonymousVMObject::try_create_with_size(size_t size, AllocationStrategy strategy) +ErrorOr> AnonymousVMObject::try_create_with_size(size_t size, AllocationStrategy strategy) { Optional committed_pages; if (strategy == AllocationStrategy::Reserve || strategy == AllocationStrategy::AllocateNow) { @@ -76,7 +76,7 @@ KResultOr> AnonymousVMObject::try_create_with_s return adopt_nonnull_ref_or_enomem(new (nothrow) AnonymousVMObject(size, strategy, move(committed_pages))); } -KResultOr> AnonymousVMObject::try_create_physically_contiguous_with_size(size_t size) +ErrorOr> AnonymousVMObject::try_create_physically_contiguous_with_size(size_t size) { auto contiguous_physical_pages = MM.allocate_contiguous_supervisor_physical_pages(size); if (contiguous_physical_pages.is_empty()) @@ -85,7 +85,7 @@ KResultOr> AnonymousVMObject::try_create_physic return adopt_nonnull_ref_or_enomem(new (nothrow) AnonymousVMObject(contiguous_physical_pages.span())); } -KResultOr> AnonymousVMObject::try_create_purgeable_with_size(size_t size, AllocationStrategy strategy) +ErrorOr> AnonymousVMObject::try_create_purgeable_with_size(size_t size, AllocationStrategy strategy) { Optional committed_pages; if (strategy == AllocationStrategy::Reserve || strategy == AllocationStrategy::AllocateNow) { @@ -97,12 +97,12 @@ KResultOr> AnonymousVMObject::try_create_purgea return vmobject; } -KResultOr> AnonymousVMObject::try_create_with_physical_pages(Span> physical_pages) +ErrorOr> AnonymousVMObject::try_create_with_physical_pages(Span> physical_pages) { return adopt_nonnull_ref_or_enomem(new (nothrow) AnonymousVMObject(physical_pages)); } -KResultOr> AnonymousVMObject::try_create_for_physical_range(PhysicalAddress paddr, size_t size) +ErrorOr> AnonymousVMObject::try_create_for_physical_range(PhysicalAddress paddr, size_t size) { if (paddr.offset(size) < paddr) { dbgln("Shenanigans! try_create_for_physical_range({}, {}) would wrap around", paddr, size); @@ -182,7 +182,7 @@ size_t AnonymousVMObject::purge() return total_pages_purged; } -KResult AnonymousVMObject::set_volatile(bool is_volatile, bool& was_purged) +ErrorOr AnonymousVMObject::set_volatile(bool is_volatile, bool& was_purged) { VERIFY(is_purgeable()); @@ -190,7 +190,7 @@ KResult AnonymousVMObject::set_volatile(bool is_volatile, bool& was_purged) was_purged = m_was_purged; if (m_volatile == is_volatile) - return KSuccess; + return {}; if (is_volatile) { // When a VMObject is made volatile, it gives up all of its committed memory. @@ -210,7 +210,7 @@ KResult AnonymousVMObject::set_volatile(bool is_volatile, bool& was_purged) m_was_purged = false; for_each_region([&](auto& region) { region.remap(); }); - return KSuccess; + return {}; } // When a VMObject is made non-volatile, we try to commit however many pages are not currently available. // If that fails, we return false to indicate that memory allocation failed. @@ -223,7 +223,7 @@ KResult AnonymousVMObject::set_volatile(bool is_volatile, bool& was_purged) if (!committed_pages_needed) { m_volatile = false; - return KSuccess; + return {}; } m_unused_committed_pages = TRY(MM.commit_user_physical_pages(committed_pages_needed)); @@ -236,7 +236,7 @@ KResult AnonymousVMObject::set_volatile(bool is_volatile, bool& was_purged) m_volatile = false; m_was_purged = false; for_each_region([&](auto& region) { region.remap(); }); - return KSuccess; + return {}; } NonnullRefPtr AnonymousVMObject::allocate_committed_page(Badge) diff --git a/Kernel/Memory/AnonymousVMObject.h b/Kernel/Memory/AnonymousVMObject.h index f0b308a3be8..b645b4d1385 100644 --- a/Kernel/Memory/AnonymousVMObject.h +++ b/Kernel/Memory/AnonymousVMObject.h @@ -18,12 +18,12 @@ class AnonymousVMObject final : public VMObject { public: virtual ~AnonymousVMObject() override; - static KResultOr> try_create_with_size(size_t, AllocationStrategy); - static KResultOr> try_create_for_physical_range(PhysicalAddress paddr, size_t size); - static KResultOr> try_create_with_physical_pages(Span>); - static KResultOr> try_create_purgeable_with_size(size_t, AllocationStrategy); - static KResultOr> try_create_physically_contiguous_with_size(size_t); - virtual KResultOr> try_clone() override; + static ErrorOr> try_create_with_size(size_t, AllocationStrategy); + static ErrorOr> try_create_for_physical_range(PhysicalAddress paddr, size_t size); + static ErrorOr> try_create_with_physical_pages(Span>); + static ErrorOr> try_create_purgeable_with_size(size_t, AllocationStrategy); + static ErrorOr> try_create_physically_contiguous_with_size(size_t); + virtual ErrorOr> try_clone() override; [[nodiscard]] NonnullRefPtr allocate_committed_page(Badge); PageFaultResponse handle_cow_fault(size_t, VirtualAddress); @@ -34,7 +34,7 @@ public: bool is_purgeable() const { return m_purgeable; } bool is_volatile() const { return m_volatile; } - KResult set_volatile(bool is_volatile, bool& was_purged); + ErrorOr set_volatile(bool is_volatile, bool& was_purged); size_t purge(); diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index 377cea04caf..2a5afa36887 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -705,7 +705,7 @@ PageFaultResponse MemoryManager::handle_page_fault(PageFault const& fault) return region->handle_fault(fault); } -KResultOr> MemoryManager::allocate_contiguous_kernel_region(size_t size, StringView name, Region::Access access, Region::Cacheable cacheable) +ErrorOr> MemoryManager::allocate_contiguous_kernel_region(size_t size, StringView name, Region::Access access, Region::Cacheable cacheable) { VERIFY(!(size % PAGE_SIZE)); SpinlockLocker lock(kernel_page_directory().get_lock()); @@ -714,7 +714,7 @@ KResultOr> MemoryManager::allocate_contiguous_kernel_regio return allocate_kernel_region_with_vmobject(range, move(vmobject), name, access, cacheable); } -KResultOr> MemoryManager::allocate_kernel_region(size_t size, StringView name, Region::Access access, AllocationStrategy strategy, Region::Cacheable cacheable) +ErrorOr> MemoryManager::allocate_kernel_region(size_t size, StringView name, Region::Access access, AllocationStrategy strategy, Region::Cacheable cacheable) { VERIFY(!(size % PAGE_SIZE)); auto vmobject = TRY(AnonymousVMObject::try_create_with_size(size, strategy)); @@ -723,7 +723,7 @@ KResultOr> MemoryManager::allocate_kernel_region(size_t si return allocate_kernel_region_with_vmobject(range, move(vmobject), name, access, cacheable); } -KResultOr> MemoryManager::allocate_kernel_region(PhysicalAddress paddr, size_t size, StringView name, Region::Access access, Region::Cacheable cacheable) +ErrorOr> MemoryManager::allocate_kernel_region(PhysicalAddress paddr, size_t size, StringView name, Region::Access access, Region::Cacheable cacheable) { VERIFY(!(size % PAGE_SIZE)); auto vmobject = TRY(AnonymousVMObject::try_create_for_physical_range(paddr, size)); @@ -732,7 +732,7 @@ KResultOr> MemoryManager::allocate_kernel_region(PhysicalA return allocate_kernel_region_with_vmobject(range, move(vmobject), name, access, cacheable); } -KResultOr> MemoryManager::allocate_kernel_region_with_vmobject(VirtualRange const& range, VMObject& vmobject, StringView name, Region::Access access, Region::Cacheable cacheable) +ErrorOr> MemoryManager::allocate_kernel_region_with_vmobject(VirtualRange const& range, VMObject& vmobject, StringView name, Region::Access access, Region::Cacheable cacheable) { OwnPtr name_kstring; if (!name.is_null()) @@ -742,7 +742,7 @@ KResultOr> MemoryManager::allocate_kernel_region_with_vmob return region; } -KResultOr> MemoryManager::allocate_kernel_region_with_vmobject(VMObject& vmobject, size_t size, StringView name, Region::Access access, Region::Cacheable cacheable) +ErrorOr> MemoryManager::allocate_kernel_region_with_vmobject(VMObject& vmobject, size_t size, StringView name, Region::Access access, Region::Cacheable cacheable) { VERIFY(!(size % PAGE_SIZE)); SpinlockLocker lock(kernel_page_directory().get_lock()); @@ -750,7 +750,7 @@ KResultOr> MemoryManager::allocate_kernel_region_with_vmob return allocate_kernel_region_with_vmobject(range, vmobject, name, access, cacheable); } -KResultOr MemoryManager::commit_user_physical_pages(size_t page_count) +ErrorOr MemoryManager::commit_user_physical_pages(size_t page_count) { VERIFY(page_count > 0); SpinlockLocker lock(s_mm_lock); diff --git a/Kernel/Memory/MemoryManager.h b/Kernel/Memory/MemoryManager.h index 9309ad02fae..13380820b56 100644 --- a/Kernel/Memory/MemoryManager.h +++ b/Kernel/Memory/MemoryManager.h @@ -171,7 +171,7 @@ public: Yes }; - KResultOr commit_user_physical_pages(size_t page_count); + ErrorOr commit_user_physical_pages(size_t page_count); void uncommit_user_physical_pages(Badge, size_t page_count); NonnullRefPtr allocate_committed_user_physical_page(Badge, ShouldZeroFill = ShouldZeroFill::Yes); @@ -180,11 +180,11 @@ public: NonnullRefPtrVector allocate_contiguous_supervisor_physical_pages(size_t size); void deallocate_physical_page(PhysicalAddress); - KResultOr> allocate_contiguous_kernel_region(size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes); - KResultOr> allocate_kernel_region(size_t, StringView name, Region::Access access, AllocationStrategy strategy = AllocationStrategy::Reserve, Region::Cacheable = Region::Cacheable::Yes); - KResultOr> allocate_kernel_region(PhysicalAddress, size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes); - KResultOr> allocate_kernel_region_with_vmobject(VMObject&, size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes); - KResultOr> allocate_kernel_region_with_vmobject(VirtualRange const&, VMObject&, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes); + ErrorOr> allocate_contiguous_kernel_region(size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes); + ErrorOr> allocate_kernel_region(size_t, StringView name, Region::Access access, AllocationStrategy strategy = AllocationStrategy::Reserve, Region::Cacheable = Region::Cacheable::Yes); + ErrorOr> allocate_kernel_region(PhysicalAddress, size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes); + ErrorOr> allocate_kernel_region_with_vmobject(VMObject&, size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes); + ErrorOr> allocate_kernel_region_with_vmobject(VirtualRange const&, VMObject&, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes); struct SystemMemoryInfo { PhysicalSize user_physical_pages { 0 }; diff --git a/Kernel/Memory/PageDirectory.cpp b/Kernel/Memory/PageDirectory.cpp index e277e634547..4461a294bef 100644 --- a/Kernel/Memory/PageDirectory.cpp +++ b/Kernel/Memory/PageDirectory.cpp @@ -42,7 +42,7 @@ UNMAP_AFTER_INIT NonnullRefPtr PageDirectory::must_create_kernel_ return directory; } -KResultOr> PageDirectory::try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator) +ErrorOr> PageDirectory::try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator) { constexpr FlatPtr userspace_range_base = 0x00800000; FlatPtr const userspace_range_ceiling = USER_RANGE_CEILING; diff --git a/Kernel/Memory/PageDirectory.h b/Kernel/Memory/PageDirectory.h index d1755f4f1c5..125dd81f708 100644 --- a/Kernel/Memory/PageDirectory.h +++ b/Kernel/Memory/PageDirectory.h @@ -20,7 +20,7 @@ class PageDirectory : public RefCounted { friend class MemoryManager; public: - static KResultOr> try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator = nullptr); + static ErrorOr> try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator = nullptr); static NonnullRefPtr must_create_kernel_page_directory(); static RefPtr find_by_cr3(FlatPtr); diff --git a/Kernel/Memory/PrivateInodeVMObject.cpp b/Kernel/Memory/PrivateInodeVMObject.cpp index 25b1b6cbad0..cd552bc34f8 100644 --- a/Kernel/Memory/PrivateInodeVMObject.cpp +++ b/Kernel/Memory/PrivateInodeVMObject.cpp @@ -9,12 +9,12 @@ namespace Kernel::Memory { -KResultOr> PrivateInodeVMObject::try_create_with_inode(Inode& inode) +ErrorOr> PrivateInodeVMObject::try_create_with_inode(Inode& inode) { return adopt_nonnull_ref_or_enomem(new (nothrow) PrivateInodeVMObject(inode, inode.size())); } -KResultOr> PrivateInodeVMObject::try_clone() +ErrorOr> PrivateInodeVMObject::try_clone() { return adopt_nonnull_ref_or_enomem(new (nothrow) PrivateInodeVMObject(*this)); } diff --git a/Kernel/Memory/PrivateInodeVMObject.h b/Kernel/Memory/PrivateInodeVMObject.h index 96a2f67fb7a..2f6d8598e9e 100644 --- a/Kernel/Memory/PrivateInodeVMObject.h +++ b/Kernel/Memory/PrivateInodeVMObject.h @@ -18,8 +18,8 @@ class PrivateInodeVMObject final : public InodeVMObject { public: virtual ~PrivateInodeVMObject() override; - static KResultOr> try_create_with_inode(Inode&); - virtual KResultOr> try_clone() override; + static ErrorOr> try_create_with_inode(Inode&); + virtual ErrorOr> try_clone() override; private: virtual bool is_private_inode() const override { return true; } diff --git a/Kernel/Memory/Region.cpp b/Kernel/Memory/Region.cpp index 534909c035c..b757ffee9e6 100644 --- a/Kernel/Memory/Region.cpp +++ b/Kernel/Memory/Region.cpp @@ -50,7 +50,7 @@ Region::~Region() } } -KResultOr> Region::try_clone() +ErrorOr> Region::try_clone() { VERIFY(Process::has_current()); @@ -144,12 +144,12 @@ size_t Region::amount_shared() const return bytes; } -KResultOr> Region::try_create_user_accessible(VirtualRange const& range, NonnullRefPtr vmobject, size_t offset_in_vmobject, OwnPtr name, Region::Access access, Cacheable cacheable, bool shared) +ErrorOr> Region::try_create_user_accessible(VirtualRange const& range, NonnullRefPtr vmobject, size_t offset_in_vmobject, OwnPtr name, Region::Access access, Cacheable cacheable, bool shared) { return adopt_nonnull_own_or_enomem(new (nothrow) Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, shared)); } -KResultOr> Region::try_create_kernel_only(VirtualRange const& range, NonnullRefPtr vmobject, size_t offset_in_vmobject, OwnPtr name, Region::Access access, Cacheable cacheable) +ErrorOr> Region::try_create_kernel_only(VirtualRange const& range, NonnullRefPtr vmobject, size_t offset_in_vmobject, OwnPtr name, Region::Access access, Cacheable cacheable) { return adopt_nonnull_own_or_enomem(new (nothrow) Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, false)); } @@ -253,7 +253,7 @@ void Region::set_page_directory(PageDirectory& page_directory) m_page_directory = page_directory; } -KResult Region::map(PageDirectory& page_directory, ShouldFlushTLB should_flush_tlb) +ErrorOr Region::map(PageDirectory& page_directory, ShouldFlushTLB should_flush_tlb) { SpinlockLocker page_lock(page_directory.get_lock()); SpinlockLocker lock(s_mm_lock); @@ -274,7 +274,7 @@ KResult Region::map(PageDirectory& page_directory, ShouldFlushTLB should_flush_t if (should_flush_tlb == ShouldFlushTLB::Yes) MemoryManager::flush_tlb(m_page_directory, vaddr(), page_index); if (page_index == page_count()) - return KSuccess; + return {}; } return ENOMEM; } diff --git a/Kernel/Memory/Region.h b/Kernel/Memory/Region.h index 1b02cf6d127..80bb799fcfb 100644 --- a/Kernel/Memory/Region.h +++ b/Kernel/Memory/Region.h @@ -49,8 +49,8 @@ public: Yes, }; - static KResultOr> try_create_user_accessible(VirtualRange const&, NonnullRefPtr, size_t offset_in_vmobject, OwnPtr name, Region::Access access, Cacheable, bool shared); - static KResultOr> try_create_kernel_only(VirtualRange const&, NonnullRefPtr, size_t offset_in_vmobject, OwnPtr name, Region::Access access, Cacheable = Cacheable::Yes); + static ErrorOr> try_create_user_accessible(VirtualRange const&, NonnullRefPtr, size_t offset_in_vmobject, OwnPtr name, Region::Access access, Cacheable, bool shared); + static ErrorOr> try_create_kernel_only(VirtualRange const&, NonnullRefPtr, size_t offset_in_vmobject, OwnPtr name, Region::Access access, Cacheable = Cacheable::Yes); ~Region(); @@ -90,7 +90,7 @@ public: PageFaultResponse handle_fault(PageFault const&); - KResultOr> try_clone(); + ErrorOr> try_clone(); [[nodiscard]] bool contains(VirtualAddress vaddr) const { @@ -170,7 +170,7 @@ public: void set_executable(bool b) { set_access_bit(Access::Execute, b); } void set_page_directory(PageDirectory&); - KResult map(PageDirectory&, ShouldFlushTLB = ShouldFlushTLB::Yes); + ErrorOr map(PageDirectory&, ShouldFlushTLB = ShouldFlushTLB::Yes); enum class ShouldDeallocateVirtualRange { No, Yes, diff --git a/Kernel/Memory/RingBuffer.cpp b/Kernel/Memory/RingBuffer.cpp index 06d2da02ff7..a807d0aa931 100644 --- a/Kernel/Memory/RingBuffer.cpp +++ b/Kernel/Memory/RingBuffer.cpp @@ -29,7 +29,7 @@ bool RingBuffer::copy_data_in(const UserOrKernelBuffer& buffer, size_t offset, s return true; } -KResultOr RingBuffer::copy_data_out(size_t size, UserOrKernelBuffer& buffer) const +ErrorOr 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); @@ -37,7 +37,7 @@ KResultOr RingBuffer::copy_data_out(size_t size, UserOrKernelBuffer& buf return num_bytes; } -KResultOr RingBuffer::reserve_space(size_t size) +ErrorOr RingBuffer::reserve_space(size_t size) { if (m_capacity_in_bytes < m_num_used_bytes + size) return ENOSPC; diff --git a/Kernel/Memory/RingBuffer.h b/Kernel/Memory/RingBuffer.h index 45338c51f05..7b8889f64aa 100644 --- a/Kernel/Memory/RingBuffer.h +++ b/Kernel/Memory/RingBuffer.h @@ -18,8 +18,8 @@ public: bool has_space() const { return m_num_used_bytes < m_capacity_in_bytes; } bool copy_data_in(const UserOrKernelBuffer& buffer, size_t offset, size_t length, PhysicalAddress& start_of_copied_data, size_t& bytes_copied); - KResultOr copy_data_out(size_t size, UserOrKernelBuffer& buffer) const; - KResultOr reserve_space(size_t size); + ErrorOr copy_data_out(size_t size, UserOrKernelBuffer& buffer) const; + ErrorOr reserve_space(size_t size); void reclaim_space(PhysicalAddress chunk_start, size_t chunk_size); PhysicalAddress start_of_used() const; diff --git a/Kernel/Memory/ScatterGatherList.cpp b/Kernel/Memory/ScatterGatherList.cpp index 1f4cd530af2..803b0c15f8a 100644 --- a/Kernel/Memory/ScatterGatherList.cpp +++ b/Kernel/Memory/ScatterGatherList.cpp @@ -12,7 +12,7 @@ RefPtr ScatterGatherList::try_create(AsyncBlockDeviceRequest& { auto maybe_vm_object = AnonymousVMObject::try_create_with_physical_pages(allocated_pages); if (maybe_vm_object.is_error()) { - // FIXME: Would be nice to be able to return a KResultOr here. + // FIXME: Would be nice to be able to return a ErrorOr here. return {}; } return adopt_ref_if_nonnull(new (nothrow) ScatterGatherList(maybe_vm_object.release_value(), request, device_block_size)); diff --git a/Kernel/Memory/SharedInodeVMObject.cpp b/Kernel/Memory/SharedInodeVMObject.cpp index 7dacfd6e66f..605af54e181 100644 --- a/Kernel/Memory/SharedInodeVMObject.cpp +++ b/Kernel/Memory/SharedInodeVMObject.cpp @@ -9,7 +9,7 @@ namespace Kernel::Memory { -KResultOr> SharedInodeVMObject::try_create_with_inode(Inode& inode) +ErrorOr> SharedInodeVMObject::try_create_with_inode(Inode& inode) { size_t size = inode.size(); if (auto shared_vmobject = inode.shared_vmobject()) @@ -19,7 +19,7 @@ KResultOr> SharedInodeVMObject::try_create_wi return vmobject; } -KResultOr> SharedInodeVMObject::try_clone() +ErrorOr> SharedInodeVMObject::try_clone() { return adopt_nonnull_ref_or_enomem(new (nothrow) SharedInodeVMObject(*this)); } diff --git a/Kernel/Memory/SharedInodeVMObject.h b/Kernel/Memory/SharedInodeVMObject.h index 5b91785c55c..b6fea408b5d 100644 --- a/Kernel/Memory/SharedInodeVMObject.h +++ b/Kernel/Memory/SharedInodeVMObject.h @@ -16,8 +16,8 @@ class SharedInodeVMObject final : public InodeVMObject { AK_MAKE_NONMOVABLE(SharedInodeVMObject); public: - static KResultOr> try_create_with_inode(Inode&); - virtual KResultOr> try_clone() override; + static ErrorOr> try_create_with_inode(Inode&); + virtual ErrorOr> try_clone() override; private: virtual bool is_shared_inode() const override { return true; } diff --git a/Kernel/Memory/VMObject.h b/Kernel/Memory/VMObject.h index e6079dd22bb..2cfa03f8aa9 100644 --- a/Kernel/Memory/VMObject.h +++ b/Kernel/Memory/VMObject.h @@ -26,7 +26,7 @@ class VMObject public: virtual ~VMObject(); - virtual KResultOr> try_clone() = 0; + virtual ErrorOr> try_clone() = 0; virtual bool is_anonymous() const { return false; } virtual bool is_inode() const { return false; } diff --git a/Kernel/Memory/VirtualRange.cpp b/Kernel/Memory/VirtualRange.cpp index c049ddf1bc6..06d5b040a65 100644 --- a/Kernel/Memory/VirtualRange.cpp +++ b/Kernel/Memory/VirtualRange.cpp @@ -36,7 +36,7 @@ VirtualRange VirtualRange::intersect(VirtualRange const& other) const return VirtualRange(new_base, (new_end - new_base).get()); } -KResultOr VirtualRange::expand_to_page_boundaries(FlatPtr address, size_t size) +ErrorOr VirtualRange::expand_to_page_boundaries(FlatPtr address, size_t size) { if (page_round_up_would_wrap(size)) return EINVAL; diff --git a/Kernel/Memory/VirtualRange.h b/Kernel/Memory/VirtualRange.h index cad0b04a647..93c8f80df3f 100644 --- a/Kernel/Memory/VirtualRange.h +++ b/Kernel/Memory/VirtualRange.h @@ -7,7 +7,7 @@ #pragma once -#include +#include #include namespace Kernel::Memory { @@ -51,7 +51,7 @@ public: Vector carve(VirtualRange const&) const; VirtualRange intersect(VirtualRange const&) const; - static KResultOr expand_to_page_boundaries(FlatPtr address, size_t size); + static ErrorOr expand_to_page_boundaries(FlatPtr address, size_t size); private: VirtualAddress m_base; diff --git a/Kernel/Memory/VirtualRangeAllocator.cpp b/Kernel/Memory/VirtualRangeAllocator.cpp index 02389c8e1b9..b9cbdf154d8 100644 --- a/Kernel/Memory/VirtualRangeAllocator.cpp +++ b/Kernel/Memory/VirtualRangeAllocator.cpp @@ -56,7 +56,7 @@ void VirtualRangeAllocator::carve_from_region(VirtualRange const& from, VirtualR } } -KResultOr VirtualRangeAllocator::try_allocate_randomized(size_t size, size_t alignment) +ErrorOr VirtualRangeAllocator::try_allocate_randomized(size_t size, size_t alignment) { if (!size) return EINVAL; @@ -80,7 +80,7 @@ KResultOr VirtualRangeAllocator::try_allocate_randomized(size_t si return try_allocate_anywhere(size, alignment); } -KResultOr VirtualRangeAllocator::try_allocate_anywhere(size_t size, size_t alignment) +ErrorOr VirtualRangeAllocator::try_allocate_anywhere(size_t size, size_t alignment) { if (!size) return EINVAL; @@ -129,7 +129,7 @@ KResultOr VirtualRangeAllocator::try_allocate_anywhere(size_t size return ENOMEM; } -KResultOr VirtualRangeAllocator::try_allocate_specific(VirtualAddress base, size_t size) +ErrorOr VirtualRangeAllocator::try_allocate_specific(VirtualAddress base, size_t size) { if (!size) return EINVAL; diff --git a/Kernel/Memory/VirtualRangeAllocator.h b/Kernel/Memory/VirtualRangeAllocator.h index 9a62b001946..c09202e1653 100644 --- a/Kernel/Memory/VirtualRangeAllocator.h +++ b/Kernel/Memory/VirtualRangeAllocator.h @@ -21,9 +21,9 @@ public: void initialize_with_range(VirtualAddress, size_t); void initialize_from_parent(VirtualRangeAllocator const&); - KResultOr try_allocate_anywhere(size_t, size_t alignment = PAGE_SIZE); - KResultOr try_allocate_specific(VirtualAddress, size_t); - KResultOr try_allocate_randomized(size_t, size_t alignment); + ErrorOr try_allocate_anywhere(size_t, size_t alignment = PAGE_SIZE); + ErrorOr try_allocate_specific(VirtualAddress, size_t); + ErrorOr try_allocate_randomized(size_t, size_t alignment); void deallocate(VirtualRange const&); void dump() const; diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 9eaebce4370..dfb84a474fb 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -35,12 +35,12 @@ MutexProtected& IPv4Socket::all_sockets() return *s_all_sockets; } -KResultOr> IPv4Socket::try_create_receive_buffer() +ErrorOr> IPv4Socket::try_create_receive_buffer() { return DoubleBuffer::try_create(256 * KiB); } -KResultOr> IPv4Socket::create(int type, int protocol) +ErrorOr> IPv4Socket::create(int type, int protocol) { auto receive_buffer = TRY(IPv4Socket::try_create_receive_buffer()); @@ -94,7 +94,7 @@ void IPv4Socket::get_peer_address(sockaddr* address, socklen_t* address_size) *address_size = sizeof(sockaddr_in); } -KResult IPv4Socket::bind(Userspace user_address, socklen_t address_size) +ErrorOr IPv4Socket::bind(Userspace user_address, socklen_t address_size) { VERIFY(setup_state() == SetupState::Unstarted); if (address_size != sizeof(sockaddr_in)) @@ -122,12 +122,12 @@ KResult IPv4Socket::bind(Userspace user_address, socklen_t addr return protocol_bind(); } -KResult IPv4Socket::listen(size_t backlog) +ErrorOr IPv4Socket::listen(size_t backlog) { MutexLocker locker(mutex()); auto result = allocate_local_port_if_needed(); - if (result.error_or_port.is_error() && result.error_or_port.error() != ENOPROTOOPT) - return result.error_or_port.error(); + if (result.error_or_port.is_error() && result.error_or_port.error().code() != ENOPROTOOPT) + return result.error_or_port.release_error(); set_backlog(backlog); set_role(Role::Listener); @@ -138,7 +138,7 @@ KResult IPv4Socket::listen(size_t backlog) return protocol_listen(result.did_allocate); } -KResult IPv4Socket::connect(OpenFileDescription& description, Userspace address, socklen_t address_size, ShouldBlock should_block) +ErrorOr IPv4Socket::connect(OpenFileDescription& description, Userspace address, socklen_t address_size, ShouldBlock should_block) { if (address_size != sizeof(sockaddr_in)) return set_so_error(EINVAL); @@ -182,12 +182,12 @@ PortAllocationResult IPv4Socket::allocate_local_port_if_needed() return { m_local_port, false }; auto port_or_error = protocol_allocate_local_port(); if (port_or_error.is_error()) - return { port_or_error.error(), false }; - m_local_port = port_or_error.value(); + return { port_or_error.release_error(), false }; + m_local_port = port_or_error.release_value(); return { m_local_port, true }; } -KResultOr IPv4Socket::sendto(OpenFileDescription&, const UserOrKernelBuffer& data, size_t data_length, [[maybe_unused]] int flags, Userspace addr, socklen_t addr_length) +ErrorOr IPv4Socket::sendto(OpenFileDescription&, const UserOrKernelBuffer& data, size_t data_length, [[maybe_unused]] int flags, Userspace addr, socklen_t addr_length) { MutexLocker locker(mutex()); @@ -217,8 +217,8 @@ KResultOr IPv4Socket::sendto(OpenFileDescription&, const UserOrKernelBuf if (m_local_address.to_u32() == 0) m_local_address = routing_decision.adapter->ipv4_address(); - if (auto result = allocate_local_port_if_needed(); result.error_or_port.is_error() && result.error_or_port.error() != ENOPROTOOPT) - return result.error_or_port.error(); + if (auto result = allocate_local_port_if_needed(); result.error_or_port.is_error() && result.error_or_port.error().code() != ENOPROTOOPT) + return result.error_or_port.release_error(); dbgln_if(IPV4_SOCKET_DEBUG, "sendto: destination={}:{}", m_peer_address, m_peer_port); @@ -232,7 +232,7 @@ KResultOr IPv4Socket::sendto(OpenFileDescription&, const UserOrKernelBuf m_peer_address, (IPv4Protocol)protocol(), data_length, m_type_of_service, m_ttl); 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(result); + return set_so_error(result.release_error()); } routing_decision.adapter->send_packet(packet->bytes()); routing_decision.adapter->release_packet_buffer(*packet); @@ -245,7 +245,7 @@ KResultOr IPv4Socket::sendto(OpenFileDescription&, const UserOrKernelBuf return nsent_or_error; } -KResultOr IPv4Socket::receive_byte_buffered(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace, Userspace) +ErrorOr IPv4Socket::receive_byte_buffered(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace, Userspace) { MutexLocker locker(mutex()); @@ -271,7 +271,7 @@ KResultOr IPv4Socket::receive_byte_buffered(OpenFileDescription& descrip } } - KResultOr nreceived_or_error { 0 }; + ErrorOr nreceived_or_error { 0 }; if (flags & MSG_PEEK) nreceived_or_error = m_receive_buffer->peek(buffer, buffer_length); else @@ -284,7 +284,7 @@ KResultOr IPv4Socket::receive_byte_buffered(OpenFileDescription& descrip return nreceived_or_error; } -KResultOr IPv4Socket::receive_packet_buffered(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace addr, Userspace addr_length, Time& packet_timestamp) +ErrorOr IPv4Socket::receive_packet_buffered(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace addr, Userspace addr_length, Time& packet_timestamp) { MutexLocker locker(mutex()); ReceivedPacket taken_packet; @@ -379,7 +379,7 @@ KResultOr IPv4Socket::receive_packet_buffered(OpenFileDescription& descr return protocol_receive(packet->data->bytes(), buffer, buffer_length, flags); } -KResultOr IPv4Socket::recvfrom(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace user_addr, Userspace user_addr_length, Time& packet_timestamp) +ErrorOr IPv4Socket::recvfrom(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace user_addr, Userspace user_addr_length, Time& packet_timestamp) { if (user_addr_length) { socklen_t addr_length; @@ -390,7 +390,7 @@ KResultOr IPv4Socket::recvfrom(OpenFileDescription& description, UserOrK dbgln_if(IPV4_SOCKET_DEBUG, "recvfrom: type={}, local_port={}", type(), local_port()); - KResultOr nreceived = 0; + ErrorOr nreceived = 0; if (buffer_mode() == BufferMode::Bytes) nreceived = receive_byte_buffered(description, buffer, buffer_length, flags, user_addr, user_addr_length); else @@ -456,7 +456,7 @@ bool IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, return true; } -KResultOr> IPv4Socket::pseudo_path(const OpenFileDescription&) const +ErrorOr> IPv4Socket::pseudo_path(const OpenFileDescription&) const { if (m_role == Role::None) return KString::try_create("socket"sv); @@ -488,7 +488,7 @@ KResultOr> IPv4Socket::pseudo_path(const OpenFileDescript return KString::try_create(builder.to_string()); } -KResult IPv4Socket::setsockopt(int level, int option, Userspace user_value, socklen_t user_value_size) +ErrorOr IPv4Socket::setsockopt(int level, int option, Userspace user_value, socklen_t user_value_size) { if (level != IPPROTO_IP) return Socket::setsockopt(level, option, user_value, user_value_size); @@ -502,7 +502,7 @@ KResult IPv4Socket::setsockopt(int level, int option, Userspace use if (value < 0 || value > 255) return EINVAL; m_ttl = value; - return KSuccess; + return {}; } case IP_TOS: { if (user_value_size < sizeof(int)) @@ -512,7 +512,7 @@ KResult IPv4Socket::setsockopt(int level, int option, Userspace use if (value < 0 || value > 255) return EINVAL; m_type_of_service = value; - return KSuccess; + return {}; } case IP_MULTICAST_LOOP: { if (user_value_size != 1) @@ -522,7 +522,7 @@ KResult IPv4Socket::setsockopt(int level, int option, Userspace use if (value != 0 && value != 1) return EINVAL; m_multicast_loop = value; - return KSuccess; + return {}; } case IP_ADD_MEMBERSHIP: { if (user_value_size != sizeof(ip_mreq)) @@ -534,7 +534,7 @@ KResult IPv4Socket::setsockopt(int level, int option, Userspace use IPv4Address address { (const u8*)&mreq.imr_multiaddr.s_addr }; if (!m_multicast_memberships.contains_slow(address)) m_multicast_memberships.append(address); - return KSuccess; + return {}; } case IP_DROP_MEMBERSHIP: { if (user_value_size != sizeof(ip_mreq)) @@ -545,14 +545,14 @@ KResult IPv4Socket::setsockopt(int level, int option, Userspace use return ENOTSUP; IPv4Address address { (const u8*)&mreq.imr_multiaddr.s_addr }; m_multicast_memberships.remove_first_matching([&address](auto& a) { return a == address; }); - return KSuccess; + return {}; } default: return ENOPROTOOPT; } } -KResult IPv4Socket::getsockopt(OpenFileDescription& description, int level, int option, Userspace value, Userspace value_size) +ErrorOr IPv4Socket::getsockopt(OpenFileDescription& description, int level, int option, Userspace value, Userspace value_size) { if (level != IPPROTO_IP) return Socket::getsockopt(description, level, option, value, value_size); @@ -589,11 +589,11 @@ KResult IPv4Socket::getsockopt(OpenFileDescription& description, int level, int } } -KResult IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspace arg) +ErrorOr IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspace arg) { REQUIRE_PROMISE(inet); - auto ioctl_route = [request, arg]() -> KResult { + auto ioctl_route = [request, arg]() -> ErrorOr { auto user_route = static_ptr_cast(arg); rtentry route; TRY(copy_from_user(&route, user_route)); @@ -614,17 +614,17 @@ KResult IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspaceset_ipv4_gateway(IPv4Address(((sockaddr_in&)route.rt_gateway).sin_addr.s_addr)); - return KSuccess; + return {}; case SIOCDELRT: // FIXME: Support gateway deletion - return KSuccess; + return {}; } return EINVAL; }; - auto ioctl_arp = [request, arg]() -> KResult { + auto ioctl_arp = [request, arg]() -> ErrorOr { auto user_req = static_ptr_cast(arg); arpreq arp_req; TRY(copy_from_user(&arp_req, user_req)); @@ -636,7 +636,7 @@ KResult IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspace KResult { + auto ioctl_interface = [request, arg]() -> ErrorOr { auto user_ifr = static_ptr_cast(arg); ifreq ifr; TRY(copy_from_user(&ifr, user_ifr)); @@ -670,7 +670,7 @@ KResult IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspaceset_ipv4_address(IPv4Address(((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr)); - return KSuccess; + return {}; case SIOCSIFNETMASK: if (!Process::current().is_superuser()) @@ -678,7 +678,7 @@ KResult IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspaceset_ipv4_netmask(IPv4Address(((sockaddr_in&)ifr.ifr_netmask).sin_addr.s_addr)); - return KSuccess; + return {}; case SIOCGIFADDR: { auto ip4_addr = adapter->ipv4_address().to_u32(); @@ -770,10 +770,10 @@ KResult IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspace IPv4Socket::close() { [[maybe_unused]] auto rc = shutdown(SHUT_RDWR); - return KSuccess; + return {}; } void IPv4Socket::shut_down_for_reading() diff --git a/Kernel/Net/IPv4Socket.h b/Kernel/Net/IPv4Socket.h index bf122114bea..6f1addbc2f3 100644 --- a/Kernel/Net/IPv4Socket.h +++ b/Kernel/Net/IPv4Socket.h @@ -22,29 +22,29 @@ class TCPPacket; class TCPSocket; struct PortAllocationResult { - KResultOr error_or_port; + ErrorOr error_or_port; bool did_allocate; }; class IPv4Socket : public Socket { public: - static KResultOr> create(int type, int protocol); + static ErrorOr> create(int type, int protocol); virtual ~IPv4Socket() override; - virtual KResult close() override; - virtual KResult bind(Userspace, socklen_t) override; - virtual KResult connect(OpenFileDescription&, Userspace, socklen_t, ShouldBlock = ShouldBlock::Yes) override; - virtual KResult listen(size_t) override; + virtual ErrorOr close() override; + virtual ErrorOr bind(Userspace, socklen_t) override; + virtual ErrorOr connect(OpenFileDescription&, Userspace, socklen_t, ShouldBlock = ShouldBlock::Yes) override; + virtual ErrorOr listen(size_t) override; virtual void get_local_address(sockaddr*, socklen_t*) override; virtual void get_peer_address(sockaddr*, socklen_t*) override; virtual bool can_read(const OpenFileDescription&, size_t) const override; virtual bool can_write(const OpenFileDescription&, size_t) const override; - virtual KResultOr sendto(OpenFileDescription&, const UserOrKernelBuffer&, size_t, int, Userspace, socklen_t) override; - virtual KResultOr recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace, Userspace, Time&) override; - virtual KResult setsockopt(int level, int option, Userspace, socklen_t) override; - virtual KResult getsockopt(OpenFileDescription&, int level, int option, Userspace, Userspace) override; + virtual ErrorOr sendto(OpenFileDescription&, const UserOrKernelBuffer&, size_t, int, Userspace, socklen_t) override; + virtual ErrorOr recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace, Userspace, Time&) override; + virtual ErrorOr setsockopt(int level, int option, Userspace, socklen_t) override; + virtual ErrorOr getsockopt(OpenFileDescription&, int level, int option, Userspace, Userspace) override; - virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; + virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; bool did_receive(const IPv4Address& peer_address, u16 peer_port, ReadonlyBytes, const Time&); @@ -61,7 +61,7 @@ public: IPv4SocketTuple tuple() const { return IPv4SocketTuple(m_local_address, m_local_port, m_peer_address, m_peer_port); } - KResultOr> pseudo_path(const OpenFileDescription& description) const override; + ErrorOr> pseudo_path(const OpenFileDescription& description) const override; u8 type_of_service() const { return m_type_of_service; } u8 ttl() const { return m_ttl; } @@ -78,12 +78,12 @@ protected: PortAllocationResult allocate_local_port_if_needed(); - virtual KResult protocol_bind() { return KSuccess; } - virtual KResult protocol_listen([[maybe_unused]] bool did_allocate_port) { return KSuccess; } - virtual KResultOr protocol_receive(ReadonlyBytes /* raw_ipv4_packet */, UserOrKernelBuffer&, size_t, int) { return ENOTIMPL; } - virtual KResultOr protocol_send(const UserOrKernelBuffer&, size_t) { return ENOTIMPL; } - virtual KResult protocol_connect(OpenFileDescription&, ShouldBlock) { return KSuccess; } - virtual KResultOr protocol_allocate_local_port() { return ENOPROTOOPT; } + virtual ErrorOr protocol_bind() { return {}; } + virtual ErrorOr protocol_listen([[maybe_unused]] bool did_allocate_port) { return {}; } + virtual ErrorOr protocol_receive(ReadonlyBytes /* raw_ipv4_packet */, UserOrKernelBuffer&, size_t, int) { return ENOTIMPL; } + virtual ErrorOr protocol_send(const UserOrKernelBuffer&, size_t) { return ENOTIMPL; } + virtual ErrorOr protocol_connect(OpenFileDescription&, ShouldBlock) { return {}; } + virtual ErrorOr protocol_allocate_local_port() { return ENOPROTOOPT; } virtual bool protocol_is_disconnected() const { return false; } virtual void shut_down_for_reading() override; @@ -91,14 +91,14 @@ protected: void set_local_address(IPv4Address address) { m_local_address = address; } void set_peer_address(IPv4Address address) { m_peer_address = address; } - static KResultOr> try_create_receive_buffer(); + static ErrorOr> try_create_receive_buffer(); void drop_receive_buffer(); private: virtual bool is_ipv4() const override { return true; } - KResultOr receive_byte_buffered(OpenFileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace, Userspace); - KResultOr receive_packet_buffered(OpenFileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace, Userspace, Time&); + ErrorOr receive_byte_buffered(OpenFileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace, Userspace); + ErrorOr receive_packet_buffered(OpenFileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace, Userspace, Time&); void set_can_read(bool); diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index 47e428c9536..b66b91fff3a 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -34,14 +34,14 @@ void LocalSocket::for_each(Function callback) }); } -KResultOr> LocalSocket::try_create(int type) +ErrorOr> LocalSocket::try_create(int type) { auto client_buffer = TRY(DoubleBuffer::try_create()); auto server_buffer = TRY(DoubleBuffer::try_create()); return adopt_nonnull_ref_or_enomem(new (nothrow) LocalSocket(type, move(client_buffer), move(server_buffer))); } -KResultOr LocalSocket::try_create_connected_pair(int type) +ErrorOr LocalSocket::try_create_connected_pair(int type) { auto socket = TRY(LocalSocket::try_create(type)); auto description1 = TRY(OpenFileDescription::try_create(*socket)); @@ -106,7 +106,7 @@ void LocalSocket::get_peer_address(sockaddr* address, socklen_t* address_size) get_local_address(address, address_size); } -KResult LocalSocket::bind(Userspace user_address, socklen_t address_size) +ErrorOr LocalSocket::bind(Userspace user_address, socklen_t address_size) { VERIFY(setup_state() == SetupState::Unstarted); if (address_size != sizeof(sockaddr_un)) @@ -125,9 +125,9 @@ KResult LocalSocket::bind(Userspace user_address, socklen_t add UidAndGid owner { m_prebind_uid, m_prebind_gid }; auto result = VirtualFileSystem::the().open(path->view(), O_CREAT | O_EXCL | O_NOFOLLOW_NOERROR, mode, Process::current().current_directory(), owner); if (result.is_error()) { - if (result.error() == EEXIST) + if (result.error().code() == EEXIST) return set_so_error(EADDRINUSE); - return result.error(); + return result.release_error(); } auto file = move(result.value()); @@ -141,10 +141,10 @@ KResult LocalSocket::bind(Userspace user_address, socklen_t add m_path = move(path); m_bound = true; - return KSuccess; + return {}; } -KResult LocalSocket::connect(OpenFileDescription& description, Userspace address, socklen_t address_size, ShouldBlock) +ErrorOr LocalSocket::connect(OpenFileDescription& description, Userspace address, socklen_t address_size, ShouldBlock) { VERIFY(!m_bound); if (address_size != sizeof(sockaddr_un)) @@ -191,7 +191,7 @@ KResult LocalSocket::connect(OpenFileDescription& description, Userspace LocalSocket::listen(size_t backlog) { MutexLocker locker(mutex()); if (type() != SOCK_STREAM) @@ -222,10 +222,10 @@ KResult LocalSocket::listen(size_t backlog) dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) listening with backlog={}", this, backlog); - return KSuccess; + return {}; } -KResult LocalSocket::attach(OpenFileDescription& description) +ErrorOr LocalSocket::attach(OpenFileDescription& description) { VERIFY(!m_accept_side_fd_open); if (m_connect_side_role == Role::None) { @@ -237,7 +237,7 @@ KResult LocalSocket::attach(OpenFileDescription& description) } evaluate_block_conditions(); - return KSuccess; + return {}; } void LocalSocket::detach(OpenFileDescription& description) @@ -290,7 +290,7 @@ bool LocalSocket::can_write(const OpenFileDescription& description, size_t) cons return false; } -KResultOr LocalSocket::sendto(OpenFileDescription& description, const UserOrKernelBuffer& data, size_t data_size, int, Userspace, socklen_t) +ErrorOr LocalSocket::sendto(OpenFileDescription& description, const UserOrKernelBuffer& data, size_t data_size, int, Userspace, socklen_t) { if (!has_attached_peer(description)) return set_so_error(EPIPE); @@ -323,7 +323,7 @@ DoubleBuffer* LocalSocket::send_buffer_for(OpenFileDescription& description) return nullptr; } -KResultOr LocalSocket::recvfrom(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_size, int, Userspace, Userspace, Time&) +ErrorOr LocalSocket::recvfrom(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_size, int, Userspace, Userspace, Time&) { auto* socket_buffer = receive_buffer_for(description); if (!socket_buffer) @@ -355,7 +355,7 @@ StringView LocalSocket::socket_path() const return m_path->view(); } -KResultOr> LocalSocket::pseudo_path(const OpenFileDescription& description) const +ErrorOr> LocalSocket::pseudo_path(const OpenFileDescription& description) const { StringBuilder builder; builder.append("socket:"); @@ -381,7 +381,7 @@ KResultOr> LocalSocket::pseudo_path(const OpenFileDescrip return KString::try_create(builder.to_string()); } -KResult LocalSocket::getsockopt(OpenFileDescription& description, int level, int option, Userspace value, Userspace value_size) +ErrorOr LocalSocket::getsockopt(OpenFileDescription& description, int level, int option, Userspace value, Userspace value_size) { if (level != SOL_SOCKET) return Socket::getsockopt(description, level, option, value, value_size); @@ -402,12 +402,12 @@ KResult LocalSocket::getsockopt(OpenFileDescription& description, int level, int TRY(copy_to_user(static_ptr_cast(value), &m_origin)); size = sizeof(ucred); TRY(copy_to_user(value_size, &size)); - return KSuccess; + return {}; case Role::Connected: TRY(copy_to_user(static_ptr_cast(value), &m_acceptor)); size = sizeof(ucred); TRY(copy_to_user(value_size, &size)); - return KSuccess; + return {}; case Role::Connecting: return ENOTCONN; default: @@ -420,7 +420,7 @@ KResult LocalSocket::getsockopt(OpenFileDescription& description, int level, int } } -KResult LocalSocket::ioctl(OpenFileDescription& description, unsigned request, Userspace arg) +ErrorOr LocalSocket::ioctl(OpenFileDescription& description, unsigned request, Userspace arg) { switch (request) { case FIONREAD: { @@ -432,17 +432,17 @@ KResult LocalSocket::ioctl(OpenFileDescription& description, unsigned request, U return ENOTTY; } -KResult LocalSocket::chmod(OpenFileDescription&, mode_t mode) +ErrorOr LocalSocket::chmod(OpenFileDescription&, mode_t mode) { auto inode = m_inode.strong_ref(); if (inode) return inode->chmod(mode); m_prebind_mode = mode & 0777; - return KSuccess; + return {}; } -KResult LocalSocket::chown(OpenFileDescription&, UserID uid, GroupID gid) +ErrorOr LocalSocket::chown(OpenFileDescription&, UserID uid, GroupID gid) { auto inode = m_inode.strong_ref(); if (inode) @@ -454,7 +454,7 @@ KResult LocalSocket::chown(OpenFileDescription&, UserID uid, GroupID gid) m_prebind_uid = uid; m_prebind_gid = gid; - return KSuccess; + return {}; } NonnullRefPtrVector& LocalSocket::recvfd_queue_for(const OpenFileDescription& description) @@ -477,7 +477,7 @@ NonnullRefPtrVector& LocalSocket::sendfd_queue_for(const Op VERIFY_NOT_REACHED(); } -KResult LocalSocket::sendfd(OpenFileDescription const& socket_description, NonnullRefPtr passing_description) +ErrorOr LocalSocket::sendfd(OpenFileDescription const& socket_description, NonnullRefPtr passing_description) { MutexLocker locker(mutex()); auto role = this->role(socket_description); @@ -489,10 +489,10 @@ KResult LocalSocket::sendfd(OpenFileDescription const& socket_description, Nonnu return set_so_error(EBUSY); if (!queue.try_append(move(passing_description))) return set_so_error(ENOMEM); - return KSuccess; + return {}; } -KResultOr> LocalSocket::recvfd(const OpenFileDescription& socket_description) +ErrorOr> LocalSocket::recvfd(const OpenFileDescription& socket_description) { MutexLocker locker(mutex()); auto role = this->role(socket_description); @@ -506,10 +506,10 @@ KResultOr> LocalSocket::recvfd(const OpenFile return queue.take_first(); } -KResult LocalSocket::try_set_path(StringView path) +ErrorOr LocalSocket::try_set_path(StringView path) { m_path = TRY(KString::try_create(path)); - return KSuccess; + return {}; } } diff --git a/Kernel/Net/LocalSocket.h b/Kernel/Net/LocalSocket.h index 97705f10186..881eb29cf7c 100644 --- a/Kernel/Net/LocalSocket.h +++ b/Kernel/Net/LocalSocket.h @@ -22,34 +22,34 @@ struct SocketPair { class LocalSocket final : public Socket { public: - static KResultOr> try_create(int type); - static KResultOr try_create_connected_pair(int type); + static ErrorOr> try_create(int type); + static ErrorOr try_create_connected_pair(int type); virtual ~LocalSocket() override; - KResult sendfd(OpenFileDescription const& socket_description, NonnullRefPtr passing_description); - KResultOr> recvfd(const OpenFileDescription& socket_description); + ErrorOr sendfd(OpenFileDescription const& socket_description, NonnullRefPtr passing_description); + ErrorOr> recvfd(const OpenFileDescription& socket_description); static void for_each(Function); StringView socket_path() const; - KResultOr> pseudo_path(const OpenFileDescription& description) const override; + ErrorOr> pseudo_path(const OpenFileDescription& description) const override; // ^Socket - virtual KResult bind(Userspace, socklen_t) override; - virtual KResult connect(OpenFileDescription&, Userspace, socklen_t, ShouldBlock = ShouldBlock::Yes) override; - virtual KResult listen(size_t) override; + virtual ErrorOr bind(Userspace, socklen_t) override; + virtual ErrorOr connect(OpenFileDescription&, Userspace, socklen_t, ShouldBlock = ShouldBlock::Yes) override; + virtual ErrorOr listen(size_t) override; virtual void get_local_address(sockaddr*, socklen_t*) override; virtual void get_peer_address(sockaddr*, socklen_t*) override; - virtual KResult attach(OpenFileDescription&) override; + virtual ErrorOr attach(OpenFileDescription&) override; virtual void detach(OpenFileDescription&) override; virtual bool can_read(const OpenFileDescription&, size_t) const override; virtual bool can_write(const OpenFileDescription&, size_t) const override; - virtual KResultOr sendto(OpenFileDescription&, const UserOrKernelBuffer&, size_t, int, Userspace, socklen_t) override; - virtual KResultOr recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace, Userspace, Time&) override; - virtual KResult getsockopt(OpenFileDescription&, int level, int option, Userspace, Userspace) override; - virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; - virtual KResult chown(OpenFileDescription&, UserID, GroupID) override; - virtual KResult chmod(OpenFileDescription&, mode_t) override; + virtual ErrorOr sendto(OpenFileDescription&, const UserOrKernelBuffer&, size_t, int, Userspace, socklen_t) override; + virtual ErrorOr recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace, Userspace, Time&) override; + virtual ErrorOr getsockopt(OpenFileDescription&, int level, int option, Userspace, Userspace) override; + virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; + virtual ErrorOr chown(OpenFileDescription&, UserID, GroupID) override; + virtual ErrorOr chmod(OpenFileDescription&, mode_t) override; private: explicit LocalSocket(int type, NonnullOwnPtr client_buffer, NonnullOwnPtr server_buffer); @@ -69,7 +69,7 @@ private: evaluate_block_conditions(); } - KResult try_set_path(StringView); + ErrorOr try_set_path(StringView); // The inode this socket is bound to. WeakPtr m_inode; diff --git a/Kernel/Net/NetworkingManagement.cpp b/Kernel/Net/NetworkingManagement.cpp index 3464695d7f1..64ed1ef2a65 100644 --- a/Kernel/Net/NetworkingManagement.cpp +++ b/Kernel/Net/NetworkingManagement.cpp @@ -74,7 +74,7 @@ RefPtr NetworkingManagement::lookup_by_name(const StringView& na return found_adapter; } -KResultOr> NetworkingManagement::generate_interface_name_from_pci_address(PCI::DeviceIdentifier const& device_identifier) +ErrorOr> NetworkingManagement::generate_interface_name_from_pci_address(PCI::DeviceIdentifier const& device_identifier) { VERIFY(device_identifier.class_code().value() == 0x2); // Note: This stands for e - "Ethernet", p - "Port" as for PCI bus, "s" for slot as for PCI slot diff --git a/Kernel/Net/NetworkingManagement.h b/Kernel/Net/NetworkingManagement.h index 418b43c5268..cec11713cf1 100644 --- a/Kernel/Net/NetworkingManagement.h +++ b/Kernel/Net/NetworkingManagement.h @@ -27,7 +27,7 @@ public: static bool is_initialized(); bool initialize(); - static KResultOr> generate_interface_name_from_pci_address(PCI::DeviceIdentifier const&); + static ErrorOr> generate_interface_name_from_pci_address(PCI::DeviceIdentifier const&); NetworkingManagement(); diff --git a/Kernel/Net/Socket.cpp b/Kernel/Net/Socket.cpp index 812448d784c..151eba6e909 100644 --- a/Kernel/Net/Socket.cpp +++ b/Kernel/Net/Socket.cpp @@ -17,7 +17,7 @@ namespace Kernel { -KResultOr> Socket::create(int domain, int type, int protocol) +ErrorOr> Socket::create(int domain, int type, int protocol) { switch (domain) { case AF_LOCAL: @@ -65,7 +65,7 @@ RefPtr Socket::accept() return client; } -KResult Socket::queue_connection_from(NonnullRefPtr peer) +ErrorOr Socket::queue_connection_from(NonnullRefPtr peer) { dbgln_if(SOCKET_DEBUG, "Socket({}) queueing connection", this); MutexLocker locker(mutex()); @@ -74,10 +74,10 @@ KResult Socket::queue_connection_from(NonnullRefPtr peer) if (!m_pending.try_append(peer)) return set_so_error(ENOMEM); evaluate_block_conditions(); - return KSuccess; + return {}; } -KResult Socket::setsockopt(int level, int option, Userspace user_value, socklen_t user_value_size) +ErrorOr Socket::setsockopt(int level, int option, Userspace user_value, socklen_t user_value_size) { if (level != SOL_SOCKET) return ENOPROTOOPT; @@ -87,12 +87,12 @@ KResult Socket::setsockopt(int level, int option, Userspace user_va if (user_value_size != sizeof(timeval)) return EINVAL; m_send_timeout = TRY(copy_time_from_user(static_ptr_cast(user_value))); - return KSuccess; + return {}; case SO_RCVTIMEO: if (user_value_size != sizeof(timeval)) return EINVAL; m_receive_timeout = TRY(copy_time_from_user(static_ptr_cast(user_value))); - return KSuccess; + return {}; case SO_BINDTODEVICE: { if (user_value_size != IFNAMSIZ) return EINVAL; @@ -102,11 +102,11 @@ KResult Socket::setsockopt(int level, int option, Userspace user_va if (!device) return ENODEV; m_bound_interface = move(device); - return KSuccess; + return {}; } case SO_KEEPALIVE: // FIXME: Obviously, this is not a real keepalive. - return KSuccess; + return {}; case SO_TIMESTAMP: if (user_value_size != sizeof(int)) return EINVAL; @@ -120,14 +120,14 @@ KResult Socket::setsockopt(int level, int option, Userspace user_va m_timestamp = 0; return ENOTSUP; } - return KSuccess; + return {}; default: dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option); return ENOPROTOOPT; } } -KResult Socket::getsockopt(OpenFileDescription&, int level, int option, Userspace value, Userspace value_size) +ErrorOr Socket::getsockopt(OpenFileDescription&, int level, int option, Userspace value, Userspace value_size) { socklen_t size; TRY(copy_from_user(&size, value_size.unsafe_userspace_ptr())); @@ -160,11 +160,16 @@ KResult Socket::getsockopt(OpenFileDescription&, int level, int option, Userspac case SO_ERROR: { if (size < sizeof(int)) return EINVAL; - int errno = so_error().error(); + int errno; + if (so_error().is_error()) + errno = so_error().error().code(); + else + errno = 0; TRY(copy_to_user(static_ptr_cast(value), &errno)); size = sizeof(int); TRY(copy_to_user(value_size, &size)); - return set_so_error(KSuccess); + clear_so_error(); + return {}; } case SO_BINDTODEVICE: if (size < IFNAMSIZ) @@ -200,7 +205,7 @@ KResult Socket::getsockopt(OpenFileDescription&, int level, int option, Userspac } } -KResultOr Socket::read(OpenFileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size) +ErrorOr Socket::read(OpenFileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size) { if (is_shut_down_for_reading()) return 0; @@ -208,14 +213,14 @@ KResultOr Socket::read(OpenFileDescription& description, u64, UserOrKern return recvfrom(description, buffer, size, 0, {}, 0, t); } -KResultOr Socket::write(OpenFileDescription& description, u64, const UserOrKernelBuffer& data, size_t size) +ErrorOr Socket::write(OpenFileDescription& description, u64, const UserOrKernelBuffer& data, size_t size) { if (is_shut_down_for_writing()) return set_so_error(EPIPE); return sendto(description, data, size, 0, {}, 0); } -KResult Socket::shutdown(int how) +ErrorOr Socket::shutdown(int how) { MutexLocker locker(mutex()); if (type() == SOCK_STREAM && !is_connected()) @@ -228,14 +233,14 @@ KResult Socket::shutdown(int how) shut_down_for_reading(); m_shut_down_for_reading |= (how & SHUT_RD) != 0; m_shut_down_for_writing |= (how & SHUT_WR) != 0; - return KSuccess; + return {}; } -KResult Socket::stat(::stat& st) const +ErrorOr Socket::stat(::stat& st) const { memset(&st, 0, sizeof(st)); st.st_mode = S_IFSOCK; - return KSuccess; + return {}; } void Socket::set_connected(bool connected) diff --git a/Kernel/Net/Socket.h b/Kernel/Net/Socket.h index d623bdb5fd6..b7d069c6bf8 100644 --- a/Kernel/Net/Socket.h +++ b/Kernel/Net/Socket.h @@ -6,11 +6,11 @@ #pragma once +#include #include #include #include #include -#include #include #include #include @@ -27,7 +27,7 @@ class OpenFileDescription; class Socket : public File { public: - static KResultOr> create(int domain, int type, int protocol); + static ErrorOr> create(int domain, int type, int protocol); virtual ~Socket() override; int domain() const { return m_domain; } @@ -76,20 +76,20 @@ public: bool can_accept() const { return !m_pending.is_empty(); } RefPtr accept(); - KResult shutdown(int how); + ErrorOr shutdown(int how); - virtual KResult bind(Userspace, socklen_t) = 0; - virtual KResult connect(OpenFileDescription&, Userspace, socklen_t, ShouldBlock) = 0; - virtual KResult listen(size_t) = 0; + virtual ErrorOr bind(Userspace, socklen_t) = 0; + virtual ErrorOr connect(OpenFileDescription&, Userspace, socklen_t, ShouldBlock) = 0; + virtual ErrorOr listen(size_t) = 0; virtual void get_local_address(sockaddr*, socklen_t*) = 0; virtual void get_peer_address(sockaddr*, socklen_t*) = 0; virtual bool is_local() const { return false; } virtual bool is_ipv4() const { return false; } - virtual KResultOr sendto(OpenFileDescription&, const UserOrKernelBuffer&, size_t, int flags, Userspace, socklen_t) = 0; - virtual KResultOr recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace, Userspace, Time&) = 0; + virtual ErrorOr sendto(OpenFileDescription&, const UserOrKernelBuffer&, size_t, int flags, Userspace, socklen_t) = 0; + virtual ErrorOr recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace, Userspace, Time&) = 0; - virtual KResult setsockopt(int level, int option, Userspace, socklen_t); - virtual KResult getsockopt(OpenFileDescription&, int level, int option, Userspace, Userspace); + virtual ErrorOr setsockopt(int level, int option, Userspace, socklen_t); + virtual ErrorOr getsockopt(OpenFileDescription&, int level, int option, Userspace, Userspace); ProcessID origin_pid() const { return m_origin.pid; } UserID origin_uid() const { return m_origin.uid; } @@ -102,10 +102,10 @@ public: Mutex& mutex() { return m_mutex; } // ^File - virtual KResultOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override final; - virtual KResultOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override final; - virtual KResult stat(::stat&) const override; - virtual KResultOr> pseudo_path(const OpenFileDescription&) const override = 0; + virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override final; + virtual ErrorOr write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override final; + virtual ErrorOr stat(::stat&) const override; + virtual ErrorOr> pseudo_path(const OpenFileDescription&) const override = 0; bool has_receive_timeout() const { return m_receive_timeout != Time::zero(); } const Time& receive_timeout() const { return m_receive_timeout; } @@ -118,7 +118,7 @@ public: protected: Socket(int domain, int type, int protocol); - KResult queue_connection_from(NonnullRefPtr); + ErrorOr queue_connection_from(NonnullRefPtr); size_t backlog() const { return m_backlog; } void set_backlog(size_t backlog) { m_backlog = backlog; } @@ -130,13 +130,25 @@ protected: Role m_role { Role::None }; - KResult so_error() const { return m_so_error; } - KResult set_so_error(KResult error) + ErrorOr so_error() const { return m_so_error; } + + Error set_so_error(ErrnoCode error_code) + { + auto error = Error::from_errno(error_code); + m_so_error = error; + return error; + } + Error set_so_error(Error error) { m_so_error = error; return error; } + void clear_so_error() + { + m_so_error = {}; + } + void set_origin(Process const&); void set_acceptor(Process const&); @@ -166,7 +178,7 @@ private: Time m_send_timeout {}; int m_timestamp { 0 }; - KResult m_so_error { KSuccess }; + ErrorOr m_so_error; NonnullRefPtrVector m_pending; }; diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index 24f078cdc3f..b3091b85b5f 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -40,7 +40,7 @@ void TCPSocket::set_state(State new_state) if (new_state == State::Established && m_direction == Direction::Outgoing) { set_role(Role::Connected); - [[maybe_unused]] auto rc = set_so_error(KSuccess); + clear_so_error(); } if (new_state == State::TimeWait) { @@ -97,10 +97,10 @@ RefPtr TCPSocket::from_tuple(const IPv4SocketTuple& tuple) return {}; }); } -KResultOr> TCPSocket::try_create_client(const IPv4Address& new_local_address, u16 new_local_port, const IPv4Address& new_peer_address, u16 new_peer_port) +ErrorOr> TCPSocket::try_create_client(const IPv4Address& new_local_address, u16 new_local_port, const IPv4Address& new_peer_address, u16 new_peer_port) { auto tuple = IPv4SocketTuple(new_local_address, new_local_port, new_peer_address, new_peer_port); - return sockets_by_tuple().with_exclusive([&](auto& table) -> KResultOr> { + return sockets_by_tuple().with_exclusive([&](auto& table) -> ErrorOr> { if (table.contains(tuple)) return EEXIST; @@ -154,14 +154,14 @@ TCPSocket::~TCPSocket() dbgln_if(TCP_SOCKET_DEBUG, "~TCPSocket in state {}", to_string(state())); } -KResultOr> TCPSocket::try_create(int protocol, NonnullOwnPtr receive_buffer) +ErrorOr> TCPSocket::try_create(int protocol, NonnullOwnPtr receive_buffer) { // Note: Scratch buffer is only used for SOCK_STREAM sockets. auto scratch_buffer = TRY(KBuffer::try_create_with_size(65536)); return adopt_nonnull_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer))); } -KResultOr TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags) +ErrorOr TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags) { auto& ipv4_packet = *reinterpret_cast(raw_ipv4_packet.data()); auto& tcp_packet = *static_cast(ipv4_packet.payload()); @@ -172,7 +172,7 @@ KResultOr TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, Use return payload_size; } -KResultOr TCPSocket::protocol_send(const UserOrKernelBuffer& data, size_t data_length) +ErrorOr TCPSocket::protocol_send(const UserOrKernelBuffer& data, size_t data_length) { RoutingDecision routing_decision = route_to(peer_address(), local_address(), bound_interface()); if (routing_decision.is_zero()) @@ -183,14 +183,14 @@ KResultOr TCPSocket::protocol_send(const UserOrKernelBuffer& data, size_ return data_length; } -KResult TCPSocket::send_ack(bool allow_duplicate) +ErrorOr TCPSocket::send_ack(bool allow_duplicate) { if (!allow_duplicate && m_last_ack_number_sent == m_ack_number) - return KSuccess; + return {}; return send_tcp_packet(TCPFlags::ACK); } -KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, size_t payload_size, RoutingDecision* user_routing_decision) +ErrorOr TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, size_t payload_size, RoutingDecision* user_routing_decision) { RoutingDecision routing_decision = user_routing_decision ? *user_routing_decision : route_to(peer_address(), local_address(), bound_interface()); if (routing_decision.is_zero()) @@ -227,7 +227,7 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, 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); + return set_so_error(result.release_error()); } } @@ -260,7 +260,7 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, routing_decision.adapter->release_packet_buffer(*packet); } - return KSuccess; + return {}; } void TCPSocket::receive_tcp_packet(const TCPPacket& packet, u16 size) @@ -362,7 +362,7 @@ NetworkOrdered TCPSocket::compute_tcp_checksum(const IPv4Address& source, c return ~(checksum & 0xffff); } -KResult TCPSocket::protocol_bind() +ErrorOr TCPSocket::protocol_bind() { if (has_specific_local_address() && !m_adapter) { m_adapter = NetworkingManagement::the().from_ipv4_address(local_address()); @@ -370,10 +370,10 @@ KResult TCPSocket::protocol_bind() return set_so_error(EADDRNOTAVAIL); } - return KSuccess; + return {}; } -KResult TCPSocket::protocol_listen(bool did_allocate_port) +ErrorOr TCPSocket::protocol_listen(bool did_allocate_port) { if (!did_allocate_port) { bool ok = sockets_by_tuple().with_exclusive([&](auto& table) -> bool { @@ -389,10 +389,10 @@ KResult TCPSocket::protocol_listen(bool did_allocate_port) set_direction(Direction::Passive); set_state(State::Listen); set_setup_state(SetupState::Completed); - return KSuccess; + return {}; } -KResult TCPSocket::protocol_connect(OpenFileDescription& description, ShouldBlock should_block) +ErrorOr TCPSocket::protocol_connect(OpenFileDescription& description, ShouldBlock should_block) { MutexLocker locker(mutex()); @@ -403,7 +403,7 @@ KResult TCPSocket::protocol_connect(OpenFileDescription& description, ShouldBloc set_local_address(routing_decision.adapter->ipv4_address()); if (auto result = allocate_local_port_if_needed(); result.error_or_port.is_error()) - return result.error_or_port.error(); + return result.error_or_port.release_error(); m_sequence_number = get_good_random(); m_ack_number = 0; @@ -430,20 +430,20 @@ KResult TCPSocket::protocol_connect(OpenFileDescription& description, ShouldBloc else return set_so_error(ECONNREFUSED); } - return KSuccess; + return {}; } return set_so_error(EINPROGRESS); } -KResultOr TCPSocket::protocol_allocate_local_port() +ErrorOr TCPSocket::protocol_allocate_local_port() { constexpr u16 first_ephemeral_port = 32768; constexpr u16 last_ephemeral_port = 60999; constexpr u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port; u16 first_scan_port = first_ephemeral_port + get_good_random() % ephemeral_port_range_size; - return sockets_by_tuple().with_exclusive([&](auto& table) -> KResultOr { + return sockets_by_tuple().with_exclusive([&](auto& table) -> ErrorOr { for (u16 port = first_scan_port;;) { IPv4SocketTuple proposed_tuple(local_address(), port, peer_address(), peer_port()); @@ -490,7 +490,7 @@ void TCPSocket::shut_down_for_writing() } } -KResult TCPSocket::close() +ErrorOr TCPSocket::close() { MutexLocker locker(mutex()); auto result = IPv4Socket::close(); diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h index 2dcc8774645..b8c60c3fd69 100644 --- a/Kernel/Net/TCPSocket.h +++ b/Kernel/Net/TCPSocket.h @@ -6,11 +6,11 @@ #pragma once +#include #include #include #include #include -#include #include #include @@ -19,7 +19,7 @@ namespace Kernel { class TCPSocket final : public IPv4Socket { public: static void for_each(Function); - static KResultOr> try_create(int protocol, NonnullOwnPtr receive_buffer); + static ErrorOr> try_create(int protocol, NonnullOwnPtr receive_buffer); virtual ~TCPSocket() override; enum class Direction { @@ -136,8 +136,8 @@ public: void set_duplicate_acks(u32 acks) { m_duplicate_acks = acks; } u32 duplicate_acks() const { return m_duplicate_acks; } - KResult send_ack(bool allow_duplicate = false); - KResult send_tcp_packet(u16 flags, const UserOrKernelBuffer* = nullptr, size_t = 0, RoutingDecision* = nullptr); + ErrorOr send_ack(bool allow_duplicate = false); + ErrorOr send_tcp_packet(u16 flags, const UserOrKernelBuffer* = nullptr, size_t = 0, RoutingDecision* = nullptr); void receive_tcp_packet(const TCPPacket&, u16 size); bool should_delay_next_ack() const; @@ -147,7 +147,7 @@ public: static MutexProtected>>& closing_sockets(); - KResultOr> try_create_client(IPv4Address const& local_address, u16 local_port, IPv4Address const& peer_address, u16 peer_port); + ErrorOr> try_create_client(IPv4Address const& local_address, u16 local_port, IPv4Address const& peer_address, u16 peer_port); void set_originator(TCPSocket& originator) { m_originator = originator; } bool has_originator() { return !!m_originator; } void release_to_originator(); @@ -155,7 +155,7 @@ public: void retransmit_packets(); - virtual KResult close() override; + virtual ErrorOr close() override; virtual bool can_write(const OpenFileDescription&, size_t) const override; @@ -170,13 +170,13 @@ private: virtual void shut_down_for_writing() override; - virtual KResultOr protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, int flags) override; - virtual KResultOr protocol_send(const UserOrKernelBuffer&, size_t) override; - virtual KResult protocol_connect(OpenFileDescription&, ShouldBlock) override; - virtual KResultOr protocol_allocate_local_port() override; + virtual ErrorOr protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, int flags) override; + virtual ErrorOr protocol_send(const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr protocol_connect(OpenFileDescription&, ShouldBlock) override; + virtual ErrorOr protocol_allocate_local_port() override; virtual bool protocol_is_disconnected() const override; - virtual KResult protocol_bind() override; - virtual KResult protocol_listen(bool did_allocate_port) override; + virtual ErrorOr protocol_bind() override; + virtual ErrorOr protocol_listen(bool did_allocate_port) override; void enqueue_for_retransmit(); void dequeue_for_retransmit(); diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp index 51847893c72..0a6b4eb7f83 100644 --- a/Kernel/Net/UDPSocket.cpp +++ b/Kernel/Net/UDPSocket.cpp @@ -54,12 +54,12 @@ UDPSocket::~UDPSocket() }); } -KResultOr> UDPSocket::try_create(int protocol, NonnullOwnPtr receive_buffer) +ErrorOr> UDPSocket::try_create(int protocol, NonnullOwnPtr receive_buffer) { return adopt_nonnull_ref_or_enomem(new (nothrow) UDPSocket(protocol, move(receive_buffer))); } -KResultOr UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags) +ErrorOr UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags) { auto& ipv4_packet = *(const IPv4Packet*)(raw_ipv4_packet.data()); auto& udp_packet = *static_cast(ipv4_packet.payload()); @@ -69,7 +69,7 @@ KResultOr UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, Use return read_size; } -KResultOr UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_t data_length) +ErrorOr UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_t data_length) { auto routing_decision = route_to(peer_address(), local_address(), bound_interface()); if (routing_decision.is_zero()) @@ -92,21 +92,21 @@ KResultOr UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_ return data_length; } -KResult UDPSocket::protocol_connect(OpenFileDescription&, ShouldBlock) +ErrorOr UDPSocket::protocol_connect(OpenFileDescription&, ShouldBlock) { set_role(Role::Connected); set_connected(true); - return KSuccess; + return {}; } -KResultOr UDPSocket::protocol_allocate_local_port() +ErrorOr UDPSocket::protocol_allocate_local_port() { constexpr u16 first_ephemeral_port = 32768; constexpr u16 last_ephemeral_port = 60999; constexpr u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port; u16 first_scan_port = first_ephemeral_port + get_good_random() % ephemeral_port_range_size; - return sockets_by_port().with_exclusive([&](auto& table) -> KResultOr { + return sockets_by_port().with_exclusive([&](auto& table) -> ErrorOr { for (u16 port = first_scan_port;;) { auto it = table.find(port); if (it == table.end()) { @@ -124,13 +124,13 @@ KResultOr UDPSocket::protocol_allocate_local_port() }); } -KResult UDPSocket::protocol_bind() +ErrorOr UDPSocket::protocol_bind() { - return sockets_by_port().with_exclusive([&](auto& table) -> KResult { + return sockets_by_port().with_exclusive([&](auto& table) -> ErrorOr { if (table.contains(local_port())) return set_so_error(EADDRINUSE); table.set(local_port(), this); - return KSuccess; + return {}; }); } diff --git a/Kernel/Net/UDPSocket.h b/Kernel/Net/UDPSocket.h index c387ac536b0..74845b7a27e 100644 --- a/Kernel/Net/UDPSocket.h +++ b/Kernel/Net/UDPSocket.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #include #include @@ -14,7 +14,7 @@ namespace Kernel { class UDPSocket final : public IPv4Socket { public: - static KResultOr> try_create(int protocol, NonnullOwnPtr receive_buffer); + static ErrorOr> try_create(int protocol, NonnullOwnPtr receive_buffer); virtual ~UDPSocket() override; static SocketHandle from_port(u16); @@ -25,11 +25,11 @@ private: virtual StringView class_name() const override { return "UDPSocket"sv; } static MutexProtected>& sockets_by_port(); - virtual KResultOr protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, int flags) override; - virtual KResultOr protocol_send(const UserOrKernelBuffer&, size_t) override; - virtual KResult protocol_connect(OpenFileDescription&, ShouldBlock) override; - virtual KResultOr protocol_allocate_local_port() override; - virtual KResult protocol_bind() override; + virtual ErrorOr protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, int flags) override; + virtual ErrorOr protocol_send(const UserOrKernelBuffer&, size_t) override; + virtual ErrorOr protocol_connect(OpenFileDescription&, ShouldBlock) override; + virtual ErrorOr protocol_allocate_local_port() override; + virtual ErrorOr protocol_bind() override; }; } diff --git a/Kernel/PerformanceEventBuffer.cpp b/Kernel/PerformanceEventBuffer.cpp index 5aeca08000e..fc8e10f3da6 100644 --- a/Kernel/PerformanceEventBuffer.cpp +++ b/Kernel/PerformanceEventBuffer.cpp @@ -21,7 +21,7 @@ PerformanceEventBuffer::PerformanceEventBuffer(NonnullOwnPtr buffer) { } -NEVER_INLINE KResult PerformanceEventBuffer::append(int type, FlatPtr arg1, FlatPtr arg2, const StringView& arg3, Thread* current_thread) +NEVER_INLINE ErrorOr PerformanceEventBuffer::append(int type, FlatPtr arg1, FlatPtr arg2, const StringView& arg3, Thread* current_thread) { FlatPtr ebp; asm volatile("movl %%ebp, %%eax" @@ -55,13 +55,13 @@ static Vector raw_backtrace(Fl return backtrace; } -KResult PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID tid, const RegisterState& regs, +ErrorOr PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID tid, const RegisterState& regs, int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, const StringView& arg3) { return append_with_ip_and_bp(pid, tid, regs.ip(), regs.bp(), type, lost_samples, arg1, arg2, arg3); } -KResult PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID tid, +ErrorOr PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID tid, FlatPtr ip, FlatPtr bp, int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, const StringView& arg3) { if (count() >= capacity()) @@ -160,7 +160,7 @@ KResult PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID ti event.tid = tid.value(); event.timestamp = TimeManagement::the().uptime_ms(); at(m_count++) = event; - return KSuccess; + return {}; } PerformanceEvent& PerformanceEventBuffer::at(size_t index) @@ -171,7 +171,7 @@ PerformanceEvent& PerformanceEventBuffer::at(size_t index) } template -KResult PerformanceEventBuffer::to_json_impl(Serializer& object) const +ErrorOr PerformanceEventBuffer::to_json_impl(Serializer& object) const { { auto strings = object.add_array("strings"); @@ -270,10 +270,10 @@ KResult PerformanceEventBuffer::to_json_impl(Serializer& object) const } array.finish(); object.finish(); - return KSuccess; + return {}; } -KResult PerformanceEventBuffer::to_json(KBufferBuilder& builder) const +ErrorOr PerformanceEventBuffer::to_json(KBufferBuilder& builder) const { JsonObjectSerializer object(builder); return to_json_impl(object); @@ -312,7 +312,7 @@ void PerformanceEventBuffer::add_process(const Process& process, ProcessEventTyp } } -KResultOr PerformanceEventBuffer::register_string(NonnullOwnPtr string) +ErrorOr PerformanceEventBuffer::register_string(NonnullOwnPtr string) { FlatPtr string_id = m_strings.size(); diff --git a/Kernel/PerformanceEventBuffer.h b/Kernel/PerformanceEventBuffer.h index 6247cb173d7..c8b84301390 100644 --- a/Kernel/PerformanceEventBuffer.h +++ b/Kernel/PerformanceEventBuffer.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #include namespace Kernel { @@ -101,10 +101,10 @@ class PerformanceEventBuffer { public: static OwnPtr try_create_with_size(size_t buffer_size); - KResult append(int type, FlatPtr arg1, FlatPtr arg2, const StringView& arg3, Thread* current_thread = Thread::current()); - KResult append_with_ip_and_bp(ProcessID pid, ThreadID tid, FlatPtr eip, FlatPtr ebp, + ErrorOr append(int type, FlatPtr arg1, FlatPtr arg2, const StringView& arg3, Thread* current_thread = Thread::current()); + ErrorOr append_with_ip_and_bp(ProcessID pid, ThreadID tid, FlatPtr eip, FlatPtr ebp, int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, const StringView& arg3); - KResult append_with_ip_and_bp(ProcessID pid, ThreadID tid, const RegisterState& regs, + ErrorOr append_with_ip_and_bp(ProcessID pid, ThreadID tid, const RegisterState& regs, int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, const StringView& arg3); void clear() @@ -119,17 +119,17 @@ public: return const_cast(*this).at(index); } - KResult to_json(KBufferBuilder&) const; + ErrorOr to_json(KBufferBuilder&) const; void add_process(const Process&, ProcessEventType event_type); - KResultOr register_string(NonnullOwnPtr); + ErrorOr register_string(NonnullOwnPtr); private: explicit PerformanceEventBuffer(NonnullOwnPtr); template - KResult to_json_impl(Serializer&) const; + ErrorOr to_json_impl(Serializer&) const; PerformanceEvent& at(size_t index); diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 261564954a8..88f52c60d2d 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -140,7 +139,7 @@ void Process::register_new(Process& process) }); } -KResultOr> Process::try_create_user_process(RefPtr& first_thread, StringView path, UserID uid, GroupID gid, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, TTY* tty) +ErrorOr> Process::try_create_user_process(RefPtr& first_thread, StringView path, UserID uid, GroupID gid, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, TTY* tty) { auto parts = path.split_view('/'); if (arguments.is_empty()) { @@ -168,9 +167,9 @@ KResultOr> Process::try_create_user_process(RefPtrexec(move(path_string), move(arguments), move(environment)); result.is_error()) { - dbgln("Failed to exec {}: {}", path, result); + dbgln("Failed to exec {}: {}", path, result.error()); first_thread = nullptr; - return result; + return result.release_error(); } register_new(*process); @@ -218,7 +217,7 @@ void Process::unprotect_data() }); } -KResultOr> Process::try_create(RefPtr& first_thread, NonnullOwnPtr name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr cwd, RefPtr executable, TTY* tty, Process* fork_parent) +ErrorOr> Process::try_create(RefPtr& first_thread, NonnullOwnPtr name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr cwd, RefPtr executable, TTY* tty, Process* fork_parent) { auto space = TRY(Memory::AddressSpace::try_create(fork_parent ? &fork_parent->address_space() : nullptr)); auto process = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Process(move(name), uid, gid, ppid, is_kernel_process, move(cwd), move(executable), tty))); @@ -249,7 +248,7 @@ Process::Process(NonnullOwnPtr name, UserID uid, GroupID gid, ProcessID dbgln_if(PROCESS_DEBUG, "Created new process {}({})", m_name, this->pid().value()); } -KResult Process::attach_resources(NonnullOwnPtr&& preallocated_space, RefPtr& first_thread, Process* fork_parent) +ErrorOr Process::attach_resources(NonnullOwnPtr&& preallocated_space, RefPtr& first_thread, Process* fork_parent) { m_space = move(preallocated_space); @@ -270,7 +269,7 @@ KResult Process::attach_resources(NonnullOwnPtr&& prealloc } m_procfs_traits = TRY(ProcessProcFSTraits::try_create({}, *this)); - return KSuccess; + return {}; } Process::~Process() @@ -450,7 +449,7 @@ Process::OpenFileDescriptionAndFlags& Process::OpenFileDescriptions::at(size_t i return m_fds_metadatas[i]; } -KResultOr> Process::OpenFileDescriptions::open_file_description(int fd) const +ErrorOr> Process::OpenFileDescriptions::open_file_description(int fd) const { SpinlockLocker lock(m_fds_lock); if (fd < 0) @@ -489,7 +488,7 @@ size_t Process::OpenFileDescriptions::open_count() const return count; } -KResultOr Process::OpenFileDescriptions::allocate(int first_candidate_fd) +ErrorOr Process::OpenFileDescriptions::allocate(int first_candidate_fd) { SpinlockLocker lock(m_fds_lock); for (size_t i = first_candidate_fd; i < max_open(); ++i) { @@ -530,7 +529,7 @@ Custody& Process::current_directory() return *m_cwd; } -KResultOr> Process::get_syscall_path_argument(Userspace user_path, size_t path_length) const +ErrorOr> Process::get_syscall_path_argument(Userspace user_path, size_t path_length) const { if (path_length == 0) return EINVAL; @@ -539,7 +538,7 @@ KResultOr> Process::get_syscall_path_argument(Userspace> Process::get_syscall_path_argument(Syscall::StringArgument const& path) const +ErrorOr> Process::get_syscall_path_argument(Syscall::StringArgument const& path) const { Userspace path_characters((FlatPtr)path.characters); return get_syscall_path_argument(path_characters, path.length); @@ -734,7 +733,7 @@ void Process::terminate_due_to_signal(u8 signal) die(); } -KResult Process::send_signal(u8 signal, Process* sender) +ErrorOr Process::send_signal(u8 signal, Process* sender) { // Try to send it to the "obvious" main thread: auto receiver_thread = Thread::from_tid(pid().value()); @@ -749,7 +748,7 @@ KResult Process::send_signal(u8 signal, Process* sender) } if (receiver_thread) { receiver_thread->send_signal(signal, sender); - return KSuccess; + return {}; } return ESRCH; } @@ -799,10 +798,10 @@ void Process::set_tty(TTY* tty) m_tty = tty; } -KResult Process::start_tracing_from(ProcessID tracer) +ErrorOr Process::start_tracing_from(ProcessID tracer) { m_tracer = TRY(ThreadTracer::try_create(tracer)); - return KSuccess; + return {}; } void Process::stop_tracing() @@ -861,7 +860,7 @@ void Process::set_dumpable(bool dumpable) m_protected_values.dumpable = dumpable; } -KResult Process::set_coredump_property(NonnullOwnPtr key, NonnullOwnPtr value) +ErrorOr Process::set_coredump_property(NonnullOwnPtr key, NonnullOwnPtr value) { // Write it into the first available property slot. for (auto& slot : m_coredump_properties) { @@ -869,12 +868,12 @@ KResult Process::set_coredump_property(NonnullOwnPtr key, NonnullOwnPtr continue; slot.key = move(key); slot.value = move(value); - return KSuccess; + return {}; } return ENOBUFS; } -KResult Process::try_set_coredump_property(StringView key, StringView value) +ErrorOr Process::try_set_coredump_property(StringView key, StringView value) { auto key_kstring = TRY(KString::try_create(key)); auto value_kstring = TRY(KString::try_create(value)); diff --git a/Kernel/Process.h b/Kernel/Process.h index 1806dad3f6f..bb26b29d3c5 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -179,7 +179,7 @@ public: } static RefPtr create_kernel_process(RefPtr& first_thread, NonnullOwnPtr name, void (*entry)(void*), void* entry_data = nullptr, u32 affinity = THREAD_AFFINITY_DEFAULT, RegisterProcess do_register = RegisterProcess::Yes); - static KResultOr> try_create_user_process(RefPtr& first_thread, StringView path, UserID, GroupID, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, TTY*); + static ErrorOr> try_create_user_process(RefPtr& first_thread, StringView path, UserID, GroupID, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, TTY*); static void register_new(Process&); bool unref() const; @@ -260,163 +260,163 @@ public: ThreadTracer* tracer() { return m_tracer.ptr(); } bool is_traced() const { return !!m_tracer; } - KResult start_tracing_from(ProcessID tracer); + ErrorOr start_tracing_from(ProcessID tracer); void stop_tracing(); void tracer_trap(Thread&, const RegisterState&); - KResultOr sys$emuctl(); - KResultOr sys$yield(); - KResultOr sys$sync(); - KResultOr sys$beep(); - KResultOr sys$get_process_name(Userspace buffer, size_t buffer_size); - KResultOr sys$set_process_name(Userspace user_name, size_t user_name_length); - KResultOr sys$create_inode_watcher(u32 flags); - KResultOr sys$inode_watcher_add_watch(Userspace user_params); - KResultOr sys$inode_watcher_remove_watch(int fd, int wd); - KResultOr sys$dbgputch(u8); - KResultOr sys$dbgputstr(Userspace, size_t); - KResultOr sys$dump_backtrace(); - KResultOr sys$gettid(); - KResultOr sys$setsid(); - KResultOr sys$getsid(pid_t); - KResultOr sys$setpgid(pid_t pid, pid_t pgid); - KResultOr sys$getpgrp(); - KResultOr sys$getpgid(pid_t); - KResultOr sys$getuid(); - KResultOr sys$getgid(); - KResultOr sys$geteuid(); - KResultOr sys$getegid(); - KResultOr sys$getpid(); - KResultOr sys$getppid(); - KResultOr sys$getresuid(Userspace, Userspace, Userspace); - KResultOr sys$getresgid(Userspace, Userspace, Userspace); - KResultOr sys$umask(mode_t); - KResultOr sys$open(Userspace); - KResultOr sys$close(int fd); - KResultOr sys$read(int fd, Userspace, size_t); - KResultOr sys$pread(int fd, Userspace, size_t, off_t); - KResultOr sys$readv(int fd, Userspace iov, int iov_count); - KResultOr sys$write(int fd, Userspace, size_t); - KResultOr sys$writev(int fd, Userspace iov, int iov_count); - KResultOr sys$fstat(int fd, Userspace); - KResultOr sys$stat(Userspace); - KResultOr sys$lseek(int fd, Userspace, int whence); - KResultOr sys$ftruncate(int fd, Userspace); - KResultOr sys$kill(pid_t pid_or_pgid, int sig); + ErrorOr sys$emuctl(); + ErrorOr sys$yield(); + ErrorOr sys$sync(); + ErrorOr sys$beep(); + ErrorOr sys$get_process_name(Userspace buffer, size_t buffer_size); + ErrorOr sys$set_process_name(Userspace user_name, size_t user_name_length); + ErrorOr sys$create_inode_watcher(u32 flags); + ErrorOr sys$inode_watcher_add_watch(Userspace user_params); + ErrorOr sys$inode_watcher_remove_watch(int fd, int wd); + ErrorOr sys$dbgputch(u8); + ErrorOr sys$dbgputstr(Userspace, size_t); + ErrorOr sys$dump_backtrace(); + ErrorOr sys$gettid(); + ErrorOr sys$setsid(); + ErrorOr sys$getsid(pid_t); + ErrorOr sys$setpgid(pid_t pid, pid_t pgid); + ErrorOr sys$getpgrp(); + ErrorOr sys$getpgid(pid_t); + ErrorOr sys$getuid(); + ErrorOr sys$getgid(); + ErrorOr sys$geteuid(); + ErrorOr sys$getegid(); + ErrorOr sys$getpid(); + ErrorOr sys$getppid(); + ErrorOr sys$getresuid(Userspace, Userspace, Userspace); + ErrorOr sys$getresgid(Userspace, Userspace, Userspace); + ErrorOr sys$umask(mode_t); + ErrorOr sys$open(Userspace); + ErrorOr sys$close(int fd); + ErrorOr sys$read(int fd, Userspace, size_t); + ErrorOr sys$pread(int fd, Userspace, size_t, off_t); + ErrorOr sys$readv(int fd, Userspace iov, int iov_count); + ErrorOr sys$write(int fd, Userspace, size_t); + ErrorOr sys$writev(int fd, Userspace iov, int iov_count); + ErrorOr sys$fstat(int fd, Userspace); + ErrorOr sys$stat(Userspace); + ErrorOr sys$lseek(int fd, Userspace, int whence); + ErrorOr sys$ftruncate(int fd, Userspace); + ErrorOr sys$kill(pid_t pid_or_pgid, int sig); [[noreturn]] void sys$exit(int status); - KResultOr sys$sigreturn(RegisterState& registers); - KResultOr sys$waitid(Userspace); - KResultOr sys$mmap(Userspace); - KResultOr sys$mremap(Userspace); - KResultOr sys$munmap(Userspace, size_t); - KResultOr sys$set_mmap_name(Userspace); - KResultOr sys$mprotect(Userspace, size_t, int prot); - KResultOr sys$madvise(Userspace, size_t, int advice); - KResultOr sys$msyscall(Userspace); - KResultOr sys$purge(int mode); - KResultOr sys$select(Userspace); - KResultOr sys$poll(Userspace); - KResultOr sys$get_dir_entries(int fd, Userspace, size_t); - KResultOr sys$getcwd(Userspace, size_t); - KResultOr sys$chdir(Userspace, size_t); - KResultOr sys$fchdir(int fd); - KResultOr sys$adjtime(Userspace, Userspace); - KResultOr sys$clock_gettime(clockid_t, Userspace); - KResultOr sys$clock_settime(clockid_t, Userspace); - KResultOr sys$clock_nanosleep(Userspace); - KResultOr sys$gethostname(Userspace, size_t); - KResultOr sys$sethostname(Userspace, size_t); - KResultOr sys$uname(Userspace); - KResultOr sys$readlink(Userspace); - KResultOr sys$ttyname(int fd, Userspace, size_t); - KResultOr sys$ptsname(int fd, Userspace, size_t); - KResultOr sys$fork(RegisterState&); - KResultOr sys$execve(Userspace); - KResultOr sys$dup2(int old_fd, int new_fd); - KResultOr sys$sigaction(int signum, Userspace act, Userspace old_act); - KResultOr sys$sigprocmask(int how, Userspace set, Userspace old_set); - KResultOr sys$sigpending(Userspace); - KResultOr sys$getgroups(size_t, Userspace); - KResultOr sys$setgroups(size_t, Userspace); - KResultOr sys$pipe(int pipefd[2], int flags); - KResultOr sys$killpg(pid_t pgrp, int sig); - KResultOr sys$seteuid(UserID); - KResultOr sys$setegid(GroupID); - KResultOr sys$setuid(UserID); - KResultOr sys$setgid(GroupID); - KResultOr sys$setreuid(UserID, UserID); - KResultOr sys$setresuid(UserID, UserID, UserID); - KResultOr sys$setresgid(GroupID, GroupID, GroupID); - KResultOr sys$alarm(unsigned seconds); - KResultOr sys$access(Userspace pathname, size_t path_length, int mode); - KResultOr sys$fcntl(int fd, int cmd, u32 extra_arg); - KResultOr sys$ioctl(int fd, unsigned request, FlatPtr arg); - KResultOr sys$mkdir(Userspace pathname, size_t path_length, mode_t mode); - KResultOr sys$times(Userspace); - KResultOr sys$utime(Userspace pathname, size_t path_length, Userspace); - KResultOr sys$link(Userspace); - KResultOr sys$unlink(Userspace pathname, size_t path_length); - KResultOr sys$symlink(Userspace); - KResultOr sys$rmdir(Userspace pathname, size_t path_length); - KResultOr sys$mount(Userspace); - KResultOr sys$umount(Userspace mountpoint, size_t mountpoint_length); - KResultOr sys$chmod(Userspace pathname, size_t path_length, mode_t); - KResultOr sys$fchmod(int fd, mode_t); - KResultOr sys$chown(Userspace); - KResultOr sys$fchown(int fd, UserID, GroupID); - KResultOr sys$fsync(int fd); - KResultOr sys$socket(int domain, int type, int protocol); - KResultOr sys$bind(int sockfd, Userspace addr, socklen_t); - KResultOr sys$listen(int sockfd, int backlog); - KResultOr sys$accept4(Userspace); - KResultOr sys$connect(int sockfd, Userspace, socklen_t); - KResultOr sys$shutdown(int sockfd, int how); - KResultOr sys$sendmsg(int sockfd, Userspace, int flags); - KResultOr sys$recvmsg(int sockfd, Userspace, int flags); - KResultOr sys$getsockopt(Userspace); - KResultOr sys$setsockopt(Userspace); - KResultOr sys$getsockname(Userspace); - KResultOr sys$getpeername(Userspace); - KResultOr sys$socketpair(Userspace); - KResultOr sys$sched_setparam(pid_t pid, Userspace); - KResultOr sys$sched_getparam(pid_t pid, Userspace); - KResultOr sys$create_thread(void* (*)(void*), Userspace); + ErrorOr sys$sigreturn(RegisterState& registers); + ErrorOr sys$waitid(Userspace); + ErrorOr sys$mmap(Userspace); + ErrorOr sys$mremap(Userspace); + ErrorOr sys$munmap(Userspace, size_t); + ErrorOr sys$set_mmap_name(Userspace); + ErrorOr sys$mprotect(Userspace, size_t, int prot); + ErrorOr sys$madvise(Userspace, size_t, int advice); + ErrorOr sys$msyscall(Userspace); + ErrorOr sys$purge(int mode); + ErrorOr sys$select(Userspace); + ErrorOr sys$poll(Userspace); + ErrorOr sys$get_dir_entries(int fd, Userspace, size_t); + ErrorOr sys$getcwd(Userspace, size_t); + ErrorOr sys$chdir(Userspace, size_t); + ErrorOr sys$fchdir(int fd); + ErrorOr sys$adjtime(Userspace, Userspace); + ErrorOr sys$clock_gettime(clockid_t, Userspace); + ErrorOr sys$clock_settime(clockid_t, Userspace); + ErrorOr sys$clock_nanosleep(Userspace); + ErrorOr sys$gethostname(Userspace, size_t); + ErrorOr sys$sethostname(Userspace, size_t); + ErrorOr sys$uname(Userspace); + ErrorOr sys$readlink(Userspace); + ErrorOr sys$ttyname(int fd, Userspace, size_t); + ErrorOr sys$ptsname(int fd, Userspace, size_t); + ErrorOr sys$fork(RegisterState&); + ErrorOr sys$execve(Userspace); + ErrorOr sys$dup2(int old_fd, int new_fd); + ErrorOr sys$sigaction(int signum, Userspace act, Userspace old_act); + ErrorOr sys$sigprocmask(int how, Userspace set, Userspace old_set); + ErrorOr sys$sigpending(Userspace); + ErrorOr sys$getgroups(size_t, Userspace); + ErrorOr sys$setgroups(size_t, Userspace); + ErrorOr sys$pipe(int pipefd[2], int flags); + ErrorOr sys$killpg(pid_t pgrp, int sig); + ErrorOr sys$seteuid(UserID); + ErrorOr sys$setegid(GroupID); + ErrorOr sys$setuid(UserID); + ErrorOr sys$setgid(GroupID); + ErrorOr sys$setreuid(UserID, UserID); + ErrorOr sys$setresuid(UserID, UserID, UserID); + ErrorOr sys$setresgid(GroupID, GroupID, GroupID); + ErrorOr sys$alarm(unsigned seconds); + ErrorOr sys$access(Userspace pathname, size_t path_length, int mode); + ErrorOr sys$fcntl(int fd, int cmd, u32 extra_arg); + ErrorOr sys$ioctl(int fd, unsigned request, FlatPtr arg); + ErrorOr sys$mkdir(Userspace pathname, size_t path_length, mode_t mode); + ErrorOr sys$times(Userspace); + ErrorOr sys$utime(Userspace pathname, size_t path_length, Userspace); + ErrorOr sys$link(Userspace); + ErrorOr sys$unlink(Userspace pathname, size_t path_length); + ErrorOr sys$symlink(Userspace); + ErrorOr sys$rmdir(Userspace pathname, size_t path_length); + ErrorOr sys$mount(Userspace); + ErrorOr sys$umount(Userspace mountpoint, size_t mountpoint_length); + ErrorOr sys$chmod(Userspace pathname, size_t path_length, mode_t); + ErrorOr sys$fchmod(int fd, mode_t); + ErrorOr sys$chown(Userspace); + ErrorOr sys$fchown(int fd, UserID, GroupID); + ErrorOr sys$fsync(int fd); + ErrorOr sys$socket(int domain, int type, int protocol); + ErrorOr sys$bind(int sockfd, Userspace addr, socklen_t); + ErrorOr sys$listen(int sockfd, int backlog); + ErrorOr sys$accept4(Userspace); + ErrorOr sys$connect(int sockfd, Userspace, socklen_t); + ErrorOr sys$shutdown(int sockfd, int how); + ErrorOr sys$sendmsg(int sockfd, Userspace, int flags); + ErrorOr sys$recvmsg(int sockfd, Userspace, int flags); + ErrorOr sys$getsockopt(Userspace); + ErrorOr sys$setsockopt(Userspace); + ErrorOr sys$getsockname(Userspace); + ErrorOr sys$getpeername(Userspace); + ErrorOr sys$socketpair(Userspace); + ErrorOr sys$sched_setparam(pid_t pid, Userspace); + ErrorOr sys$sched_getparam(pid_t pid, Userspace); + ErrorOr sys$create_thread(void* (*)(void*), Userspace); [[noreturn]] void sys$exit_thread(Userspace, Userspace, size_t); - KResultOr sys$join_thread(pid_t tid, Userspace exit_value); - KResultOr sys$detach_thread(pid_t tid); - KResultOr sys$set_thread_name(pid_t tid, Userspace buffer, size_t buffer_size); - KResultOr sys$get_thread_name(pid_t tid, Userspace buffer, size_t buffer_size); - KResultOr sys$kill_thread(pid_t tid, int signal); - KResultOr sys$rename(Userspace); - KResultOr sys$mknod(Userspace); - KResultOr sys$realpath(Userspace); - KResultOr sys$getrandom(Userspace, size_t, unsigned int); - KResultOr sys$getkeymap(Userspace); - KResultOr sys$setkeymap(Userspace); - KResultOr sys$profiling_enable(pid_t, u64); - KResultOr sys$profiling_disable(pid_t); - KResultOr sys$profiling_free_buffer(pid_t); - KResultOr sys$futex(Userspace); - KResultOr sys$pledge(Userspace); - KResultOr sys$unveil(Userspace); - KResultOr sys$perf_event(int type, FlatPtr arg1, FlatPtr arg2); - KResultOr sys$perf_register_string(Userspace, size_t); - KResultOr sys$get_stack_bounds(Userspace stack_base, Userspace stack_size); - KResultOr sys$ptrace(Userspace); - KResultOr sys$sendfd(int sockfd, int fd); - KResultOr sys$recvfd(int sockfd, int options); - KResultOr sys$sysconf(int name); - KResultOr sys$disown(ProcessID); - KResultOr sys$allocate_tls(Userspace initial_data, size_t); - KResultOr sys$prctl(int option, FlatPtr arg1, FlatPtr arg2); - KResultOr sys$set_coredump_metadata(Userspace); - KResultOr sys$anon_create(size_t, int options); - KResultOr sys$statvfs(Userspace user_params); - KResultOr sys$fstatvfs(int fd, statvfs* buf); - KResultOr sys$map_time_page(); + ErrorOr sys$join_thread(pid_t tid, Userspace exit_value); + ErrorOr sys$detach_thread(pid_t tid); + ErrorOr sys$set_thread_name(pid_t tid, Userspace buffer, size_t buffer_size); + ErrorOr sys$get_thread_name(pid_t tid, Userspace buffer, size_t buffer_size); + ErrorOr sys$kill_thread(pid_t tid, int signal); + ErrorOr sys$rename(Userspace); + ErrorOr sys$mknod(Userspace); + ErrorOr sys$realpath(Userspace); + ErrorOr sys$getrandom(Userspace, size_t, unsigned int); + ErrorOr sys$getkeymap(Userspace); + ErrorOr sys$setkeymap(Userspace); + ErrorOr sys$profiling_enable(pid_t, u64); + ErrorOr sys$profiling_disable(pid_t); + ErrorOr sys$profiling_free_buffer(pid_t); + ErrorOr sys$futex(Userspace); + ErrorOr sys$pledge(Userspace); + ErrorOr sys$unveil(Userspace); + ErrorOr sys$perf_event(int type, FlatPtr arg1, FlatPtr arg2); + ErrorOr sys$perf_register_string(Userspace, size_t); + ErrorOr sys$get_stack_bounds(Userspace stack_base, Userspace stack_size); + ErrorOr sys$ptrace(Userspace); + ErrorOr sys$sendfd(int sockfd, int fd); + ErrorOr sys$recvfd(int sockfd, int options); + ErrorOr sys$sysconf(int name); + ErrorOr sys$disown(ProcessID); + ErrorOr sys$allocate_tls(Userspace initial_data, size_t); + ErrorOr sys$prctl(int option, FlatPtr arg1, FlatPtr arg2); + ErrorOr sys$set_coredump_metadata(Userspace); + ErrorOr sys$anon_create(size_t, int options); + ErrorOr sys$statvfs(Userspace user_params); + ErrorOr sys$fstatvfs(int fd, statvfs* buf); + ErrorOr sys$map_time_page(); template - KResult get_sock_or_peer_name(Params const&); + ErrorOr get_sock_or_peer_name(Params const&); static void initialize(); @@ -439,14 +439,14 @@ public: NonnullOwnPtrVector const& arguments() const { return m_arguments; }; NonnullOwnPtrVector const& environment() const { return m_environment; }; - KResult exec(NonnullOwnPtr path, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, int recusion_depth = 0); + ErrorOr exec(NonnullOwnPtr path, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, int recusion_depth = 0); - KResultOr load(NonnullRefPtr main_program_description, RefPtr interpreter_description, const ElfW(Ehdr) & main_program_header); + ErrorOr load(NonnullRefPtr main_program_description, RefPtr interpreter_description, const ElfW(Ehdr) & main_program_header); bool is_superuser() const { return euid() == 0; } void terminate_due_to_signal(u8 signal); - KResult send_signal(u8 signal, Process* sender); + ErrorOr send_signal(u8 signal, Process* sender); u8 termination_signal() const { return m_protected_values.termination_signal; } @@ -479,8 +479,8 @@ public: m_wait_for_tracer_at_next_execve = val; } - KResultOr peek_user_data(Userspace address); - KResult poke_user_data(Userspace address, u32 data); + ErrorOr peek_user_data(Userspace address); + ErrorOr poke_user_data(Userspace address, u32 data); void disowned_by_waiter(Process& process); void unblock_waiters(Thread::WaitBlocker::UnblockFlags, u8 signal = 0); @@ -495,8 +495,8 @@ public: } } - KResult set_coredump_property(NonnullOwnPtr key, NonnullOwnPtr value); - KResult try_set_coredump_property(StringView key, StringView value); + ErrorOr set_coredump_property(NonnullOwnPtr key, NonnullOwnPtr value); + ErrorOr try_set_coredump_property(StringView key, StringView value); const NonnullRefPtrVector& threads_for_coredump(Badge) const { return m_threads_for_coredump; } @@ -521,8 +521,8 @@ private: bool remove_thread(Thread&); Process(NonnullOwnPtr name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr cwd, RefPtr executable, TTY* tty); - static KResultOr> try_create(RefPtr& first_thread, NonnullOwnPtr name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr cwd = nullptr, RefPtr executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr); - KResult attach_resources(NonnullOwnPtr&&, RefPtr& first_thread, Process* fork_parent); + static ErrorOr> try_create(RefPtr& first_thread, NonnullOwnPtr name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr cwd = nullptr, RefPtr executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr); + ErrorOr attach_resources(NonnullOwnPtr&&, RefPtr& first_thread, Process* fork_parent); static ProcessID allocate_pid(); void kill_threads_except_self(); @@ -532,22 +532,22 @@ private: bool create_perf_events_buffer_if_needed(); void delete_perf_events_buffer(); - KResult do_exec(NonnullRefPtr main_program_description, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, RefPtr interpreter_description, Thread*& new_main_thread, u32& prev_flags, const ElfW(Ehdr) & main_program_header); - KResultOr do_write(OpenFileDescription&, const UserOrKernelBuffer&, size_t); + ErrorOr do_exec(NonnullRefPtr main_program_description, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, RefPtr interpreter_description, Thread*& new_main_thread, u32& prev_flags, const ElfW(Ehdr) & main_program_header); + ErrorOr do_write(OpenFileDescription&, const UserOrKernelBuffer&, size_t); - KResultOr do_statvfs(StringView path, statvfs* buf); + ErrorOr do_statvfs(StringView path, statvfs* buf); - KResultOr> find_elf_interpreter_for_executable(StringView path, ElfW(Ehdr) const& main_executable_header, size_t main_executable_header_size, size_t file_size); + ErrorOr> find_elf_interpreter_for_executable(StringView path, ElfW(Ehdr) const& main_executable_header, size_t main_executable_header_size, size_t file_size); - KResult do_kill(Process&, int signal); - KResult do_killpg(ProcessGroupID pgrp, int signal); - KResult do_killall(int signal); - KResult do_killself(int signal); + ErrorOr do_kill(Process&, int signal); + ErrorOr do_killpg(ProcessGroupID pgrp, int signal); + ErrorOr do_killall(int signal); + ErrorOr do_killself(int signal); - KResultOr do_waitid(Variant, NonnullRefPtr> waitee, int options); + ErrorOr do_waitid(Variant, NonnullRefPtr> waitee, int options); - KResultOr> get_syscall_path_argument(Userspace user_path, size_t path_length) const; - KResultOr> get_syscall_path_argument(const Syscall::StringArgument&) const; + ErrorOr> get_syscall_path_argument(Userspace user_path, size_t path_length) const; + ErrorOr> get_syscall_path_argument(const Syscall::StringArgument&) const; bool has_tracee_thread(ProcessID tracer_pid); @@ -557,20 +557,20 @@ private: public: NonnullRefPtr procfs_traits() const { return *m_procfs_traits; } - KResult procfs_get_fds_stats(KBufferBuilder& builder) const; - KResult procfs_get_perf_events(KBufferBuilder& builder) const; - KResult procfs_get_unveil_stats(KBufferBuilder& builder) const; - KResult procfs_get_pledge_stats(KBufferBuilder& builder) const; - KResult procfs_get_virtual_memory_stats(KBufferBuilder& builder) const; - KResult procfs_get_binary_link(KBufferBuilder& builder) const; - KResult procfs_get_current_work_directory_link(KBufferBuilder& builder) const; + ErrorOr procfs_get_fds_stats(KBufferBuilder& builder) const; + ErrorOr procfs_get_perf_events(KBufferBuilder& builder) const; + ErrorOr procfs_get_unveil_stats(KBufferBuilder& builder) const; + ErrorOr procfs_get_pledge_stats(KBufferBuilder& builder) const; + ErrorOr procfs_get_virtual_memory_stats(KBufferBuilder& builder) const; + ErrorOr procfs_get_binary_link(KBufferBuilder& builder) const; + ErrorOr procfs_get_current_work_directory_link(KBufferBuilder& builder) const; mode_t binary_link_required_mode() const; - KResultOr procfs_get_thread_stack(ThreadID thread_id, KBufferBuilder& builder) const; - KResult traverse_stacks_directory(unsigned fsid, Function callback) const; - KResultOr> lookup_stacks_directory(const ProcFS&, StringView name) const; - KResultOr procfs_get_file_description_link(unsigned fd, KBufferBuilder& builder) const; - KResult traverse_file_descriptions_directory(unsigned fsid, Function callback) const; - KResultOr> lookup_file_descriptions_directory(const ProcFS&, StringView name) const; + ErrorOr procfs_get_thread_stack(ThreadID thread_id, KBufferBuilder& builder) const; + ErrorOr traverse_stacks_directory(unsigned fsid, Function callback) const; + ErrorOr> lookup_stacks_directory(const ProcFS&, StringView name) const; + ErrorOr procfs_get_file_description_link(unsigned fd, KBufferBuilder& builder) const; + ErrorOr traverse_file_descriptions_directory(unsigned fsid, Function callback) const; + ErrorOr> lookup_file_descriptions_directory(const ProcFS&, StringView name) const; private: inline PerformanceEventBuffer* current_perf_events_buffer() @@ -638,7 +638,7 @@ public: ALWAYS_INLINE const OpenFileDescriptionAndFlags& operator[](size_t i) const { return at(i); } ALWAYS_INLINE OpenFileDescriptionAndFlags& operator[](size_t i) { return at(i); } - KResult try_clone(const Kernel::Process::OpenFileDescriptions& other) + ErrorOr try_clone(const Kernel::Process::OpenFileDescriptions& other) { SpinlockLocker lock_other(other.m_fds_lock); if (!try_resize(other.m_fds_metadatas.size())) @@ -647,7 +647,7 @@ public: for (size_t i = 0; i < other.m_fds_metadatas.size(); ++i) { m_fds_metadatas[i] = other.m_fds_metadatas[i]; } - return KSuccess; + return {}; } const OpenFileDescriptionAndFlags& at(size_t i) const; @@ -659,7 +659,7 @@ public: void enumerate(Function) const; void change_each(Function); - KResultOr allocate(int first_candidate_fd = 0); + ErrorOr allocate(int first_candidate_fd = 0); size_t open_count() const; bool try_resize(size_t size) { return m_fds_metadatas.try_resize(size); } @@ -675,7 +675,7 @@ public: m_fds_metadatas.clear(); } - KResultOr> open_file_description(int fd) const; + ErrorOr> open_file_description(int fd) const; private: OpenFileDescriptions() = default; @@ -717,14 +717,14 @@ public: class ProcessProcFSTraits : public ProcFSExposedComponent { public: - static KResultOr> try_create(Badge, Process& process) + static ErrorOr> try_create(Badge, Process& process) { return adopt_nonnull_ref_or_enomem(new (nothrow) ProcessProcFSTraits(process)); } virtual InodeIndex component_index() const override; - virtual KResultOr> to_inode(const ProcFS& procfs_instance) const override; - virtual KResult traverse_as_directory(unsigned, Function) const override; + virtual ErrorOr> to_inode(const ProcFS& procfs_instance) const override; + virtual ErrorOr traverse_as_directory(unsigned, Function) const override; virtual mode_t required_mode() const override { return 0555; } virtual UserID owner_user() const override; @@ -974,7 +974,7 @@ inline ProcessID Thread::pid() const #define VERIFY_NO_PROCESS_BIG_LOCK(process) \ VERIFY(!process->big_lock().is_locked_by_current_thread()); -inline static KResultOr> try_copy_kstring_from_user(const Kernel::Syscall::StringArgument& string) +inline static ErrorOr> try_copy_kstring_from_user(const Kernel::Syscall::StringArgument& string) { Userspace characters((FlatPtr)string.characters); return try_copy_kstring_from_user(characters, string.length); diff --git a/Kernel/ProcessExposed.cpp b/Kernel/ProcessExposed.cpp index 7db390ea9d4..d3d188bc9af 100644 --- a/Kernel/ProcessExposed.cpp +++ b/Kernel/ProcessExposed.cpp @@ -107,7 +107,7 @@ ProcFSExposedLink::ProcFSExposedLink(StringView name) : ProcFSExposedComponent(name) { } -KResultOr ProcFSGlobalInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const +ErrorOr ProcFSGlobalInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const { dbgln_if(PROCFS_DEBUG, "ProcFSGlobalInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count); @@ -115,13 +115,13 @@ KResultOr ProcFSGlobalInformation::read_bytes(off_t offset, size_t count VERIFY(buffer.user_or_kernel_ptr()); if (!description) - return KResult(EIO); + return Error::from_errno(EIO); MutexLocker locker(m_refresh_lock); if (!description->data()) { dbgln("ProcFSGlobalInformation: Do not have cached data!"); - return KResult(EIO); + return Error::from_errno(EIO); } auto& typed_cached_data = static_cast(*description->data()); @@ -135,7 +135,7 @@ KResultOr ProcFSGlobalInformation::read_bytes(off_t offset, size_t count return nread; } -KResult ProcFSGlobalInformation::refresh_data(OpenFileDescription& description) const +ErrorOr ProcFSGlobalInformation::refresh_data(OpenFileDescription& description) const { MutexLocker lock(m_refresh_lock); auto& cached_data = description.data(); @@ -150,15 +150,15 @@ KResult ProcFSGlobalInformation::refresh_data(OpenFileDescription& description) typed_cached_data.buffer = builder.build(); if (!typed_cached_data.buffer) return ENOMEM; - return KSuccess; + return {}; } -KResult ProcFSSystemBoolean::try_generate(KBufferBuilder& builder) +ErrorOr ProcFSSystemBoolean::try_generate(KBufferBuilder& builder) { return builder.appendff("{}\n", static_cast(value())); } -KResultOr ProcFSSystemBoolean::write_bytes(off_t, size_t count, const UserOrKernelBuffer& buffer, OpenFileDescription*) +ErrorOr ProcFSSystemBoolean::write_bytes(off_t, size_t count, const UserOrKernelBuffer& buffer, OpenFileDescription*) { if (count != 1) return EINVAL; @@ -174,45 +174,45 @@ KResultOr ProcFSSystemBoolean::write_bytes(off_t, size_t count, const Us return 1; } -KResult ProcFSSystemBoolean::truncate(u64 size) +ErrorOr ProcFSSystemBoolean::truncate(u64 size) { if (size != 0) return EPERM; - return KSuccess; + return {}; } -KResult ProcFSSystemBoolean::set_mtime(time_t) +ErrorOr ProcFSSystemBoolean::set_mtime(time_t) { - return KSuccess; + return {}; } -KResultOr ProcFSExposedLink::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const +ErrorOr ProcFSExposedLink::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const { VERIFY(offset == 0); MutexLocker locker(m_lock); auto builder = TRY(KBufferBuilder::try_create()); if (!const_cast(*this).acquire_link(builder)) - return KResult(EFAULT); + return Error::from_errno(EFAULT); auto blob = builder.build(); if (!blob) - return KResult(EFAULT); + return Error::from_errno(EFAULT); ssize_t nread = min(static_cast(blob->size() - offset), static_cast(count)); TRY(buffer.write(blob->data() + offset, nread)); return nread; } -KResultOr> ProcFSExposedLink::to_inode(const ProcFS& procfs_instance) const +ErrorOr> ProcFSExposedLink::to_inode(const ProcFS& procfs_instance) const { return TRY(ProcFSLinkInode::try_create(procfs_instance, *this)); } -KResultOr> ProcFSExposedComponent::to_inode(const ProcFS& procfs_instance) const +ErrorOr> ProcFSExposedComponent::to_inode(const ProcFS& procfs_instance) const { return TRY(ProcFSGlobalInode::try_create(procfs_instance, *this)); } -KResultOr> ProcFSExposedDirectory::to_inode(const ProcFS& procfs_instance) const +ErrorOr> ProcFSExposedDirectory::to_inode(const ProcFS& procfs_instance) const { return TRY(ProcFSDirectoryInode::try_create(procfs_instance, *this)); } @@ -222,7 +222,7 @@ void ProcFSExposedDirectory::add_component(const ProcFSExposedComponent&) TODO(); } -KResultOr> ProcFSExposedDirectory::lookup(StringView name) +ErrorOr> ProcFSExposedDirectory::lookup(StringView name) { for (auto& component : m_components) { if (component.name() == name) { @@ -232,12 +232,12 @@ KResultOr> ProcFSExposedDirectory::lookup( return ENOENT; } -KResult ProcFSExposedDirectory::traverse_as_directory(unsigned fsid, Function callback) const +ErrorOr ProcFSExposedDirectory::traverse_as_directory(unsigned fsid, Function callback) const { MutexLocker locker(ProcFSComponentRegistry::the().get_lock()); auto parent_directory = m_parent_directory.strong_ref(); if (parent_directory.is_null()) - return KResult(EINVAL); + return Error::from_errno(EINVAL); callback({ ".", { fsid, component_index() }, DT_DIR }); callback({ "..", { fsid, parent_directory->component_index() }, DT_DIR }); @@ -245,7 +245,7 @@ KResult ProcFSExposedDirectory::traverse_as_directory(unsigned fsid, Function #include #include #include #include #include -#include #include #include #include @@ -66,12 +66,12 @@ private: class ProcFSExposedComponent : public RefCounted { public: StringView name() const { return m_name->view(); } - virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { VERIFY_NOT_REACHED(); } - virtual KResult traverse_as_directory(unsigned, Function) const { VERIFY_NOT_REACHED(); } - virtual KResultOr> lookup(StringView) { VERIFY_NOT_REACHED(); }; - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) { return EROFS; } - virtual KResult truncate(u64) { return EPERM; } - virtual KResult set_mtime(time_t) { return ENOTIMPL; } + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { VERIFY_NOT_REACHED(); } + virtual ErrorOr traverse_as_directory(unsigned, Function) const { VERIFY_NOT_REACHED(); } + virtual ErrorOr> lookup(StringView) { VERIFY_NOT_REACHED(); }; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) { return EROFS; } + virtual ErrorOr truncate(u64) { return EPERM; } + virtual ErrorOr set_mtime(time_t) { return ENOTIMPL; } virtual mode_t required_mode() const { return 0444; } virtual UserID owner_user() const { return 0; } @@ -79,12 +79,12 @@ public: time_t modified_time() const { return TimeManagement::now().to_timeval().tv_sec; } virtual void prepare_for_deletion() { } - virtual KResult refresh_data(OpenFileDescription&) const + virtual ErrorOr refresh_data(OpenFileDescription&) const { - return KSuccess; + return {}; } - virtual KResultOr> to_inode(const ProcFS& procfs_instance) const; + virtual ErrorOr> to_inode(const ProcFS& procfs_instance) const; virtual InodeIndex component_index() const { return m_component_index; } @@ -105,8 +105,8 @@ class ProcFSExposedDirectory friend class ProcFSComponentRegistry; public: - virtual KResult traverse_as_directory(unsigned, Function) const override; - virtual KResultOr> lookup(StringView name) override; + virtual ErrorOr traverse_as_directory(unsigned, Function) const override; + virtual ErrorOr> lookup(StringView name) override; void add_component(const ProcFSExposedComponent&); virtual void prepare_for_deletion() override @@ -117,7 +117,7 @@ public: } virtual mode_t required_mode() const override { return 0555; } - virtual KResultOr> to_inode(const ProcFS& procfs_instance) const override final; + virtual ErrorOr> to_inode(const ProcFS& procfs_instance) const override final; protected: explicit ProcFSExposedDirectory(StringView name); @@ -128,9 +128,9 @@ protected: class ProcFSExposedLink : public ProcFSExposedComponent { public: - virtual KResultOr> to_inode(const ProcFS& procfs_instance) const override final; + virtual ErrorOr> to_inode(const ProcFS& procfs_instance) const override final; - virtual KResultOr read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const override; + virtual ErrorOr read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const override; protected: virtual bool acquire_link(KBufferBuilder& builder) = 0; @@ -142,12 +142,12 @@ class ProcFSRootDirectory final : public ProcFSExposedDirectory { friend class ProcFSComponentRegistry; public: - virtual KResultOr> lookup(StringView name) override; + virtual ErrorOr> lookup(StringView name) override; static NonnullRefPtr must_create(); virtual ~ProcFSRootDirectory(); private: - virtual KResult traverse_as_directory(unsigned, Function) const override; + virtual ErrorOr traverse_as_directory(unsigned, Function) const override; ProcFSRootDirectory(); }; @@ -159,7 +159,7 @@ class ProcFSGlobalInformation : public ProcFSExposedComponent { public: virtual ~ProcFSGlobalInformation() override {}; - virtual KResultOr read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const override; + virtual ErrorOr read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const override; virtual mode_t required_mode() const override { return 0444; } @@ -168,8 +168,8 @@ protected: : ProcFSExposedComponent(name) { } - virtual KResult refresh_data(OpenFileDescription&) const override; - virtual KResult try_generate(KBufferBuilder&) = 0; + virtual ErrorOr refresh_data(OpenFileDescription&) const override; + virtual ErrorOr try_generate(KBufferBuilder&) = 0; mutable Mutex m_refresh_lock; }; @@ -187,13 +187,13 @@ protected: private: // ^ProcFSGlobalInformation - virtual KResult try_generate(KBufferBuilder&) override final; + virtual ErrorOr try_generate(KBufferBuilder&) override final; // ^ProcFSExposedComponent - virtual KResultOr write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) override final; + virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*) override final; virtual mode_t required_mode() const override final { return 0644; } - virtual KResult truncate(u64) override final; - virtual KResult set_mtime(time_t) override final; + virtual ErrorOr truncate(u64) override final; + virtual ErrorOr set_mtime(time_t) override final; }; } diff --git a/Kernel/ProcessGroup.cpp b/Kernel/ProcessGroup.cpp index 086cf3491ac..d9b6aa60b89 100644 --- a/Kernel/ProcessGroup.cpp +++ b/Kernel/ProcessGroup.cpp @@ -24,7 +24,7 @@ ProcessGroup::~ProcessGroup() }); } -KResultOr> ProcessGroup::try_create(ProcessGroupID pgid) +ErrorOr> ProcessGroup::try_create(ProcessGroupID pgid) { auto process_group = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProcessGroup(pgid))); process_groups().with([&](auto& groups) { @@ -33,9 +33,9 @@ KResultOr> ProcessGroup::try_create(ProcessGroupID p return process_group; } -KResultOr> ProcessGroup::try_find_or_create(ProcessGroupID pgid) +ErrorOr> ProcessGroup::try_find_or_create(ProcessGroupID pgid) { - return process_groups().with([&](auto& groups) -> KResultOr> { + return process_groups().with([&](auto& groups) -> ErrorOr> { for (auto& group : groups) { if (group.pgid() == pgid) return group; diff --git a/Kernel/ProcessGroup.h b/Kernel/ProcessGroup.h index de5bf0ed215..5d9309ceb8a 100644 --- a/Kernel/ProcessGroup.h +++ b/Kernel/ProcessGroup.h @@ -24,8 +24,8 @@ class ProcessGroup public: ~ProcessGroup(); - static KResultOr> try_create(ProcessGroupID); - static KResultOr> try_find_or_create(ProcessGroupID); + static ErrorOr> try_create(ProcessGroupID); + static ErrorOr> try_find_or_create(ProcessGroupID); static RefPtr from_pgid(ProcessGroupID); const ProcessGroupID& pgid() const { return m_pgid; } diff --git a/Kernel/ProcessProcFSTraits.cpp b/Kernel/ProcessProcFSTraits.cpp index 2fdb453aaa7..d9592f4744f 100644 --- a/Kernel/ProcessProcFSTraits.cpp +++ b/Kernel/ProcessProcFSTraits.cpp @@ -36,7 +36,7 @@ InodeIndex Process::ProcessProcFSTraits::component_index() const return SegmentedProcFSIndex::build_segmented_index_for_pid_directory(process->pid()); } -KResultOr> Process::ProcessProcFSTraits::to_inode(const ProcFS& procfs_instance) const +ErrorOr> Process::ProcessProcFSTraits::to_inode(const ProcFS& procfs_instance) const { auto process = m_process.strong_ref(); if (!process) @@ -45,7 +45,7 @@ KResultOr> Process::ProcessProcFSTraits::to_inode(const Pro return TRY(ProcFSProcessDirectoryInode::try_create(procfs_instance, process->pid())); } -KResult Process::ProcessProcFSTraits::traverse_as_directory(unsigned fsid, Function callback) const +ErrorOr Process::ProcessProcFSTraits::traverse_as_directory(unsigned fsid, Function callback) const { auto process = m_process.strong_ref(); if (!process) @@ -62,7 +62,7 @@ KResult Process::ProcessProcFSTraits::traverse_as_directory(unsigned fsid, Funct callback({ "cwd", { fsid, SegmentedProcFSIndex::build_segmented_index_for_main_property_in_pid_directory(process->pid(), SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink) }, DT_LNK }); callback({ "perf_events", { fsid, SegmentedProcFSIndex::build_segmented_index_for_main_property_in_pid_directory(process->pid(), SegmentedProcFSIndex::MainProcessProperty::PerformanceEvents) }, DT_REG }); callback({ "vm", { fsid, SegmentedProcFSIndex::build_segmented_index_for_main_property_in_pid_directory(process->pid(), SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats) }, DT_REG }); - return KSuccess; + return {}; } } diff --git a/Kernel/ProcessSpecificExposed.cpp b/Kernel/ProcessSpecificExposed.cpp index f18c46ec709..2c3a27b8d6d 100644 --- a/Kernel/ProcessSpecificExposed.cpp +++ b/Kernel/ProcessSpecificExposed.cpp @@ -18,12 +18,12 @@ namespace Kernel { -KResultOr Process::procfs_get_thread_stack(ThreadID thread_id, KBufferBuilder& builder) const +ErrorOr Process::procfs_get_thread_stack(ThreadID thread_id, KBufferBuilder& builder) const { JsonArraySerializer array { builder }; auto thread = Thread::from_tid(thread_id); if (!thread) - return KResult(ESRCH); + return Error::from_errno(ESRCH); bool show_kernel_addresses = Process::current().is_superuser(); bool kernel_address_added = false; for (auto address : Processor::capture_stack_trace(*thread, 1024)) { @@ -37,10 +37,11 @@ KResultOr Process::procfs_get_thread_stack(ThreadID thread_id, KBufferBu } array.finish(); - return KSuccess; + // FIXME: This return value seems useless. + return 0; } -KResult Process::traverse_stacks_directory(unsigned fsid, Function callback) const +ErrorOr Process::traverse_stacks_directory(unsigned fsid, Function callback) const { callback({ ".", { fsid, SegmentedProcFSIndex::build_segmented_index_for_main_property(pid(), SegmentedProcFSIndex::ProcessSubDirectory::Stacks, SegmentedProcFSIndex::MainProcessProperty::Reserved) }, 0 }); callback({ "..", { fsid, m_procfs_traits->component_index() }, 0 }); @@ -50,12 +51,12 @@ KResult Process::traverse_stacks_directory(unsigned fsid, Function> Process::lookup_stacks_directory(const ProcFS& procfs, StringView name) const +ErrorOr> Process::lookup_stacks_directory(const ProcFS& procfs, StringView name) const { - KResultOr> thread_stack_inode { ENOENT }; + ErrorOr> thread_stack_inode { ENOENT }; // FIXME: Try to exit the loop earlier for_each_thread([&](const Thread& thread) { @@ -64,7 +65,7 @@ KResultOr> Process::lookup_stacks_directory(const ProcFS& p if (name.to_int() == tid) { auto maybe_inode = ProcFSProcessPropertyInode::try_create_for_thread_stack(procfs, thread.tid(), pid()); if (maybe_inode.is_error()) { - thread_stack_inode = maybe_inode.error(); + thread_stack_inode = maybe_inode.release_error(); return; } @@ -73,11 +74,11 @@ KResultOr> Process::lookup_stacks_directory(const ProcFS& p }); if (thread_stack_inode.is_error()) - return thread_stack_inode.error(); + return thread_stack_inode.release_error(); return thread_stack_inode.release_value(); } -KResultOr Process::procfs_get_file_description_link(unsigned fd, KBufferBuilder& builder) const +ErrorOr Process::procfs_get_file_description_link(unsigned fd, KBufferBuilder& builder) const { auto file_description = TRY(m_fds.open_file_description(fd)); // Note: These links are not guaranteed to point to actual VFS paths, just like in other kernels. @@ -86,7 +87,7 @@ KResultOr Process::procfs_get_file_description_link(unsigned fd, KBuffer return data->length(); } -KResult Process::traverse_file_descriptions_directory(unsigned fsid, Function callback) const +ErrorOr Process::traverse_file_descriptions_directory(unsigned fsid, Function callback) const { callback({ ".", { fsid, m_procfs_traits->component_index() }, 0 }); callback({ "..", { fsid, m_procfs_traits->component_index() }, 0 }); @@ -101,10 +102,10 @@ KResult Process::traverse_file_descriptions_directory(unsigned fsid, Function> Process::lookup_file_descriptions_directory(const ProcFS& procfs, StringView name) const +ErrorOr> Process::lookup_file_descriptions_directory(const ProcFS& procfs, StringView name) const { auto maybe_index = name.to_uint(); if (!maybe_index.has_value()) @@ -116,7 +117,7 @@ KResultOr> Process::lookup_file_descriptions_directory(cons return TRY(ProcFSProcessPropertyInode::try_create_for_file_description_link(procfs, *maybe_index, pid())); } -KResult Process::procfs_get_pledge_stats(KBufferBuilder& builder) const +ErrorOr Process::procfs_get_pledge_stats(KBufferBuilder& builder) const { JsonObjectSerializer obj { builder }; #define __ENUMERATE_PLEDGE_PROMISE(x) \ @@ -132,10 +133,10 @@ KResult Process::procfs_get_pledge_stats(KBufferBuilder& builder) const } #undef __ENUMERATE_PLEDGE_PROMISE obj.finish(); - return KSuccess; + return {}; } -KResult Process::procfs_get_unveil_stats(KBufferBuilder& builder) const +ErrorOr Process::procfs_get_unveil_stats(KBufferBuilder& builder) const { JsonArraySerializer array { builder }; for (auto& unveiled_path : unveiled_paths()) { @@ -157,25 +158,25 @@ KResult Process::procfs_get_unveil_stats(KBufferBuilder& builder) const obj.add("permissions", permissions_builder.to_string()); } array.finish(); - return KSuccess; + return {}; } -KResult Process::procfs_get_perf_events(KBufferBuilder& builder) const +ErrorOr Process::procfs_get_perf_events(KBufferBuilder& builder) const { InterruptDisabler disabler; if (!perf_events()) { dbgln("ProcFS: No perf events for {}", pid()); - return KResult(ENOBUFS); + return Error::from_errno(ENOBUFS); } return perf_events()->to_json(builder); } -KResult Process::procfs_get_fds_stats(KBufferBuilder& builder) const +ErrorOr Process::procfs_get_fds_stats(KBufferBuilder& builder) const { JsonArraySerializer array { builder }; if (fds().open_count() == 0) { array.finish(); - return KSuccess; + return {}; } size_t count = 0; @@ -209,10 +210,10 @@ KResult Process::procfs_get_fds_stats(KBufferBuilder& builder) const }); array.finish(); - return KSuccess; + return {}; } -KResult Process::procfs_get_virtual_memory_stats(KBufferBuilder& builder) const +ErrorOr Process::procfs_get_virtual_memory_stats(KBufferBuilder& builder) const { JsonArraySerializer array { builder }; { @@ -254,10 +255,10 @@ KResult Process::procfs_get_virtual_memory_stats(KBufferBuilder& builder) const } } array.finish(); - return KSuccess; + return {}; } -KResult Process::procfs_get_current_work_directory_link(KBufferBuilder& builder) const +ErrorOr Process::procfs_get_current_work_directory_link(KBufferBuilder& builder) const { return builder.append_bytes(const_cast(*this).current_directory().absolute_path().bytes()); } @@ -269,11 +270,11 @@ mode_t Process::binary_link_required_mode() const return m_procfs_traits->required_mode(); } -KResult Process::procfs_get_binary_link(KBufferBuilder& builder) const +ErrorOr Process::procfs_get_binary_link(KBufferBuilder& builder) const { auto* custody = executable(); if (!custody) - return KResult(ENOEXEC); + return Error::from_errno(ENOEXEC); return builder.append(custody->absolute_path().bytes()); } diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index 145f03d1b71..5c7c915bd9f 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -13,7 +13,7 @@ #include #include -Kernel::KResultOr> try_copy_kstring_from_user(Userspace user_str, size_t user_str_size) +ErrorOr> try_copy_kstring_from_user(Userspace user_str, size_t user_str_size) { bool is_user = Kernel::Memory::is_user_range(VirtualAddress(user_str), user_str_size); if (!is_user) @@ -40,14 +40,14 @@ Kernel::KResultOr> try_copy_kstring_from_user(Use return new_string; } -KResultOr