VirtualFileSystem.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. class VirtualFileSystem {
  32. public:
  33. // Required to be at least 8 by POSIX
  34. // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html
  35. static constexpr int symlink_recursion_limit = 8;
  36. static void initialize();
  37. static VirtualFileSystem& the();
  38. VirtualFileSystem();
  39. ~VirtualFileSystem();
  40. ErrorOr<void> mount_root(FileSystem&);
  41. ErrorOr<void> mount(FileSystem&, Custody& mount_point, int flags);
  42. ErrorOr<void> bind_mount(Custody& source, Custody& mount_point, int flags);
  43. ErrorOr<void> remount(Custody& mount_point, int new_flags);
  44. ErrorOr<void> unmount(Custody& mount_point);
  45. ErrorOr<NonnullLockRefPtr<OpenFileDescription>> open(Credentials const&, StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  46. ErrorOr<NonnullLockRefPtr<OpenFileDescription>> create(Credentials const&, StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  47. ErrorOr<void> mkdir(Credentials const&, StringView path, mode_t mode, Custody& base);
  48. ErrorOr<void> link(Credentials const&, StringView old_path, StringView new_path, Custody& base);
  49. ErrorOr<void> unlink(Credentials const&, StringView path, Custody& base);
  50. ErrorOr<void> symlink(Credentials const&, StringView target, StringView linkpath, Custody& base);
  51. ErrorOr<void> rmdir(Credentials const&, StringView path, Custody& base);
  52. ErrorOr<void> chmod(Credentials const&, StringView path, mode_t, Custody& base, int options = 0);
  53. ErrorOr<void> chmod(Credentials const&, Custody&, mode_t);
  54. ErrorOr<void> chown(Credentials const&, StringView path, UserID, GroupID, Custody& base, int options);
  55. ErrorOr<void> chown(Credentials const&, Custody&, UserID, GroupID);
  56. ErrorOr<void> access(Credentials const&, StringView path, int mode, Custody& base);
  57. ErrorOr<InodeMetadata> lookup_metadata(Credentials const&, StringView path, Custody& base, int options = 0);
  58. ErrorOr<void> utime(Credentials const&, StringView path, Custody& base, time_t atime, time_t mtime);
  59. ErrorOr<void> utimensat(Credentials const&, StringView path, Custody& base, timespec const& atime, timespec const& mtime, int options = 0);
  60. ErrorOr<void> rename(Credentials const&, Custody& old_base, StringView oldpath, Custody& new_base, StringView newpath);
  61. ErrorOr<void> mknod(Credentials const&, StringView path, mode_t, dev_t, Custody& base);
  62. ErrorOr<NonnullRefPtr<Custody>> open_directory(Credentials const&, StringView path, Custody& base);
  63. ErrorOr<void> for_each_mount(Function<ErrorOr<void>(Mount const&)>) const;
  64. ErrorOr<NonnullLockRefPtr<FileBackedFileSystem>> find_already_existing_or_create_file_backed_file_system(OpenFileDescription& description, Function<ErrorOr<NonnullLockRefPtr<FileSystem>>(OpenFileDescription&)> callback);
  65. InodeIdentifier root_inode_id() const;
  66. void sync_filesystems();
  67. void lock_all_filesystems();
  68. static void sync();
  69. NonnullRefPtr<Custody> root_custody();
  70. 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);
  71. 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);
  72. private:
  73. friend class OpenFileDescription;
  74. UnveilNode const& find_matching_unveiled_path(StringView path);
  75. ErrorOr<void> validate_path_against_process_veil(Custody const& path, int options);
  76. ErrorOr<void> validate_path_against_process_veil(StringView path, int options);
  77. bool is_vfs_root(InodeIdentifier) const;
  78. ErrorOr<void> traverse_directory_inode(Inode&, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>);
  79. bool mount_point_exists_at_inode(InodeIdentifier inode);
  80. // FIXME: These functions are totally unsafe as someone could unmount the returned Mount underneath us.
  81. Mount* find_mount_for_host(InodeIdentifier);
  82. Mount* find_mount_for_guest(InodeIdentifier);
  83. LockRefPtr<Inode> m_root_inode;
  84. SpinlockProtected<RefPtr<Custody>> m_root_custody;
  85. SpinlockProtected<IntrusiveList<&Mount::m_vfs_list_node>> m_mounts { LockRank::None };
  86. SpinlockProtected<IntrusiveList<&FileBackedFileSystem::m_file_backed_file_system_node>> m_file_backed_file_systems_list { LockRank::None };
  87. SpinlockProtected<IntrusiveList<&FileSystem::m_file_system_node>> m_file_systems_list { LockRank::FileSystem };
  88. };
  89. }