VirtualFileSystem.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Badge.h>
  28. #include <AK/Function.h>
  29. #include <AK/HashMap.h>
  30. #include <AK/NonnullOwnPtrVector.h>
  31. #include <AK/OwnPtr.h>
  32. #include <AK/RefPtr.h>
  33. #include <AK/String.h>
  34. #include <Kernel/FileSystem/FileSystem.h>
  35. #include <Kernel/FileSystem/InodeIdentifier.h>
  36. #include <Kernel/FileSystem/InodeMetadata.h>
  37. #include <Kernel/KResult.h>
  38. #include <Kernel/UnveilNode.h>
  39. namespace Kernel {
  40. class Custody;
  41. class Device;
  42. class FileDescription;
  43. struct UidAndGid {
  44. uid_t uid;
  45. gid_t gid;
  46. };
  47. class VFS {
  48. AK_MAKE_ETERNAL
  49. public:
  50. class Mount {
  51. public:
  52. Mount(FS&, Custody* host_custody, int flags);
  53. Mount(Inode& source, Custody& host_custody, int flags);
  54. const Inode* host() const;
  55. Inode* host();
  56. const Inode& guest() const { return *m_guest; }
  57. Inode& guest() { return *m_guest; }
  58. const FS& guest_fs() const { return *m_guest_fs; }
  59. String absolute_path() const;
  60. int flags() const { return m_flags; }
  61. void set_flags(int flags) { m_flags = flags; }
  62. private:
  63. NonnullRefPtr<Inode> m_guest;
  64. NonnullRefPtr<FS> m_guest_fs;
  65. RefPtr<Custody> m_host_custody;
  66. int m_flags;
  67. };
  68. static void initialize();
  69. static VFS& the();
  70. VFS();
  71. ~VFS();
  72. bool mount_root(FS&);
  73. KResult mount(FS&, Custody& mount_point, int flags);
  74. KResult bind_mount(Custody& source, Custody& mount_point, int flags);
  75. KResult remount(Custody& mount_point, int new_flags);
  76. KResult unmount(Inode& guest_inode);
  77. KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  78. KResultOr<NonnullRefPtr<FileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  79. KResult mkdir(StringView path, mode_t mode, Custody& base);
  80. KResult link(StringView old_path, StringView new_path, Custody& base);
  81. KResult unlink(StringView path, Custody& base);
  82. KResult symlink(StringView target, StringView linkpath, Custody& base);
  83. KResult rmdir(StringView path, Custody& base);
  84. KResult chmod(StringView path, mode_t, Custody& base);
  85. KResult chmod(Custody&, mode_t);
  86. KResult chown(StringView path, uid_t, gid_t, Custody& base);
  87. KResult chown(Custody&, uid_t, gid_t);
  88. KResult access(StringView path, int mode, Custody& base);
  89. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  90. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  91. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  92. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  93. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  94. size_t mount_count() const { return m_mounts.size(); }
  95. void for_each_mount(Function<void(const Mount&)>) const;
  96. InodeIdentifier root_inode_id() const;
  97. void sync();
  98. Custody& root_custody();
  99. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  100. KResultOr<NonnullRefPtr<Custody>> resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  101. private:
  102. friend class FileDescription;
  103. const UnveilNode* find_matching_unveiled_path(StringView path);
  104. KResult validate_path_against_process_veil(StringView path, int options);
  105. bool is_vfs_root(InodeIdentifier) const;
  106. KResult traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntryView&)>);
  107. Mount* find_mount_for_host(Inode&);
  108. Mount* find_mount_for_host(InodeIdentifier);
  109. Mount* find_mount_for_guest(Inode&);
  110. Mount* find_mount_for_guest(InodeIdentifier);
  111. Lock m_lock { "VFSLock" };
  112. RefPtr<Inode> m_root_inode;
  113. Vector<Mount, 16> m_mounts;
  114. RefPtr<Custody> m_root_custody;
  115. };
  116. }