Files
serenity/Kernel/FileSystem/DevLoopFS/FileSystem.cpp
Liav A. 7f5a2c1466 Kernel: Register block and character devices in separate HashMaps
Instead of putting everything in one hash map, let's distinguish between
the devices based on their type.

This change makes the devices semantically separated, and is considered
a preparation before we could expose a comprehensive list of allocations
per major numbers and their purpose.
2024-07-06 21:42:32 +02:00

78 lines
2.4 KiB
C++

/*
* Copyright (c) 2024, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/API/DeviceFileTypes.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/Loop/LoopDevice.h>
#include <Kernel/FileSystem/DevLoopFS/FileSystem.h>
#include <Kernel/FileSystem/DevLoopFS/Inode.h>
#include <Kernel/FileSystem/RAMBackedFileType.h>
#include <Kernel/Time/TimeManagement.h>
namespace Kernel {
ErrorOr<NonnullRefPtr<FileSystem>> DevLoopFS::try_create(ReadonlyBytes)
{
return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevLoopFS));
}
DevLoopFS::DevLoopFS() = default;
DevLoopFS::~DevLoopFS() = default;
u8 DevLoopFS::internal_file_type_to_directory_entry_type(DirectoryEntryView const& entry) const
{
return ram_backed_file_type_to_directory_entry_type(entry);
}
ErrorOr<void> DevLoopFS::initialize()
{
m_root_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevLoopFSInode(*this)));
m_root_inode->m_metadata.inode = { fsid(), 1 };
m_root_inode->m_metadata.mode = S_IFDIR
| S_IROTH | S_IRGRP | S_IRUSR
| S_IXUSR | S_IXGRP | S_IXOTH;
m_root_inode->m_metadata.uid = 0;
m_root_inode->m_metadata.gid = 0;
m_root_inode->m_metadata.size = 0;
m_root_inode->m_metadata.mtime = TimeManagement::boot_time();
return {};
}
static unsigned inode_index_to_loop_index(InodeIndex inode_index)
{
VERIFY(inode_index > 1);
return inode_index.value() - 2;
}
Inode& DevLoopFS::root_inode()
{
return *m_root_inode;
}
ErrorOr<NonnullRefPtr<Inode>> DevLoopFS::get_inode(InodeIdentifier inode_id) const
{
if (inode_id.index() == 1)
return *m_root_inode;
unsigned loop_index = inode_index_to_loop_index(inode_id.index());
auto device = DeviceManagement::the().get_device(DeviceNodeType::Block, 20, loop_index);
VERIFY(device);
auto& loop_device = static_cast<LoopDevice&>(*device);
auto inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevLoopFSInode(const_cast<DevLoopFS&>(*this), inode_id.index(), loop_device)));
inode->m_metadata.inode = inode_id;
inode->m_metadata.size = 0;
inode->m_metadata.uid = 0;
inode->m_metadata.gid = 0;
inode->m_metadata.mode = S_IFBLK | S_IRUSR | S_IWUSR;
inode->m_metadata.major_device = device->major();
inode->m_metadata.minor_device = device->minor();
inode->m_metadata.mtime = TimeManagement::boot_time();
return inode;
}
}