mount.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include <Kernel/FileSystem/Custody.h>
  27. #include <Kernel/FileSystem/DevFS.h>
  28. #include <Kernel/FileSystem/DevPtsFS.h>
  29. #include <Kernel/FileSystem/Ext2FileSystem.h>
  30. #include <Kernel/FileSystem/Plan9FileSystem.h>
  31. #include <Kernel/FileSystem/ProcFS.h>
  32. #include <Kernel/FileSystem/TmpFS.h>
  33. #include <Kernel/FileSystem/VirtualFileSystem.h>
  34. #include <Kernel/Process.h>
  35. namespace Kernel {
  36. int Process::sys$mount(Userspace<const Syscall::SC_mount_params*> user_params)
  37. {
  38. if (!is_superuser())
  39. return -EPERM;
  40. REQUIRE_NO_PROMISES;
  41. Syscall::SC_mount_params params;
  42. if (!copy_from_user(&params, user_params))
  43. return -EFAULT;
  44. auto source_fd = params.source_fd;
  45. auto target = copy_string_from_user(params.target);
  46. if (target.is_null())
  47. return -EFAULT;
  48. auto fs_type = copy_string_from_user(params.fs_type);
  49. if (fs_type.is_null())
  50. return -EFAULT;
  51. auto description = file_description(source_fd);
  52. if (!description.is_null())
  53. dbgln("mount {}: source fd {} @ {}", fs_type, source_fd, target);
  54. else
  55. dbgln("mount {} @ {}", fs_type, target);
  56. auto custody_or_error = VFS::the().resolve_path(target, current_directory());
  57. if (custody_or_error.is_error())
  58. return custody_or_error.error();
  59. auto& target_custody = custody_or_error.value();
  60. if (!target_custody->inode().is_directory())
  61. return -ENOTDIR;
  62. if (params.flags & MS_REMOUNT) {
  63. // We're not creating a new mount, we're updating an existing one!
  64. return VFS::the().remount(target_custody, params.flags & ~MS_REMOUNT);
  65. }
  66. if (params.flags & MS_BIND) {
  67. // We're doing a bind mount.
  68. if (description.is_null())
  69. return -EBADF;
  70. if (!description->custody()) {
  71. // We only support bind-mounting inodes, not arbitrary files.
  72. return -ENODEV;
  73. }
  74. return VFS::the().bind_mount(*description->custody(), target_custody, params.flags);
  75. }
  76. RefPtr<FS> fs;
  77. if (fs_type == "ext2" || fs_type == "Ext2FS") {
  78. if (description.is_null())
  79. return -EBADF;
  80. if (!description->file().is_block_device())
  81. return -ENOTBLK;
  82. if (!description->file().is_seekable()) {
  83. dbgln("mount: this is not a seekable file");
  84. return -ENODEV;
  85. }
  86. dbgln("mount: attempting to mount {} on {}", description->absolute_path(), target);
  87. fs = Ext2FS::create(*description);
  88. } else if (fs_type == "9p" || fs_type == "Plan9FS") {
  89. if (description.is_null())
  90. return -EBADF;
  91. fs = Plan9FS::create(*description);
  92. } else if (fs_type == "proc" || fs_type == "ProcFS") {
  93. fs = ProcFS::create();
  94. } else if (fs_type == "devpts" || fs_type == "DevPtsFS") {
  95. fs = DevPtsFS::create();
  96. } else if (fs_type == "dev" || fs_type == "DevFS") {
  97. fs = DevFS::create();
  98. } else if (fs_type == "tmp" || fs_type == "TmpFS") {
  99. fs = TmpFS::create();
  100. } else {
  101. return -ENODEV;
  102. }
  103. if (!fs->initialize()) {
  104. dbgln("mount: failed to initialize {} filesystem, fd={}", fs_type, source_fd);
  105. return -ENODEV;
  106. }
  107. auto result = VFS::the().mount(fs.release_nonnull(), target_custody, params.flags);
  108. if (!description.is_null())
  109. dbgln("mount: successfully mounted {} on {}", description->absolute_path(), target);
  110. else
  111. dbgln("mount: successfully mounted {}", target);
  112. return result;
  113. }
  114. int Process::sys$umount(Userspace<const char*> user_mountpoint, size_t mountpoint_length)
  115. {
  116. if (!is_superuser())
  117. return -EPERM;
  118. REQUIRE_NO_PROMISES;
  119. auto mountpoint = get_syscall_path_argument(user_mountpoint, mountpoint_length);
  120. if (mountpoint.is_error())
  121. return mountpoint.error();
  122. auto custody_or_error = VFS::the().resolve_path(mountpoint.value(), current_directory());
  123. if (custody_or_error.is_error())
  124. return custody_or_error.error();
  125. auto& guest_inode = custody_or_error.value()->inode();
  126. return VFS::the().unmount(guest_inode);
  127. }
  128. }