mount.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/FileSystem/Custody.h>
  7. #include <Kernel/FileSystem/DevFS.h>
  8. #include <Kernel/FileSystem/DevPtsFS.h>
  9. #include <Kernel/FileSystem/Ext2FileSystem.h>
  10. #include <Kernel/FileSystem/ISO9660FileSystem.h>
  11. #include <Kernel/FileSystem/Plan9FileSystem.h>
  12. #include <Kernel/FileSystem/ProcFS.h>
  13. #include <Kernel/FileSystem/SysFS.h>
  14. #include <Kernel/FileSystem/TmpFS.h>
  15. #include <Kernel/FileSystem/VirtualFileSystem.h>
  16. #include <Kernel/Process.h>
  17. namespace Kernel {
  18. KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*> user_params)
  19. {
  20. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  21. if (!is_superuser())
  22. return EPERM;
  23. REQUIRE_NO_PROMISES;
  24. Syscall::SC_mount_params params;
  25. if (!copy_from_user(&params, user_params))
  26. return EFAULT;
  27. auto source_fd = params.source_fd;
  28. auto target_or_error = try_copy_kstring_from_user(params.target);
  29. if (target_or_error.is_error())
  30. return target_or_error.error();
  31. auto fs_type_or_error = try_copy_kstring_from_user(params.fs_type);
  32. if (fs_type_or_error.is_error())
  33. return fs_type_or_error.error();
  34. auto target = target_or_error.value()->view();
  35. auto fs_type = fs_type_or_error.value()->view();
  36. auto description = fds().file_description(source_fd);
  37. if (!description.is_null())
  38. dbgln("mount {}: source fd {} @ {}", fs_type, source_fd, target);
  39. else
  40. dbgln("mount {} @ {}", fs_type, target);
  41. auto custody_or_error = VirtualFileSystem::the().resolve_path(target, current_directory());
  42. if (custody_or_error.is_error())
  43. return custody_or_error.error();
  44. auto& target_custody = custody_or_error.value();
  45. if (params.flags & MS_REMOUNT) {
  46. // We're not creating a new mount, we're updating an existing one!
  47. return VirtualFileSystem::the().remount(target_custody, params.flags & ~MS_REMOUNT);
  48. }
  49. if (params.flags & MS_BIND) {
  50. // We're doing a bind mount.
  51. if (description.is_null())
  52. return EBADF;
  53. if (!description->custody()) {
  54. // We only support bind-mounting inodes, not arbitrary files.
  55. return ENODEV;
  56. }
  57. return VirtualFileSystem::the().bind_mount(*description->custody(), target_custody, params.flags);
  58. }
  59. RefPtr<FileSystem> fs;
  60. if (fs_type == "ext2"sv || fs_type == "Ext2FS"sv) {
  61. if (description.is_null())
  62. return EBADF;
  63. if (!description->file().is_block_device())
  64. return ENOTBLK;
  65. if (!description->file().is_seekable()) {
  66. dbgln("mount: this is not a seekable file");
  67. return ENODEV;
  68. }
  69. dbgln("mount: attempting to mount {} on {}", description->absolute_path(), target);
  70. fs = Ext2FS::create(*description);
  71. } else if (fs_type == "9p"sv || fs_type == "Plan9FS"sv) {
  72. if (description.is_null())
  73. return EBADF;
  74. fs = Plan9FS::create(*description);
  75. } else if (fs_type == "proc"sv || fs_type == "ProcFS"sv) {
  76. auto maybe_fs = ProcFS::try_create();
  77. if (maybe_fs.is_error())
  78. return maybe_fs.error();
  79. fs = maybe_fs.release_value();
  80. } else if (fs_type == "devpts"sv || fs_type == "DevPtsFS"sv) {
  81. fs = DevPtsFS::create();
  82. } else if (fs_type == "dev"sv || fs_type == "DevFS"sv) {
  83. fs = DevFS::create();
  84. } else if (fs_type == "sys"sv || fs_type == "SysFS"sv) {
  85. fs = SysFS::create();
  86. } else if (fs_type == "tmp"sv || fs_type == "TmpFS"sv) {
  87. fs = TmpFS::create();
  88. } else if (fs_type == "iso9660"sv || fs_type == "ISO9660FS"sv) {
  89. if (description.is_null())
  90. return EBADF;
  91. if (!description->file().is_seekable()) {
  92. dbgln("mount: this is not a seekable file");
  93. return ENODEV;
  94. }
  95. dbgln("mount: attempting to mount {} on {}", description->absolute_path(), target);
  96. auto maybe_fs = ISO9660FS::try_create(*description);
  97. if (maybe_fs.is_error()) {
  98. return maybe_fs.error();
  99. }
  100. fs = maybe_fs.release_value();
  101. } else {
  102. return ENODEV;
  103. }
  104. if (!fs)
  105. return ENOMEM;
  106. if (auto result = fs->initialize(); result.is_error()) {
  107. dbgln("mount: failed to initialize {} filesystem, fd={}", fs_type, source_fd);
  108. return result;
  109. }
  110. auto result = VirtualFileSystem::the().mount(fs.release_nonnull(), target_custody, params.flags);
  111. if (!description.is_null())
  112. dbgln("mount: successfully mounted {} on {}", description->absolute_path(), target);
  113. else
  114. dbgln("mount: successfully mounted {}", target);
  115. return result;
  116. }
  117. KResultOr<FlatPtr> Process::sys$umount(Userspace<const char*> user_mountpoint, size_t mountpoint_length)
  118. {
  119. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  120. if (!is_superuser())
  121. return EPERM;
  122. REQUIRE_NO_PROMISES;
  123. auto mountpoint = get_syscall_path_argument(user_mountpoint, mountpoint_length);
  124. if (mountpoint.is_error())
  125. return mountpoint.error();
  126. auto custody_or_error = VirtualFileSystem::the().resolve_path(mountpoint.value()->view(), current_directory());
  127. if (custody_or_error.is_error())
  128. return custody_or_error.error();
  129. auto& guest_inode = custody_or_error.value()->inode();
  130. return VirtualFileSystem::the().unmount(guest_inode);
  131. }
  132. }