diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index 0070eff053c..97ce003aebb 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -36,21 +36,25 @@ InodeIdentifier VFS::root_inode_id() const return m_root_inode->identifier(); } -bool VFS::mount(NonnullRefPtr&& file_system, StringView path) +KResult VFS::mount(NonnullRefPtr&& file_system, Custody& mount_point) +{ + auto& inode = mount_point.inode(); + dbg() << "VFS: Mounting " << file_system->class_name() << " at " << mount_point.absolute_path() << " (inode: " << inode.identifier() << ")"; + // FIXME: check that this is not already a mount point + auto mount = make(mount_point, move(file_system)); + m_mounts.append(move(mount)); + mount_point.did_mount_on({}); + return KSuccess; +} + +KResult VFS::mount(NonnullRefPtr&& file_system, StringView path) { auto result = resolve_path(path, root_custody()); if (result.is_error()) { dbg() << "VFS: mount can't resolve mount point '" << path << "'"; - return false; + return result.error(); } - auto& inode = result.value()->inode(); - dbg() << "VFS: Mounting " << file_system->class_name() << " at " << path << " (inode: " << inode.identifier() << ")"; - // FIXME: check that this is not already a mount point - auto mount = make(*result.value(), move(file_system)); - m_mounts.append(move(mount)); - - result.value()->did_mount_on({}); - return true; + return mount(move(file_system), result.value()); } bool VFS::mount_root(NonnullRefPtr&& file_system) diff --git a/Kernel/FileSystem/VirtualFileSystem.h b/Kernel/FileSystem/VirtualFileSystem.h index fa2c3e5550e..57dcda1b447 100644 --- a/Kernel/FileSystem/VirtualFileSystem.h +++ b/Kernel/FileSystem/VirtualFileSystem.h @@ -57,7 +57,8 @@ public: ~VFS(); bool mount_root(NonnullRefPtr&&); - bool mount(NonnullRefPtr&&, StringView path); + KResult mount(NonnullRefPtr&&, StringView path); + KResult mount(NonnullRefPtr&&, Custody& mount_point); KResultOr> open(RefPtr&&, int options); KResultOr> open(StringView path, int options, mode_t mode, Custody& base); diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index bbbe442013d..d0570cebe9b 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -2729,16 +2729,26 @@ int Process::sys$reboot() int Process::sys$mount(const char* device, const char* mountpoint) { - dbgprintf("mount: device = %s mountpoint = %s\n", device, mountpoint); + if (!is_superuser()) + return -EPERM; + if (!validate_read_str(device) || !validate_read_str(mountpoint)) return -EFAULT; + dbg() << "mount: device " << device << " @ " << mountpoint; + + auto custody_or_error = VFS::the().resolve_path(mountpoint, current_directory()); + if (custody_or_error.is_error()) + return custody_or_error.error(); + + auto& mountpoint_custody = custody_or_error.value(); + // Let's do a stat to get some information about the device.. // Let's use lstat so we can convert devname into a major/minor device pair! struct stat st; int rc = sys$stat(device, &st); if (rc < 0) { - dbgprintf("mount: call to sys$stat failed!\n"); + dbg() << "mount: call to sys$stat failed!"; return -ENODEV; } @@ -2748,7 +2758,7 @@ int Process::sys$mount(const char* device, const char* mountpoint) auto* dev = VFS::the().get_device(major, minor); auto* disk_device = static_cast(dev); - dbgprintf("mount: attempting to mount %d,%d to %s...\n", major, minor, mountpoint); + dbg() << "mount: attempting to mount device (" << major << "," << minor << ") on " << mountpoint; // Do a quick check to make sure we're not passing nullptr to Ext2FS::create! if (dev == nullptr) @@ -2757,15 +2767,14 @@ int Process::sys$mount(const char* device, const char* mountpoint) // We currently only support ext2. Sorry :^) auto ext2fs = Ext2FS::create(*disk_device); if (!ext2fs->initialize()) { - dbgprintf("mount: failed to create ext2fs!\n"); + dbg() << "mount: could not find ext2 filesystem on " << device; return -ENODEV; // Hmmm, this doesn't seem quite right.... } // Let's mount the volume now - VFS::the().mount(ext2fs, mountpoint); - dbgprintf("mount: successfully mounted ext2 volume on %s to %s!\n", device, mountpoint); - - return ESUCCESS; + auto result = VFS::the().mount(ext2fs, mountpoint_custody); + dbg() << "mount: successfully mounted " << device << " on " << mountpoint; + return result; } ProcessTracer& Process::ensure_tracer()