VirtualFileSystem.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #include <AK/Badge.h>
  3. #include <AK/Function.h>
  4. #include <AK/HashMap.h>
  5. #include <AK/NonnullOwnPtrVector.h>
  6. #include <AK/OwnPtr.h>
  7. #include <AK/RefPtr.h>
  8. #include <AK/String.h>
  9. #include <Kernel/FileSystem/FileSystem.h>
  10. #include <Kernel/FileSystem/InodeIdentifier.h>
  11. #include <Kernel/FileSystem/InodeMetadata.h>
  12. #include <Kernel/KResult.h>
  13. #define O_RDONLY 0
  14. #define O_WRONLY 1
  15. #define O_RDWR 2
  16. #define O_EXEC 4
  17. #define O_CREAT 0100
  18. #define O_EXCL 0200
  19. #define O_NOCTTY 0400
  20. #define O_TRUNC 01000
  21. #define O_APPEND 02000
  22. #define O_NONBLOCK 04000
  23. #define O_DIRECTORY 00200000
  24. #define O_NOFOLLOW 00400000
  25. #define O_CLOEXEC 02000000
  26. #define O_DIRECT 04000000
  27. #define O_NOFOLLOW_NOERROR 0x4000000
  28. #define MS_NODEV 1
  29. #define MS_NOEXEC 2
  30. #define MS_NOSUID 4
  31. #define MS_BIND 8
  32. class Custody;
  33. class Device;
  34. class FileDescription;
  35. struct UidAndGid {
  36. uid_t uid;
  37. gid_t gid;
  38. };
  39. class VFS {
  40. AK_MAKE_ETERNAL
  41. public:
  42. class Mount {
  43. public:
  44. Mount(FS&, Custody* host_custody, int flags);
  45. Mount(Inode& source, Custody& host_custody, int flags);
  46. InodeIdentifier host() const;
  47. InodeIdentifier guest() const { return m_guest; }
  48. const FS& guest_fs() const { return *m_guest_fs; }
  49. String absolute_path() const;
  50. int flags() const { return m_flags; }
  51. private:
  52. InodeIdentifier m_host;
  53. InodeIdentifier m_guest;
  54. NonnullRefPtr<FS> m_guest_fs;
  55. RefPtr<Custody> m_host_custody;
  56. int m_flags;
  57. };
  58. static VFS& the();
  59. VFS();
  60. ~VFS();
  61. bool mount_root(FS&);
  62. KResult mount(FS&, Custody& mount_point, int flags);
  63. KResult bind_mount(Custody& source, Custody& mount_point, int flags);
  64. KResult unmount(InodeIdentifier guest_inode_id);
  65. KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  66. KResultOr<NonnullRefPtr<FileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  67. KResult mkdir(StringView path, mode_t mode, Custody& base);
  68. KResult link(StringView old_path, StringView new_path, Custody& base);
  69. KResult unlink(StringView path, Custody& base);
  70. KResult symlink(StringView target, StringView linkpath, Custody& base);
  71. KResult rmdir(StringView path, Custody& base);
  72. KResult chmod(StringView path, mode_t, Custody& base);
  73. KResult chmod(Inode&, mode_t);
  74. KResult chown(StringView path, uid_t, gid_t, Custody& base);
  75. KResult chown(Inode&, uid_t, gid_t);
  76. KResult access(StringView path, int mode, Custody& base);
  77. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  78. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  79. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  80. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  81. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  82. size_t mount_count() const { return m_mounts.size(); }
  83. void for_each_mount(Function<void(const Mount&)>) const;
  84. InodeIdentifier root_inode_id() const;
  85. void sync();
  86. Custody& root_custody();
  87. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  88. private:
  89. friend class FileDescription;
  90. RefPtr<Inode> get_inode(InodeIdentifier);
  91. bool is_vfs_root(InodeIdentifier) const;
  92. void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
  93. Mount* find_mount_for_host(InodeIdentifier);
  94. Mount* find_mount_for_guest(InodeIdentifier);
  95. Lock m_lock { "VFSLock" };
  96. RefPtr<Inode> m_root_inode;
  97. Vector<Mount> m_mounts;
  98. RefPtr<Custody> m_root_custody;
  99. };