1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-10-29 11:02:36 +00:00
serenity/Kernel/FileSystem/SysFS/Subsystems/Kernel/DiskUsage.cpp
Liav A 5dcf03ad9a 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.
2024-03-13 15:33:47 -06:00

64 lines
2.5 KiB
C++

/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#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>
#include <Kernel/Sections.h>
namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<SysFSDiskUsage> SysFSDiskUsage::must_create(SysFSDirectory const& parent_directory)
{
return adopt_ref_if_nonnull(new (nothrow) SysFSDiskUsage(parent_directory)).release_nonnull();
}
UNMAP_AFTER_INIT SysFSDiskUsage::SysFSDiskUsage(SysFSDirectory const& parent_directory)
: SysFSGlobalInformation(parent_directory)
{
}
ErrorOr<void> SysFSDiskUsage::try_generate(KBufferBuilder& builder)
{
auto array = TRY(JsonArraySerializer<>::try_create(builder));
TRY(VirtualFileSystem::the().for_each_mount([&array](auto& mount) -> ErrorOr<void> {
auto& fs = mount.guest_fs();
auto fs_object = TRY(array.add_object());
TRY(fs_object.add("class_name"sv, fs.class_name()));
TRY(fs_object.add("total_block_count"sv, fs.total_block_count()));
TRY(fs_object.add("free_block_count"sv, fs.free_block_count()));
TRY(fs_object.add("total_inode_count"sv, fs.total_inode_count()));
TRY(fs_object.add("free_inode_count"sv, fs.free_inode_count()));
auto mount_point = TRY(mount.absolute_path());
TRY(fs_object.add("mount_point"sv, mount_point->view()));
TRY(fs_object.add("block_size"sv, static_cast<u64>(fs.logical_block_size())));
TRY(fs_object.add("readonly"sv, fs.is_readonly()));
TRY(fs_object.add("mount_flags"sv, mount.flags()));
if (fs.is_file_backed()) {
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"));
}
TRY(fs_object.finish());
return {};
}));
TRY(array.finish());
return {};
}
}