1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 17:27:35 +00:00

Kernel: Simplify Mount internals

- The host custody never changes after initialization, so there's no
  need to protect it with a spinlock.

- To enforce the fact that some members don't change after
  initialization, make them const.
This commit is contained in:
Andreas Kling 2023-04-02 18:19:20 +02:00
parent 673592dea8
commit 19084ef743
2 changed files with 27 additions and 31 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -11,47 +11,41 @@
namespace Kernel { namespace Kernel {
Mount::Mount(NonnullRefPtr<FileSystem> guest_fs, Custody* host_custody, int flags) Mount::Mount(NonnullRefPtr<FileSystem> guest_fs, RefPtr<Custody> host_custody, int flags)
: m_guest(guest_fs->root_inode()) : m_guest_fs(move(guest_fs))
, m_guest_fs(guest_fs) , m_guest(m_guest_fs->root_inode())
, m_host_custody(host_custody) , m_host_custody(move(host_custody))
, m_flags(flags) , m_flags(flags)
{ {
} }
Mount::Mount(Inode& source, Custody& host_custody, int flags) Mount::Mount(NonnullRefPtr<Inode> source, NonnullRefPtr<Custody> host_custody, int flags)
: m_guest(source) : m_guest_fs(source->fs())
, m_guest_fs(source.fs()) , m_guest(move(source))
, m_host_custody(host_custody) , m_host_custody(move(host_custody))
, m_flags(flags) , m_flags(flags)
{ {
} }
ErrorOr<NonnullOwnPtr<KString>> Mount::absolute_path() const ErrorOr<NonnullOwnPtr<KString>> Mount::absolute_path() const
{ {
return m_host_custody.with([&](auto& host_custody) -> ErrorOr<NonnullOwnPtr<KString>> { if (!m_host_custody)
if (!host_custody) return KString::try_create("/"sv);
return KString::try_create("/"sv); return m_host_custody->try_serialize_absolute_path();
return host_custody->try_serialize_absolute_path();
});
} }
RefPtr<Inode> Mount::host() RefPtr<Inode> Mount::host()
{ {
return m_host_custody.with([](auto& host_custody) -> RefPtr<Inode> { if (!m_host_custody)
if (!host_custody) return nullptr;
return nullptr; return m_host_custody->inode();
return &host_custody->inode();
});
} }
RefPtr<Inode const> Mount::host() const RefPtr<Inode const> Mount::host() const
{ {
return m_host_custody.with([](auto& host_custody) -> RefPtr<Inode const> { if (!m_host_custody)
if (!host_custody) return nullptr;
return nullptr; return m_host_custody->inode();
return &host_custody->inode();
});
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -17,11 +17,13 @@ namespace Kernel {
class VirtualFileSystem; class VirtualFileSystem;
class Mount { class Mount {
AK_MAKE_NONCOPYABLE(Mount);
AK_MAKE_NONMOVABLE(Mount);
friend class VirtualFileSystem; friend class VirtualFileSystem;
public: public:
Mount(NonnullRefPtr<FileSystem>, Custody* host_custody, int flags); Mount(NonnullRefPtr<FileSystem>, RefPtr<Custody> host_custody, int flags);
Mount(Inode& source, Custody& host_custody, int flags); Mount(NonnullRefPtr<Inode> source, NonnullRefPtr<Custody> host_custody, int flags);
RefPtr<Inode const> host() const; RefPtr<Inode const> host() const;
RefPtr<Inode> host(); RefPtr<Inode> host();
@ -38,10 +40,10 @@ public:
void set_flags(int flags) { m_flags = flags; } void set_flags(int flags) { m_flags = flags; }
private: private:
NonnullRefPtr<Inode> m_guest; NonnullRefPtr<FileSystem> const m_guest_fs;
NonnullRefPtr<FileSystem> m_guest_fs; NonnullRefPtr<Inode> const m_guest;
SpinlockProtected<RefPtr<Custody>, LockRank::None> m_host_custody; RefPtr<Custody> const m_host_custody;
int m_flags; int m_flags { 0 };
IntrusiveListNode<Mount> m_vfs_list_node; IntrusiveListNode<Mount> m_vfs_list_node;
}; };