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.
This commit is contained in:
Andreas Kling 2021-09-06 11:06:20 +02:00
parent 788b91a65c
commit 47bfbe343b
Notes: sideshowbarker 2024-07-18 04:37:35 +09:00
5 changed files with 19 additions and 19 deletions

View file

@ -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()

View file

@ -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()); }

View file

@ -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);
}
}

View file

@ -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);

View file

@ -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) {