diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index f51a86a091..84a169a1c9 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -2735,15 +2738,15 @@ int Process::sys$reboot() return ESUCCESS; } -int Process::sys$mount(const char* device_path, const char* mountpoint) +int Process::sys$mount(const char* device_path, const char* mountpoint, const char* fstype) { if (!is_superuser()) return -EPERM; - if (!validate_read_str(device_path) || !validate_read_str(mountpoint)) + if (!validate_read_str(device_path) || !validate_read_str(mountpoint) || !validate_read_str(fstype)) return -EFAULT; - dbg() << "mount: device " << device_path << " @ " << mountpoint; + dbg() << "mount " << fstype << ": device " << device_path << " @ " << mountpoint; auto custody_or_error = VFS::the().resolve_path(mountpoint, current_directory()); if (custody_or_error.is_error()) @@ -2751,37 +2754,47 @@ int Process::sys$mount(const char* device_path, const char* mountpoint) auto& mountpoint_custody = custody_or_error.value(); - auto metadata_or_error = VFS::the().lookup_metadata(device_path, current_directory()); - if (metadata_or_error.is_error()) - return metadata_or_error.error(); + RefPtr fs { nullptr }; - auto major = metadata_or_error.value().major_device; - auto minor = metadata_or_error.value().minor_device; + if (strcmp(fstype, "ext2") == 0 || strcmp(fstype, "Ext2FS") == 0) { + auto metadata_or_error = VFS::the().lookup_metadata(device_path, current_directory()); + if (metadata_or_error.is_error()) + return metadata_or_error.error(); - auto* device = VFS::the().get_device(major, minor); - if (!device) { - dbg() << "mount: device (" << major << "," << minor << ") not found"; + auto major = metadata_or_error.value().major_device; + auto minor = metadata_or_error.value().minor_device; + + auto* device = VFS::the().get_device(major, minor); + if (!device) { + dbg() << "mount: device (" << major << "," << minor << ") not found"; + return -ENODEV; + } + + if (!device->is_disk_device()) { + dbg() << "mount: device (" << major << "," << minor << ") is not a DiskDevice"; + return -ENODEV; + } + + auto& disk_device = static_cast(*device); + + dbg() << "mount: attempting to mount device (" << major << "," << minor << ") on " << mountpoint; + + fs = Ext2FS::create(disk_device); + } else if (strcmp(fstype, "proc") == 0 || strcmp(fstype, "ProcFS") == 0) + fs = ProcFS::create(); + else if (strcmp(fstype, "devpts") == 0 || strcmp(fstype, "DevPtsFS") == 0) + fs = DevPtsFS::create(); + else if (strcmp(fstype, "tmp") == 0 || strcmp(fstype, "TmpFS") == 0) + fs = TmpFS::create(); + else + return -ENODEV; + + if (!fs->initialize()) { + dbg() << "mount: failed to initialize " << fstype << " filesystem on " << device_path; return -ENODEV; } - if (!device->is_disk_device()) { - dbg() << "mount: device (" << major << "," << minor << ") is not a DiskDevice"; - return -ENODEV; - } - - auto& disk_device = static_cast(*device); - - dbg() << "mount: attempting to mount device (" << major << "," << minor << ") on " << mountpoint; - - // We currently only support ext2. Sorry :^) - auto ext2fs = Ext2FS::create(disk_device); - if (!ext2fs->initialize()) { - dbg() << "mount: could not find ext2 filesystem on " << device_path; - return -ENODEV; - } - - // Let's mount the volume now - auto result = VFS::the().mount(ext2fs, mountpoint_custody); + auto result = VFS::the().mount(fs.release_nonnull(), mountpoint_custody); dbg() << "mount: successfully mounted " << device_path << " on " << mountpoint; return result; } diff --git a/Kernel/Process.h b/Kernel/Process.h index dc268d0cca..d09f3d7f1a 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -186,7 +186,7 @@ public: int sys$unlink(const char* pathname); int sys$symlink(const char* target, const char* linkpath); int sys$rmdir(const char* pathname); - int sys$mount(const char* device, const char* mountpoint); + int sys$mount(const char* device, const char* mountpoint, const char* fstype); int sys$umount(const char* mountpoint); int sys$read_tsc(u32* lsw, u32* msw); int sys$chmod(const char* pathname, mode_t); diff --git a/Kernel/Syscall.cpp b/Kernel/Syscall.cpp index 54d8300c5d..eff996fbfb 100644 --- a/Kernel/Syscall.cpp +++ b/Kernel/Syscall.cpp @@ -293,7 +293,7 @@ static u32 handle(RegisterDump& regs, u32 function, u32 arg1, u32 arg2, u32 arg3 break; } case Syscall::SC_mount: - return current->process().sys$mount((const char*)arg1, (const char*)arg2); + return current->process().sys$mount((const char*)arg1, (const char*)arg2, (const char*)arg3); case Syscall::SC_reboot: { return current->process().sys$reboot(); } diff --git a/Libraries/LibC/unistd.cpp b/Libraries/LibC/unistd.cpp index 9b983fe900..8cb27006c8 100644 --- a/Libraries/LibC/unistd.cpp +++ b/Libraries/LibC/unistd.cpp @@ -551,9 +551,9 @@ int reboot() __RETURN_WITH_ERRNO(rc, rc, -1); } -int mount(const char* device, const char* mountpoint) +int mount(const char* device, const char* mountpoint, const char* fstype) { - int rc = syscall(SC_mount, device, mountpoint); + int rc = syscall(SC_mount, device, mountpoint, fstype); __RETURN_WITH_ERRNO(rc, rc, -1); } diff --git a/Libraries/LibC/unistd.h b/Libraries/LibC/unistd.h index 71bd365eba..c522bba6bc 100644 --- a/Libraries/LibC/unistd.h +++ b/Libraries/LibC/unistd.h @@ -100,7 +100,7 @@ int fchown(int fd, uid_t, gid_t); int ftruncate(int fd, off_t length); int halt(); int reboot(); -int mount(const char* device, const char* mountpoint); +int mount(const char* device, const char* mountpoint, const char* fstype); int umount(const char* mountpoint); enum { diff --git a/Userland/mount.cpp b/Userland/mount.cpp index 751063799e..0267215f3c 100644 --- a/Userland/mount.cpp +++ b/Userland/mount.cpp @@ -2,28 +2,27 @@ #include #include -// This version of 'mount' must have the following arguments -// 'mount -// It can currently only mount _physical_ devices found in /dev -// -// Currently, it is only possible to mount ext2 devices. Sorry! :^) int main(int argc, char** argv) { CArgsParser args_parser("mount"); args_parser.add_arg("devname", "device path"); args_parser.add_arg("mountpoint", "mount point"); + args_parser.add_arg("fstype", "file system type"); CArgsParserResult args = args_parser.parse(argc, argv); - if (argc == 3) { - // Let's use lstat so we can convert devname into a major/minor device pair! - if (mount(argv[1], argv[2]) < 0) { - perror("mount"); - return 1; - } - } else { + if (argc < 3 || argc > 4) { args_parser.print_usage(); return 0; } + const char* devname = argv[1]; + const char* mountpoint = argv[2]; + const char* fstype = argc == 4 ? argv[3] : "ext2"; + + if (mount(devname, mountpoint, fstype) < 0) { + perror("mount"); + return 1; + } + return 0; }