VirtualFileSystem.h 3.7 KB

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