diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-06 11:06:20 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-06 13:06:05 +0200 |
commit | 47bfbe343bd7e0cc5d841c458fd1f6241acf2a90 (patch) | |
tree | cd3ebc0bfe0f5ab83c4eef66d0823b8d87ba55c7 | |
parent | 788b91a65cd8cea0d7a4899c5df1cc8a1c732640 (diff) | |
download | serenity-47bfbe343bd7e0cc5d841c458fd1f6241acf2a90.zip |
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.
-rw-r--r-- | Kernel/FileSystem/SysFS.cpp | 16 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFS.h | 8 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFSComponent.cpp | 8 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFSComponent.h | 4 | ||||
-rw-r--r-- | 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> SysFS::create() +KResultOr<NonnullRefPtr<SysFS>> 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> SysFSInode::create(SysFS const& fs, SysFSComponent const& component) +KResultOr<NonnullRefPtr<SysFSInode>> 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> SysFSDirectoryInode::create(SysFS const& sysfs, SysFSComponent const& component) +KResultOr<NonnullRefPtr<SysFSDirectoryInode>> 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<NonnullRefPtr<Inode>> 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<SysFS> create(); + static KResultOr<NonnullRefPtr<SysFS>> try_create(); virtual KResult initialize() override; virtual StringView class_name() const override { return "SysFS"sv; } @@ -70,7 +70,7 @@ public: private: SysFS(); - NonnullRefPtr<SysFSInode> m_root_inode; + RefPtr<SysFSInode> m_root_inode; }; class SysFSInode : public Inode { @@ -78,7 +78,7 @@ class SysFSInode : public Inode { friend class SysFSDirectoryInode; public: - static NonnullRefPtr<SysFSInode> create(SysFS const&, SysFSComponent const&); + static KResultOr<NonnullRefPtr<SysFSInode>> 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<SysFSDirectoryInode> create(SysFS const&, SysFSComponent const&); + static KResultOr<NonnullRefPtr<SysFSDirectoryInode>> try_create(SysFS const&, SysFSComponent const&); virtual ~SysFSDirectoryInode() override; SysFS& fs() { return static_cast<SysFS&>(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<SysFSInode> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const +KResultOr<NonnullRefPtr<SysFSInode>> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const { - return SysFSDirectoryInode::create(sysfs_instance, *this); + return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this)); } -NonnullRefPtr<SysFSInode> SysFSComponent::to_inode(SysFS const& sysfs_instance) const +KResultOr<NonnullRefPtr<SysFSInode>> 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<size_t> write_bytes(off_t, size_t, UserOrKernelBuffer const&, FileDescription*) { return EROFS; } virtual KResult refresh_data(FileDescription&) const { return KSuccess; } - virtual NonnullRefPtr<SysFSInode> to_inode(SysFS const&) const; + virtual KResultOr<NonnullRefPtr<SysFSInode>> 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<bool(FileSystem::DirectoryEntryView const&)>) const override; virtual RefPtr<SysFSComponent> lookup(StringView name) override; - virtual NonnullRefPtr<SysFSInode> to_inode(SysFS const& sysfs_instance) const override final; + virtual KResultOr<NonnullRefPtr<SysFSInode>> 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<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*> } 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) { |