Kernel/TmpFS: Remove inode map from TmpFS

The HashMap of InodeIndex->Inode in TmpFS only had one purpose: looking
up parent inodes by index.

Instead of using a map for this, we can simply give each inode a WeakPtr
to its parent inode. This saves us the trouble of dealing with the
fallibility of HashMap allocations, and it just generally simpler. :^)
This commit is contained in:
Andreas Kling 2022-01-14 12:05:39 +01:00
parent 2d9bd24249
commit cda8f34876
Notes: sideshowbarker 2024-07-17 20:54:55 +09:00
2 changed files with 16 additions and 55 deletions

View file

@ -35,25 +35,6 @@ Inode& TmpFS::root_inode()
return *m_root_inode;
}
void TmpFS::register_inode(TmpFSInode& inode)
{
VERIFY(inode.identifier().fsid() == fsid());
Inode::all_instances().with([&](auto&) {
auto index = inode.identifier().index();
m_inodes.set(index, &inode);
});
}
void TmpFS::unregister_inode(InodeIdentifier identifier)
{
VERIFY(identifier.fsid() == fsid());
Inode::all_instances().with([&](auto&) {
m_inodes.remove(identifier.index());
});
}
unsigned TmpFS::next_inode_index()
{
MutexLocker locker(m_lock);
@ -61,21 +42,10 @@ unsigned TmpFS::next_inode_index()
return m_next_inode_index++;
}
ErrorOr<NonnullRefPtr<Inode>> TmpFS::get_inode(InodeIdentifier identifier) const
{
return Inode::all_instances().with([&](auto&) -> ErrorOr<NonnullRefPtr<Inode>> {
VERIFY(identifier.fsid() == fsid());
auto it = m_inodes.find(identifier.index());
if (it == m_inodes.end())
return ENOENT;
return *it->value;
});
}
TmpFSInode::TmpFSInode(TmpFS& fs, const InodeMetadata& metadata, InodeIdentifier parent)
TmpFSInode::TmpFSInode(TmpFS& fs, const InodeMetadata& metadata, WeakPtr<TmpFSInode> parent)
: Inode(fs, fs.next_inode_index())
, m_metadata(metadata)
, m_parent(parent)
, m_parent(move(parent))
{
m_metadata.inode = identifier();
}
@ -84,11 +54,9 @@ TmpFSInode::~TmpFSInode()
{
}
ErrorOr<NonnullRefPtr<TmpFSInode>> TmpFSInode::try_create(TmpFS& fs, InodeMetadata const& metadata, InodeIdentifier parent)
ErrorOr<NonnullRefPtr<TmpFSInode>> TmpFSInode::try_create(TmpFS& fs, InodeMetadata const& metadata, WeakPtr<TmpFSInode> parent)
{
auto inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TmpFSInode(fs, metadata, parent)));
fs.register_inode(inode);
return inode;
return adopt_nonnull_ref_or_enomem(new (nothrow) TmpFSInode(fs, metadata, move(parent)));
}
ErrorOr<NonnullRefPtr<TmpFSInode>> TmpFSInode::try_create_root(TmpFS& fs)
@ -99,7 +67,7 @@ ErrorOr<NonnullRefPtr<TmpFSInode>> TmpFSInode::try_create_root(TmpFS& fs)
metadata.ctime = now;
metadata.mtime = now;
metadata.mode = S_IFDIR | S_ISVTX | 0777;
return try_create(fs, metadata, { fs.fsid(), 1 });
return try_create(fs, metadata, {});
}
InodeMetadata TmpFSInode::metadata() const
@ -117,7 +85,8 @@ ErrorOr<void> TmpFSInode::traverse_as_directory(Function<ErrorOr<void>(FileSyste
return ENOTDIR;
TRY(callback({ ".", identifier(), 0 }));
TRY(callback({ "..", m_parent, 0 }));
if (auto parent = m_parent.strong_ref())
TRY(callback({ "..", parent->identifier(), 0 }));
for (auto& child : m_children) {
TRY(callback({ child.name->view(), child.inode->identifier(), 0 }));
@ -194,8 +163,11 @@ ErrorOr<NonnullRefPtr<Inode>> TmpFSInode::lookup(StringView name)
if (name == ".")
return *this;
if (name == "..")
return fs().get_inode(m_parent);
if (name == "..") {
if (auto parent = m_parent.strong_ref())
return parent.release_nonnull();
return ENOENT;
}
auto* child = find_child_by_name(name);
if (!child)
@ -260,7 +232,7 @@ ErrorOr<NonnullRefPtr<Inode>> TmpFSInode::create_child(StringView name, mode_t m
metadata.ctime = now;
metadata.mtime = now;
auto child = TRY(TmpFSInode::try_create(fs(), metadata, identifier()));
auto child = TRY(TmpFSInode::try_create(fs(), metadata, *this));
TRY(add_child(*child, name, mode));
return child;
}
@ -365,9 +337,4 @@ ErrorOr<void> TmpFSInode::set_mtime(time_t t)
return {};
}
void TmpFSInode::remove_from_secondary_lists()
{
fs().unregister_inode(identifier());
}
}

View file

@ -33,11 +33,6 @@ private:
RefPtr<TmpFSInode> m_root_inode;
HashMap<InodeIndex, TmpFSInode*> m_inodes;
ErrorOr<NonnullRefPtr<Inode>> get_inode(InodeIdentifier identifier) const;
void register_inode(TmpFSInode&);
void unregister_inode(InodeIdentifier);
unsigned m_next_inode_index { 1 };
unsigned next_inode_index();
};
@ -67,11 +62,10 @@ public:
virtual ErrorOr<void> set_atime(time_t) override;
virtual ErrorOr<void> set_ctime(time_t) override;
virtual ErrorOr<void> set_mtime(time_t) override;
virtual void remove_from_secondary_lists() override;
private:
TmpFSInode(TmpFS& fs, const InodeMetadata& metadata, InodeIdentifier parent);
static ErrorOr<NonnullRefPtr<TmpFSInode>> try_create(TmpFS&, InodeMetadata const& metadata, InodeIdentifier parent);
TmpFSInode(TmpFS& fs, const InodeMetadata& metadata, WeakPtr<TmpFSInode> parent);
static ErrorOr<NonnullRefPtr<TmpFSInode>> try_create(TmpFS&, InodeMetadata const& metadata, WeakPtr<TmpFSInode> parent);
static ErrorOr<NonnullRefPtr<TmpFSInode>> try_create_root(TmpFS&);
struct Child {
@ -84,7 +78,7 @@ private:
Child* find_child_by_name(StringView);
InodeMetadata m_metadata;
InodeIdentifier m_parent;
WeakPtr<TmpFSInode> m_parent;
OwnPtr<KBuffer> m_content;