From 47bfbe343bd7e0cc5d841c458fd1f6241acf2a90 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 6 Sep 2021 11:06:20 +0200 Subject: Kernel: Tidy up SysFS construction - Use KResultOr and TRY() to propagate errors - Check for OOM errors - Move allocation out of constructors There's still a lot more to do here, as SysFS is still quite brittle in the face of memory pressure. --- Kernel/FileSystem/SysFS.cpp | 16 ++++++++-------- Kernel/FileSystem/SysFS.h | 8 ++++---- Kernel/FileSystem/SysFSComponent.cpp | 8 ++++---- Kernel/FileSystem/SysFSComponent.h | 4 ++-- Kernel/Syscalls/mount.cpp | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Kernel/FileSystem/SysFS.cpp b/Kernel/FileSystem/SysFS.cpp index a7c769aca0..ded4365ff1 100644 --- a/Kernel/FileSystem/SysFS.cpp +++ b/Kernel/FileSystem/SysFS.cpp @@ -61,13 +61,12 @@ SysFSRootDirectory::SysFSRootDirectory() m_buses_directory = buses_directory; } -NonnullRefPtr SysFS::create() +KResultOr> SysFS::try_create() { - return adopt_ref(*new (nothrow) SysFS); + return adopt_nonnull_ref_or_enomem(new (nothrow) SysFS); } SysFS::SysFS() - : m_root_inode(SysFSComponentRegistry::the().root_directory().to_inode(*this)) { } @@ -77,6 +76,7 @@ SysFS::~SysFS() KResult SysFS::initialize() { + m_root_inode = TRY(SysFSComponentRegistry::the().root_directory().to_inode(*this)); return KSuccess; } @@ -85,9 +85,9 @@ Inode& SysFS::root_inode() return *m_root_inode; } -NonnullRefPtr SysFSInode::create(SysFS const& fs, SysFSComponent const& component) +KResultOr> SysFSInode::try_create(SysFS const& fs, SysFSComponent const& component) { - return adopt_ref(*new (nothrow) SysFSInode(fs, component)); + return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSInode(fs, component)); } SysFSInode::SysFSInode(SysFS const& fs, SysFSComponent const& component) @@ -179,9 +179,9 @@ KResult SysFSInode::truncate(u64) return EPERM; } -NonnullRefPtr SysFSDirectoryInode::create(SysFS const& sysfs, SysFSComponent const& component) +KResultOr> SysFSDirectoryInode::try_create(SysFS const& sysfs, SysFSComponent const& component) { - return adopt_ref(*new (nothrow) SysFSDirectoryInode(sysfs, component)); + return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSDirectoryInode(sysfs, component)); } SysFSDirectoryInode::SysFSDirectoryInode(SysFS const& fs, SysFSComponent const& component) @@ -217,7 +217,7 @@ KResultOr> SysFSDirectoryInode::lookup(StringView name) auto component = m_associated_component->lookup(name); if (!component) return ENOENT; - return component->to_inode(fs()); + return TRY(component->to_inode(fs())); } SysFSBusDirectory& SysFSComponentRegistry::buses_directory() diff --git a/Kernel/FileSystem/SysFS.h b/Kernel/FileSystem/SysFS.h index e6836343be..c7fe30c619 100644 --- a/Kernel/FileSystem/SysFS.h +++ b/Kernel/FileSystem/SysFS.h @@ -60,7 +60,7 @@ class SysFS final : public FileSystem { public: virtual ~SysFS() override; - static NonnullRefPtr create(); + static KResultOr> try_create(); virtual KResult initialize() override; virtual StringView class_name() const override { return "SysFS"sv; } @@ -70,7 +70,7 @@ public: private: SysFS(); - NonnullRefPtr m_root_inode; + RefPtr m_root_inode; }; class SysFSInode : public Inode { @@ -78,7 +78,7 @@ class SysFSInode : public Inode { friend class SysFSDirectoryInode; public: - static NonnullRefPtr create(SysFS const&, SysFSComponent const&); + static KResultOr> try_create(SysFS const&, SysFSComponent const&); StringView name() const { return m_associated_component->name(); } protected: @@ -106,7 +106,7 @@ class SysFSDirectoryInode : public SysFSInode { friend class SysFS; public: - static NonnullRefPtr create(SysFS const&, SysFSComponent const&); + static KResultOr> try_create(SysFS const&, SysFSComponent const&); virtual ~SysFSDirectoryInode() override; SysFS& fs() { return static_cast(Inode::fs()); } diff --git a/Kernel/FileSystem/SysFSComponent.cpp b/Kernel/FileSystem/SysFSComponent.cpp index 3e1d9eb8c1..7bf0278619 100644 --- a/Kernel/FileSystem/SysFSComponent.cpp +++ b/Kernel/FileSystem/SysFSComponent.cpp @@ -61,14 +61,14 @@ SysFSDirectory::SysFSDirectory(StringView name, SysFSDirectory const& parent_dir { } -NonnullRefPtr SysFSDirectory::to_inode(SysFS const& sysfs_instance) const +KResultOr> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const { - return SysFSDirectoryInode::create(sysfs_instance, *this); + return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this)); } -NonnullRefPtr SysFSComponent::to_inode(SysFS const& sysfs_instance) const +KResultOr> SysFSComponent::to_inode(SysFS const& sysfs_instance) const { - return SysFSInode::create(sysfs_instance, *this); + return SysFSInode::try_create(sysfs_instance, *this); } } diff --git a/Kernel/FileSystem/SysFSComponent.h b/Kernel/FileSystem/SysFSComponent.h index 7523c25714..c640df9ae5 100644 --- a/Kernel/FileSystem/SysFSComponent.h +++ b/Kernel/FileSystem/SysFSComponent.h @@ -32,7 +32,7 @@ public: virtual KResultOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, FileDescription*) { return EROFS; } virtual KResult refresh_data(FileDescription&) const { return KSuccess; } - virtual NonnullRefPtr to_inode(SysFS const&) const; + virtual KResultOr> to_inode(SysFS const&) const; InodeIndex component_index() const { return m_component_index; }; @@ -51,7 +51,7 @@ public: virtual KResult traverse_as_directory(unsigned, Function) const override; virtual RefPtr lookup(StringView name) override; - virtual NonnullRefPtr to_inode(SysFS const& sysfs_instance) const override final; + virtual KResultOr> to_inode(SysFS const& sysfs_instance) const override final; protected: explicit SysFSDirectory(StringView name); diff --git a/Kernel/Syscalls/mount.cpp b/Kernel/Syscalls/mount.cpp index e302df3bff..122fd64023 100644 --- a/Kernel/Syscalls/mount.cpp +++ b/Kernel/Syscalls/mount.cpp @@ -85,7 +85,7 @@ KResultOr Process::sys$mount(Userspace } else if (fs_type == "dev"sv || fs_type == "DevFS"sv) { fs = TRY(DevFS::try_create()); } else if (fs_type == "sys"sv || fs_type == "SysFS"sv) { - fs = SysFS::create(); + fs = TRY(SysFS::try_create()); } else if (fs_type == "tmp"sv || fs_type == "TmpFS"sv) { fs = TRY(TmpFS::try_create()); } else if (fs_type == "iso9660"sv || fs_type == "ISO9660FS"sv) { -- cgit v1.2.3