VirtualFileSystem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/Function.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/NonnullOwnPtrVector.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/RefPtr.h>
  13. #include <AK/String.h>
  14. #include <Kernel/API/KResult.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/Locking/MutexProtected.h>
  22. namespace Kernel {
  23. // Kernel internal options.
  24. #define O_NOFOLLOW_NOERROR (1 << 29)
  25. #define O_UNLINK_INTERNAL (1 << 30)
  26. struct UidAndGid {
  27. UserID uid;
  28. GroupID gid;
  29. };
  30. class VirtualFileSystem {
  31. AK_MAKE_ETERNAL
  32. public:
  33. static void initialize();
  34. static VirtualFileSystem& the();
  35. VirtualFileSystem();
  36. ~VirtualFileSystem();
  37. KResult mount_root(FileSystem&);
  38. KResult mount(FileSystem&, Custody& mount_point, int flags);
  39. KResult bind_mount(Custody& source, Custody& mount_point, int flags);
  40. KResult remount(Custody& mount_point, int new_flags);
  41. KResult unmount(Inode& guest_inode);
  42. KResultOr<NonnullRefPtr<OpenFileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  43. KResultOr<NonnullRefPtr<OpenFileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  44. KResult mkdir(StringView path, mode_t mode, Custody& base);
  45. KResult link(StringView old_path, StringView new_path, Custody& base);
  46. KResult unlink(StringView path, Custody& base);
  47. KResult symlink(StringView target, StringView linkpath, Custody& base);
  48. KResult rmdir(StringView path, Custody& base);
  49. KResult chmod(StringView path, mode_t, Custody& base);
  50. KResult chmod(Custody&, mode_t);
  51. KResult chown(StringView path, UserID, GroupID, Custody& base);
  52. KResult chown(Custody&, UserID, GroupID);
  53. KResult access(StringView path, int mode, Custody& base);
  54. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  55. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  56. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  57. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  58. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  59. void for_each_mount(Function<IterationDecision(const Mount&)>) const;
  60. InodeIdentifier root_inode_id() const;
  61. static void sync();
  62. Custody& root_custody();
  63. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  64. KResultOr<NonnullRefPtr<Custody>> resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  65. private:
  66. friend class OpenFileDescription;
  67. UnveilNode const& find_matching_unveiled_path(StringView path);
  68. KResult validate_path_against_process_veil(Custody const& path, int options);
  69. KResult validate_path_against_process_veil(StringView path, int options);
  70. bool is_vfs_root(InodeIdentifier) const;
  71. KResult traverse_directory_inode(Inode&, Function<bool(FileSystem::DirectoryEntryView const&)>);
  72. Mount* find_mount_for_host(InodeIdentifier);
  73. Mount* find_mount_for_guest(InodeIdentifier);
  74. RefPtr<Inode> m_root_inode;
  75. RefPtr<Custody> m_root_custody;
  76. MutexProtected<Vector<Mount, 16>> m_mounts;
  77. };
  78. }