VirtualFileSystem.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #include <AK/HashMap.h>
  3. #include <AK/OwnPtr.h>
  4. #include <AK/RetainPtr.h>
  5. #include <AK/AKString.h>
  6. #include <AK/Vector.h>
  7. #include <AK/Function.h>
  8. #include "InodeIdentifier.h"
  9. #include "InodeMetadata.h"
  10. #include "FileSystem.h"
  11. #include <Kernel/KResult.h>
  12. #define O_RDONLY 0
  13. #define O_WRONLY 1
  14. #define O_RDWR 2
  15. #define O_CREAT 0100
  16. #define O_EXCL 0200
  17. #define O_NOCTTY 0400
  18. #define O_TRUNC 01000
  19. #define O_APPEND 02000
  20. #define O_NONBLOCK 04000
  21. #define O_DIRECTORY 00200000
  22. #define O_NOFOLLOW 00400000
  23. #define O_CLOEXEC 02000000
  24. #define O_NOFOLLOW_NOERROR 0x4000000
  25. class Device;
  26. class FileDescriptor;
  27. inline constexpr dword encoded_device(unsigned major, unsigned minor)
  28. {
  29. return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
  30. }
  31. class VFS;
  32. class VFS {
  33. AK_MAKE_ETERNAL
  34. public:
  35. class Mount {
  36. public:
  37. Mount(InodeIdentifier host, RetainPtr<FS>&&);
  38. InodeIdentifier host() const { return m_host; }
  39. InodeIdentifier guest() const { return m_guest; }
  40. const FS& guest_fs() const { return *m_guest_fs; }
  41. private:
  42. InodeIdentifier m_host;
  43. InodeIdentifier m_guest;
  44. RetainPtr<FS> m_guest_fs;
  45. };
  46. [[gnu::pure]] static VFS& the();
  47. VFS();
  48. ~VFS();
  49. bool mount_root(RetainPtr<FS>&&);
  50. bool mount(RetainPtr<FS>&&, StringView path);
  51. KResultOr<Retained<FileDescriptor>> open(RetainPtr<Device>&&, int options);
  52. KResultOr<Retained<FileDescriptor>> open(StringView path, int options, mode_t mode, Inode& base);
  53. KResultOr<Retained<FileDescriptor>> create(StringView path, int options, mode_t mode, Inode& base);
  54. KResult mkdir(StringView path, mode_t mode, Inode& base);
  55. KResult link(StringView old_path, StringView new_path, Inode& base);
  56. KResult unlink(StringView path, Inode& base);
  57. KResult symlink(StringView target, StringView linkpath, Inode& base);
  58. KResult rmdir(StringView path, Inode& base);
  59. KResult chmod(StringView path, mode_t, Inode& base);
  60. KResult chmod(Inode&, mode_t);
  61. KResult chown(StringView path, uid_t, gid_t, Inode& base);
  62. KResult access(StringView path, int mode, Inode& base);
  63. KResult stat(StringView path, int options, Inode& base, struct stat&);
  64. KResult utime(StringView path, Inode& base, time_t atime, time_t mtime);
  65. KResult rename(StringView oldpath, StringView newpath, Inode& base);
  66. KResultOr<Retained<Inode>> open_directory(StringView path, Inode& base);
  67. void register_device(Device&);
  68. void unregister_device(Device&);
  69. size_t mount_count() const { return m_mounts.size(); }
  70. void for_each_mount(Function<void(const Mount&)>) const;
  71. KResultOr<String> absolute_path(Inode&);
  72. KResultOr<String> absolute_path(InodeIdentifier);
  73. InodeIdentifier root_inode_id() const;
  74. Inode* root_inode() { return m_root_inode.ptr(); }
  75. const Inode* root_inode() const { return m_root_inode.ptr(); }
  76. void sync();
  77. Device* get_device(unsigned major, unsigned minor);
  78. private:
  79. friend class FileDescriptor;
  80. RetainPtr<Inode> get_inode(InodeIdentifier);
  81. bool is_vfs_root(InodeIdentifier) const;
  82. void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
  83. InodeIdentifier old_resolve_path(StringView path, InodeIdentifier base, int& error, int options = 0, InodeIdentifier* parent_id = nullptr);
  84. KResultOr<InodeIdentifier> resolve_path(StringView path, InodeIdentifier base, int options = 0, InodeIdentifier* parent_id = nullptr);
  85. KResultOr<Retained<Inode>> resolve_path_to_inode(StringView path, Inode& base, RetainPtr<Inode>* parent_id = nullptr, int options = 0);
  86. KResultOr<InodeIdentifier> resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode);
  87. Mount* find_mount_for_host(InodeIdentifier);
  88. Mount* find_mount_for_guest(InodeIdentifier);
  89. RetainPtr<Inode> m_root_inode;
  90. Vector<OwnPtr<Mount>> m_mounts;
  91. HashMap<dword, Device*> m_devices;
  92. };