1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:28:12 +00:00

Kernel: Make FileSystem::get_inode() return KResultOr<NRP<Inode>>

This allows for natural error propagation in a bunch of new places.
This commit is contained in:
Andreas Kling 2021-09-05 18:55:55 +02:00
parent 12d9a6c1fa
commit caaeae9607
8 changed files with 33 additions and 49 deletions

View file

@ -65,7 +65,7 @@ Inode& DevFS::root_inode()
return *m_root_inode; return *m_root_inode;
} }
RefPtr<Inode> DevFS::get_inode(InodeIdentifier inode_id) const KResultOr<NonnullRefPtr<Inode>> DevFS::get_inode(InodeIdentifier inode_id) const
{ {
MutexLocker locker(m_lock); MutexLocker locker(m_lock);
if (inode_id.index() == 1) if (inode_id.index() == 1)
@ -74,7 +74,7 @@ RefPtr<Inode> DevFS::get_inode(InodeIdentifier inode_id) const
if (inode_id.index() == node.index()) if (inode_id.index() == node.index())
return node; return node;
} }
return nullptr; return ENOENT;
} }
DevFSInode::DevFSInode(DevFS& fs) DevFSInode::DevFSInode(DevFS& fs)

View file

@ -32,7 +32,7 @@ public:
private: private:
DevFS(); DevFS();
RefPtr<Inode> get_inode(InodeIdentifier) const; KResultOr<NonnullRefPtr<Inode>> get_inode(InodeIdentifier) const;
size_t allocate_inode_index(); size_t allocate_inode_index();
NonnullRefPtr<DevFSRootDirectoryInode> m_root_inode; NonnullRefPtr<DevFSRootDirectoryInode> m_root_inode;

View file

@ -55,15 +55,16 @@ Inode& DevPtsFS::root_inode()
return *m_root_inode; return *m_root_inode;
} }
RefPtr<Inode> DevPtsFS::get_inode(InodeIdentifier inode_id) const KResultOr<NonnullRefPtr<Inode>> DevPtsFS::get_inode(InodeIdentifier inode_id) const
{ {
if (inode_id.index() == 1) if (inode_id.index() == 1)
return m_root_inode; return *m_root_inode;
unsigned pty_index = inode_index_to_pty_index(inode_id.index()); unsigned pty_index = inode_index_to_pty_index(inode_id.index());
auto* device = Device::get_device(201, pty_index); auto* device = Device::get_device(201, pty_index);
VERIFY(device); VERIFY(device);
// FIXME: Handle OOM
auto inode = adopt_ref(*new DevPtsFSInode(const_cast<DevPtsFS&>(*this), inode_id.index(), static_cast<SlavePTY*>(device))); auto inode = adopt_ref(*new DevPtsFSInode(const_cast<DevPtsFS&>(*this), inode_id.index(), static_cast<SlavePTY*>(device)));
inode->m_metadata.inode = inode_id; inode->m_metadata.inode = inode_id;
inode->m_metadata.size = 0; inode->m_metadata.size = 0;
@ -142,10 +143,7 @@ KResultOr<NonnullRefPtr<Inode>> DevPtsFSInode::lookup(StringView name)
for (SlavePTY& slave_pty : list) { for (SlavePTY& slave_pty : list) {
if (slave_pty.index() != pty_index.value()) if (slave_pty.index() != pty_index.value())
continue; continue;
auto inode = fs().get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) }); return fs().get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) });
if (!inode)
return ENOENT;
return inode.release_nonnull();
} }
return ENOENT; return ENOENT;
}); });

View file

@ -29,7 +29,7 @@ public:
private: private:
DevPtsFS(); DevPtsFS();
RefPtr<Inode> get_inode(InodeIdentifier) const; KResultOr<NonnullRefPtr<Inode>> get_inode(InodeIdentifier) const;
RefPtr<DevPtsFSInode> m_root_inode; RefPtr<DevPtsFSInode> m_root_inode;
}; };

View file

