1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:17:44 +00:00

Kernel/Devices: Introduce the LoopDevice device

This device is a block device that allows a user to effectively treat an
Inode as a block device.

The static construction method is given an OpenFileDescription reference
but validates that:
- The description has a valid custody (so it's not some arbitrary file).
  Failing this requirement will yield EINVAL.
- The description custody points to an Inode which is a regular file, as
  we only support (seekable) regular files. Failing this requirement
  will yield ENOTSUP.

LoopDevice can be used to mount a regular file on the filesystem like
other supported types of (physical) block devices.
This commit is contained in:
Liav A 2024-02-23 17:11:48 +02:00 committed by Andrew Kaster
parent a9d240c647
commit 5dcf03ad9a
12 changed files with 254 additions and 4 deletions

View file

@ -47,6 +47,7 @@ public:
virtual unsigned free_inode_count() const override;
virtual bool supports_watchers() const override { return true; }
virtual bool supports_backing_loop_devices() const override { return true; }
virtual u8 internal_file_type_to_directory_entry_type(DirectoryEntryView const& entry) const override;

View file

@ -115,6 +115,7 @@ public:
virtual bool is_socket() const { return false; }
virtual bool is_inode_watcher() const { return false; }
virtual bool is_mount_file() const { return false; }
virtual bool is_loop_device() const { return false; }
virtual bool is_regular_file() const { return false; }

View file

@ -34,6 +34,11 @@ public:
virtual Inode& root_inode() = 0;
virtual bool supports_watchers() const { return false; }
// FIXME: We should aim to provide more concise mechanism to ensure
// that backing Inodes from the FileSystem are kept intact so we can
// attach them to a loop device.
virtual bool supports_backing_loop_devices() const { return false; }
bool is_readonly() const { return m_readonly; }
virtual unsigned total_block_count() const { return 0; }

View file

@ -24,6 +24,7 @@ public:
virtual StringView class_name() const override { return "RAMFS"sv; }
virtual bool supports_watchers() const override { return true; }
virtual bool supports_backing_loop_devices() const override { return true; }
virtual Inode& root_inode() override;

View file

@ -5,6 +5,7 @@
*/
#include <AK/JsonObjectSerializer.h>
#include <Kernel/Devices/Loop/LoopDevice.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/DiskUsage.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
@ -40,8 +41,15 @@ ErrorOr<void> SysFSDiskUsage::try_generate(KBufferBuilder& builder)
TRY(fs_object.add("mount_flags"sv, mount.flags()));
if (fs.is_file_backed()) {
auto pseudo_path = TRY(static_cast<const FileBackedFileSystem&>(fs).file_description().pseudo_path());
TRY(fs_object.add("source"sv, pseudo_path->view()));
auto& file = static_cast<const FileBackedFileSystem&>(fs).file();
if (file.is_loop_device()) {
auto& device = static_cast<LoopDevice const&>(file);
auto path = TRY(device.custody().try_serialize_absolute_path());
TRY(fs_object.add("source"sv, path->view()));
} else {
auto pseudo_path = TRY(static_cast<const FileBackedFileSystem&>(fs).file_description().pseudo_path());
TRY(fs_object.add("source"sv, pseudo_path->view()));
}
} else {
TRY(fs_object.add("source"sv, "none"));
}

View file

@ -14,6 +14,7 @@
#include <Kernel/Debug.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/Loop/LoopDevice.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h>
#include <Kernel/FileSystem/FileSystem.h>
@ -213,6 +214,12 @@ ErrorOr<void> VirtualFileSystem::mount(MountFile& mount_file, OpenFileDescriptio
}));
TRY(fs->initialize());
}
if (source_description->file().is_loop_device()) {
auto& device = static_cast<LoopDevice&>(source_description->file());
auto path = TRY(device.custody().try_serialize_absolute_path());
dbgln("VirtualFileSystem: mounting from loop device {}, originated from {}", device.index(), path->view());
}
TRY(add_file_system_to_mount_table(*fs, mount_point, flags));
list.append(static_cast<FileBackedFileSystem&>(*fs));
return {};