Kernel+LibC+Userland: Support mounting other kinds of filesystems

This commit is contained in:
Sergey Bugaev 2019-08-15 19:13:56 +03:00 committed by Andreas Kling
parent 66a0a12435
commit 425c356288
Notes: sideshowbarker 2024-07-19 12:39:25 +09:00
6 changed files with 58 additions and 46 deletions

View file

@ -9,6 +9,9 @@
#include <Kernel/Devices/NullDevice.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/Ext2FileSystem.h>
#include <Kernel/FileSystem/ProcFS.h>
#include <Kernel/FileSystem/DevPtsFS.h>
#include <Kernel/FileSystem/TmpFS.h>
#include <Kernel/FileSystem/FIFO.h>
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/FileSystem/InodeWatcher.h>
@ -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> 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<DiskDevice&>(*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<DiskDevice&>(*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;
}

View file

@ -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);

View file

@ -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();
}

View file

@ -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);
}

View file

@ -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 {

View file

@ -2,28 +2,27 @@
#include <stdio.h>
#include <unistd.h>
// This version of 'mount' must have the following arguments
// 'mount <device_path> <mount_point>
// 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;
}