@ -147,12 +147,7 @@ KResult Ext2FS::initialize()
} }
} }
m_root_inode = static_ptr_cast<Ext2FSInode>(get_inode({ fsid(), EXT2_ROOT_INO })); m_root_inode = static_ptr_cast<Ext2FSInode>(TRY(get_inode({ fsid(), EXT2_ROOT_INO })));
if (!m_root_inode) {
dbgln("Ext2FS: failed to acquire root inode");
return EINVAL;
}
return KSuccess; return KSuccess;
} }
@ -781,37 +776,37 @@ void Ext2FSInode::flush_metadata()
set_metadata_dirty(false); set_metadata_dirty(false);
} }
RefPtr<Inode> Ext2FS::get_inode(InodeIdentifier inode) const KResultOr<NonnullRefPtr<Inode>> Ext2FS::get_inode(InodeIdentifier inode) const
{ {
MutexLocker locker(m_lock); MutexLocker locker(m_lock);
VERIFY(inode.fsid() == fsid()); VERIFY(inode.fsid() == fsid());
{ {
auto it = m_inode_cache.find(inode.index()); auto it = m_inode_cache.find(inode.index());
if (it != m_inode_cache.end()) if (it != m_inode_cache.end()) {
return (*it).value; if (!it->value)
return ENOENT;
return *it->value;
}
} }
auto state_or_error = get_inode_allocation_state(inode.index()); auto inode_allocation_state = TRY(get_inode_allocation_state(inode.index()));
if (state_or_error.is_error())
return {};
if (!state_or_error.value()) { if (!inode_allocation_state) {
m_inode_cache.set(inode.index(), nullptr); m_inode_cache.set(inode.index(), nullptr);
return {}; return ENOENT;
} }
BlockIndex block_index; BlockIndex block_index;
unsigned offset; unsigned offset;
if (!find_block_containing_inode(inode.index(), block_index, offset)) if (!find_block_containing_inode(inode.index(), block_index, offset))
return {}; return EINVAL;
auto new_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index())));
auto new_inode = adopt_ref(*new Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index()));
auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(&new_inode->m_raw_inode)); auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(&new_inode->m_raw_inode));
if (auto result = read_block(block_index, &buffer, sizeof(ext2_inode), offset); result.is_error()) { TRY(read_block(block_index, &buffer, sizeof(ext2_inode), offset));
// FIXME: Propagate the actual error.
return nullptr;
}
m_inode_cache.set(inode.index(), new_inode); m_inode_cache.set(inode.index(), new_inode);
return new_inode; return new_inode;
} }
@ -1218,7 +1213,7 @@ KResult Ext2FSInode::remove_child(const StringView& name)
m_lookup_cache.remove(name); m_lookup_cache.remove(name);
auto child_inode = fs().get_inode(child_id); auto child_inode = TRY(fs().get_inode(child_id));
TRY(child_inode->decrement_link_count()); TRY(child_inode->decrement_link_count());
did_remove_child(child_id, name); did_remove_child(child_id, name);
@ -1541,12 +1536,11 @@ KResultOr<NonnullRefPtr<Inode>> Ext2FS::create_inode(Ext2FSInode& parent_inode,
auto success = write_ext2_inode(inode_id, e2inode); auto success = write_ext2_inode(inode_id, e2inode);
VERIFY(success); VERIFY(success);
auto new_inode = get_inode({ fsid(), inode_id }); auto new_inode = TRY(get_inode({ fsid(), inode_id }));
VERIFY(new_inode);
dbgln_if(EXT2_DEBUG, "Ext2FS: Adding inode '{}' (mode {:o}) to parent directory {}", name, mode, parent_inode.index()); dbgln_if(EXT2_DEBUG, "Ext2FS: Adding inode '{}' (mode {:o}) to parent directory {}", name, mode, parent_inode.index());
TRY(parent_inode.add_child(*new_inode, name, mode)); TRY(parent_inode.add_child(*new_inode, name, mode));
return new_inode.release_nonnull(); return new_inode;
} }
KResult Ext2FSInode::populate_lookup_cache() const KResult Ext2FSInode::populate_lookup_cache() const
@ -1586,10 +1580,7 @@ KResultOr<NonnullRefPtr<Inode>> Ext2FSInode::lookup(StringView name)
inode_index = it->value; inode_index = it->value;
} }
auto inode = fs().get_inode({ fsid(), inode_index }); return fs().get_inode({ fsid(), inode_index });
if (!inode)
return ENOENT;
return inode.release_nonnull();
} }
void Ext2FSInode::one_ref_left() void Ext2FSInode::one_ref_left()

View file

@ -129,7 +129,7 @@ private:
virtual StringView class_name() const override { return "Ext2FS"sv; } virtual StringView class_name() const override { return "Ext2FS"sv; }
virtual Ext2FSInode& root_inode() override; virtual Ext2FSInode& root_inode() override;
RefPtr<Inode> get_inode(InodeIdentifier) const; KResultOr<NonnullRefPtr<Inode>> get_inode(InodeIdentifier) const;
KResultOr<NonnullRefPtr<Inode>> create_inode(Ext2FSInode& parent_inode, StringView name, mode_t, dev_t, UserID, GroupID); KResultOr<NonnullRefPtr<Inode>> 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); KResult create_directory(Ext2FSInode& parent_inode, StringView name, mode_t, UserID, GroupID);
virtual void flush_writes() override; virtual void flush_writes() override;

View file

@ -61,14 +61,14 @@ unsigned TmpFS::next_inode_index()
return m_next_inode_index++; return m_next_inode_index++;
} }
RefPtr<Inode> TmpFS::get_inode(InodeIdentifier identifier) const KResultOr<NonnullRefPtr<Inode>> TmpFS::get_inode(InodeIdentifier identifier) const
{ {
MutexLocker locker(m_lock, Mutex::Mode::Shared); MutexLocker locker(m_lock, Mutex::Mode::Shared);
VERIFY(identifier.fsid() == fsid()); VERIFY(identifier.fsid() == fsid());
auto it = m_inodes.find(identifier.index()); auto it = m_inodes.find(identifier.index());
if (it == m_inodes.end()) if (it == m_inodes.end())
return nullptr; return ENOENT;
return it->value; return it->value;
} }
@ -201,13 +201,8 @@ KResultOr<NonnullRefPtr<Inode>> TmpFSInode::lookup(StringView name)
if (name == ".") if (name == ".")
return *this; return *this;
if (name == "..") { if (name == "..")
auto inode = fs().get_inode(m_parent); return fs().get_inode(m_parent);
// FIXME: If this cannot fail, we should probably VERIFY here instead.
if (!inode)
return ENOENT;
return inode.release_nonnull();
}
auto* child = find_child_by_name(name); auto* child = find_child_by_name(name);
if (!child) if (!child)

View file

@ -34,7 +34,7 @@ private:
RefPtr<TmpFSInode> m_root_inode; RefPtr<TmpFSInode> m_root_inode;
HashMap<InodeIndex, NonnullRefPtr<TmpFSInode>> m_inodes; HashMap<InodeIndex, NonnullRefPtr<TmpFSInode>> m_inodes;
RefPtr<Inode> get_inode(InodeIdentifier identifier) const; KResultOr<NonnullRefPtr<Inode>> get_inode(InodeIdentifier identifier) const;
void register_inode(TmpFSInode&); void register_inode(TmpFSInode&);
void unregister_inode(InodeIdentifier); void unregister_inode(InodeIdentifier);