VirtualFileSystem.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/NonnullOwnPtrVector.h>
  12. #include <AK/OwnPtr.h>
  13. #include <AK/RefPtr.h>
  14. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  15. #include <Kernel/FileSystem/FileSystem.h>
  16. #include <Kernel/FileSystem/InodeIdentifier.h>
  17. #include <Kernel/FileSystem/InodeMetadata.h>
  18. #include <Kernel/FileSystem/Mount.h>
  19. #include <Kernel/FileSystem/UnveilNode.h>
  20. #include <Kernel/Forward.h>
  21. #include <Kernel/Library/LockRefPtr.h>
  22. #include <Kernel/Locking/SpinlockProtected.h>
  23. namespace Kernel {
  24. // Kernel internal options.
  25. #define O_NOFOLLOW_NOERROR (1 << 29)
  26. #define O_UNLINK_INTERNAL (1 << 30)
  27. struct UidAndGid {
  28. UserID uid;
  29. GroupID gid;
  30. };
  31. enum class AccessFlags {
  32. None = 0,
  33. EffectiveAccess = 1 << 0,
  34. DoNotFollowSymlinks = 1 << 1,
  35. };
  36. AK_ENUM_BITWISE_OPERATORS(AccessFlags);
  37. class VirtualFileSystem {
  38. public:
  39. // Required to be at least 8 by POSIX
  40. // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html
  41. static constexpr int symlink_recursion_limit = 8;
  42. static void initialize();
  43. static VirtualFileSystem& the();
  44. VirtualFileSystem();
  45. ~VirtualFileSystem();
  46. ErrorOr<void> mount_root(FileSystem&);
  47. ErrorOr<void> mount(FileSystem&, Custody& mount_point, int flags);
  48. ErrorOr<void> bind_mount(Custody& source, Custody& mount_point, int flags);
  49. ErrorOr<void> remount(Custody& mount_point, int new_flags);
  50. ErrorOr<void> unmount(Custody& mount_point);
  51. ErrorOr<NonnullLockRefPtr<OpenFileDescription>> open(Credentials const&, StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  52. ErrorOr<NonnullLockRefPtr<OpenFileDescription>> create(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> rename(Credentials const&, Custody& old_base, StringView oldpath, Custody& new_base, StringView newpath);
  67. ErrorOr<void> mknod(Credentials const&, StringView path, mode_t, dev_t, Custody& base);
  68. ErrorOr<NonnullRefPtr<Custody>> open_directory(Credentials const&, StringView path, Custody& base);
  69. ErrorOr<void> for_each_mount(Function<ErrorOr<void>(Mount const&)>) const;
  70. ErrorOr<NonnullLockRefPtr<FileBackedFileSystem>> find_already_existing_or_create_file_backed_file_system(OpenFileDescription& description, Function<ErrorOr<NonnullLockRefPtr<FileSystem>>(OpenFileDescription&)> callback);
  71. InodeIdentifier root_inode_id() const;
  72. void sync_filesystems();
  73. void lock_all_filesystems();
  74. static void sync();
  75. NonnullRefPtr<Custody> root_custody();
  76. 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);
  77. 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);
  78. private:
  79. friend class OpenFileDescription;
  80. UnveilNode const& find_matching_unveiled_path(StringView path);
  81. ErrorOr<void> validate_path_against_process_veil(Custody const& path, int options);
  82. ErrorOr<void> validate_path_against_process_veil(StringView path, int options);
  83. bool is_vfs_root(InodeIdentifier) const;
  84. ErrorOr<void> traverse_directory_inode(Inode&, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>);
  85. bool mount_point_exists_at_inode(InodeIdentifier inode);
  86. // FIXME: These functions are totally unsafe as someone could unmount the returned Mount underneath us.
  87. Mount* find_mount_for_host(InodeIdentifier);
  88. Mount* find_mount_for_guest(InodeIdentifier);
  89. LockRefPtr<Inode> m_root_inode;
  90. SpinlockProtected<RefPtr<Custody>, LockRank::None> m_root_custody {};
  91. SpinlockProtected<IntrusiveList<&Mount::m_vfs_list_node>, LockRank::None> m_mounts {};
  92. SpinlockProtected<IntrusiveList<&FileBackedFileSystem::m_file_backed_file_system_node>, LockRank::None> m_file_backed_file_systems_list {};
  93. SpinlockProtected<IntrusiveList<&FileSystem::m_file_system_node>, LockRank::FileSystem> m_file_systems_list {};
  94. };
  95. }