VirtualFileSystem.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/Error.h>
  9. #include <AK/Function.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/RefPtr.h>
  13. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  14. #include <Kernel/FileSystem/FileSystem.h>
  15. #include <Kernel/FileSystem/InodeIdentifier.h>
  16. #include <Kernel/FileSystem/InodeMetadata.h>
  17. #include <Kernel/FileSystem/Mount.h>
  18. #include <Kernel/FileSystem/UnveilNode.h>
  19. #include <Kernel/Forward.h>
  20. #include <Kernel/Locking/SpinlockProtected.h>
  21. namespace Kernel {
  22. // Kernel internal options.
  23. #define O_NOFOLLOW_NOERROR (1 << 29)
  24. #define O_UNLINK_INTERNAL (1 << 30)
  25. struct UidAndGid {
  26. UserID uid;
  27. GroupID gid;
  28. };
  29. enum class AccessFlags {
  30. None = 0,
  31. EffectiveAccess = 1 << 0,
  32. DoNotFollowSymlinks = 1 << 1,
  33. };
  34. AK_ENUM_BITWISE_OPERATORS(AccessFlags);
  35. class VirtualFileSystem {
  36. public:
  37. // Required to be at least 8 by POSIX
  38. // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html
  39. static constexpr int symlink_recursion_limit = 8;
  40. static void initialize();
  41. static VirtualFileSystem& the();
  42. VirtualFileSystem();
  43. ~VirtualFileSystem();
  44. ErrorOr<void> mount_root(FileSystem&);
  45. ErrorOr<void> mount(FileSystem&, Custody& mount_point, int flags);
  46. ErrorOr<void> bind_mount(Custody& source, Custody& mount_point, int flags);
  47. ErrorOr<void> remount(Custody& mount_point, int new_flags);
  48. ErrorOr<void> unmount(Custody& mount_point);
  49. ErrorOr<NonnullRefPtr<OpenFileDescription>> open(Credentials const&, StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  50. ErrorOr<NonnullRefPtr<OpenFileDescription>> open(Process const&, Credentials const&, StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  51. ErrorOr<NonnullRefPtr<OpenFileDescription>> create(Credentials const&, StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  52. ErrorOr<NonnullRefPtr<OpenFileDescription>> create(Process const&, Credentials const&, StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  53. ErrorOr<void> mkdir(Credentials const&, StringView path, mode_t mode, Custody& base);
  54. ErrorOr<void> link(Credentials const&, StringView old_path, StringView new_path, Custody& base);
  55. ErrorOr<void> unlink(Credentials const&, StringView path, Custody& base);
  56. ErrorOr<void> symlink(Credentials const&, StringView target, StringView linkpath, Custody& base);
  57. ErrorOr<void> rmdir(Credentials const&, StringView path, Custody& base);
  58. ErrorOr<void> chmod(Credentials const&, StringView path, mode_t, Custody& base, int options = 0);
  59. ErrorOr<void> chmod(Credentials const&, Custody&, mode_t);
  60. ErrorOr<void> chown(Credentials const&, StringView path, UserID, GroupID, Custody& base, int options);
  61. ErrorOr<void> chown(Credentials const&, Custody&, UserID, GroupID);
  62. ErrorOr<void> access(Credentials const&, StringView path, int mode, Custody& base, AccessFlags);
  63. ErrorOr<InodeMetadata> lookup_metadata(Credentials const&, StringView path, Custody& base, int options = 0);
  64. ErrorOr<void> utime(Credentials const&, StringView path, Custody& base, time_t atime, time_t mtime);
  65. ErrorOr<void> utimensat(Credentials const&, StringView path, Custody& base, timespec const& atime, timespec const& mtime, int options = 0);
  66. ErrorOr<void> do_utimens(Credentials const& credentials, Custody& custody, timespec const& atime, timespec const& mtime);
  67. ErrorOr<void> rename(Credentials const&, Custody& old_base, StringView oldpath, Custody& new_base, StringView newpath);
  68. ErrorOr<void> mknod(Credentials const&, StringView path, mode_t, dev_t, Custody& base);
  69. ErrorOr<NonnullRefPtr<Custody>> open_directory(Credentials const&, StringView path, Custody& base);
  70. ErrorOr<void> for_each_mount(Function<ErrorOr<void>(Mount const&)>) const;
  71. ErrorOr<NonnullRefPtr<FileBackedFileSystem>> find_already_existing_or_create_file_backed_file_system(OpenFileDescription& description, Function<ErrorOr<NonnullRefPtr<FileSystem>>(OpenFileDescription&)> callback);
  72. InodeIdentifier root_inode_id() const;
  73. void sync_filesystems();
  74. void lock_all_filesystems();
  75. static void sync();
  76. NonnullRefPtr<Custody> root_custody();
  77. ErrorOr<NonnullRefPtr<Custody>> resolve_path(Credentials const&, StringView path, NonnullRefPtr<Custody> base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  78. ErrorOr<NonnullRefPtr<Custody>> resolve_path(Process const&, Credentials const&, StringView path, NonnullRefPtr<Custody> base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  79. ErrorOr<NonnullRefPtr<Custody>> resolve_path_without_veil(Credentials const&, StringView path, NonnullRefPtr<Custody> base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  80. private:
  81. friend class OpenFileDescription;
  82. UnveilNode const& find_matching_unveiled_path(Process const&, StringView path);
  83. ErrorOr<void> validate_path_against_process_veil(Process const&, StringView path, int options);
  84. ErrorOr<void> validate_path_against_process_veil(Process const& process, Custody const& custody, int options);
  85. ErrorOr<void> validate_path_against_process_veil(Custody const& path, int options);
  86. ErrorOr<void> validate_path_against_process_veil(StringView path, int options);
  87. bool is_vfs_root(InodeIdentifier) const;
  88. ErrorOr<void> traverse_directory_inode(Inode&, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>);
  89. bool mount_point_exists_at_inode(InodeIdentifier inode);
  90. // FIXME: These functions are totally unsafe as someone could unmount the returned Mount underneath us.
  91. Mount* find_mount_for_host(InodeIdentifier);
  92. Mount* find_mount_for_guest(InodeIdentifier);
  93. RefPtr<Inode> m_root_inode;
  94. SpinlockProtected<RefPtr<Custody>, LockRank::None> m_root_custody {};
  95. SpinlockProtected<IntrusiveList<&Mount::m_vfs_list_node>, LockRank::None> m_mounts {};
  96. SpinlockProtected<IntrusiveList<&FileBackedFileSystem::m_file_backed_file_system_node>, LockRank::None> m_file_backed_file_systems_list {};
  97. SpinlockProtected<IntrusiveList<&FileSystem::m_file_system_node>, LockRank::FileSystem> m_file_systems_list {};
  98. };
  99. }