VirtualFileSystem.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/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. class VirtualFileSystem {
  30. public:
  31. // Required to be at least 8 by POSIX
  32. // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html
  33. static constexpr int symlink_recursion_limit = 8;
  34. static void initialize();
  35. static VirtualFileSystem& the();
  36. VirtualFileSystem();
  37. ~VirtualFileSystem();
  38. ErrorOr<void> mount_root(FileSystem&);
  39. ErrorOr<void> mount(FileSystem&, Custody& mount_point, int flags);
  40. ErrorOr<void> bind_mount(Custody& source, Custody& mount_point, int flags);
  41. ErrorOr<void> remount(Custody& mount_point, int new_flags);
  42. ErrorOr<void> unmount(Inode& guest_inode);
  43. ErrorOr<NonnullRefPtr<OpenFileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  44. ErrorOr<NonnullRefPtr<OpenFileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  45. ErrorOr<void> mkdir(StringView path, mode_t mode, Custody& base);
  46. ErrorOr<void> link(StringView old_path, StringView new_path, Custody& base);
  47. ErrorOr<void> unlink(StringView path, Custody& base);
  48. ErrorOr<void> symlink(StringView target, StringView linkpath, Custody& base);
  49. ErrorOr<void> rmdir(StringView path, Custody& base);
  50. ErrorOr<void> chmod(StringView path, mode_t, Custody& base, int options = 0);
  51. ErrorOr<void> chmod(Custody&, mode_t);
  52. ErrorOr<void> chown(StringView path, UserID, GroupID, Custody& base, int options);
  53. ErrorOr<void> chown(Custody&, UserID, GroupID);
  54. ErrorOr<void> access(StringView path, int mode, Custody& base);
  55. ErrorOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  56. ErrorOr<void> utime(StringView path, Custody& base, time_t atime, time_t mtime);
  57. ErrorOr<void> utimensat(StringView path, Custody& base, timespec const& atime, timespec const& mtime, int options = 0);
  58. ErrorOr<void> rename(StringView oldpath, StringView newpath, Custody& base);
  59. ErrorOr<void> mknod(StringView path, mode_t, dev_t, Custody& base);
  60. ErrorOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  61. ErrorOr<void> for_each_mount(Function<ErrorOr<void>(Mount const&)>) const;
  62. InodeIdentifier root_inode_id() const;
  63. static void sync();
  64. Custody& root_custody();
  65. ErrorOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  66. ErrorOr<NonnullRefPtr<Custody>> resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  67. private:
  68. friend class OpenFileDescription;
  69. UnveilNode const& find_matching_unveiled_path(StringView path);
  70. ErrorOr<void> validate_path_against_process_veil(Custody const& path, int options);
  71. ErrorOr<void> validate_path_against_process_veil(StringView path, int options);
  72. bool is_vfs_root(InodeIdentifier) const;
  73. ErrorOr<void> traverse_directory_inode(Inode&, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>);
  74. bool mount_point_exists_at_inode(InodeIdentifier inode);
  75. Mount* find_mount_for_host(InodeIdentifier);
  76. Mount* find_mount_for_guest(InodeIdentifier);
  77. RefPtr<Inode> m_root_inode;
  78. RefPtr<Custody> m_root_custody;
  79. SpinlockProtected<Vector<Mount, 16>> m_mounts;
  80. };
  81. }