2023-02-03 22:18:52 +01:00
|
|
|
#include "fs/tmpfs/FileSystem.h"
|
2023-08-08 13:33:40 +02:00
|
|
|
#include "arch/Timer.h"
|
2023-03-18 09:10:33 +01:00
|
|
|
#include "fs/devices/DeviceRegistry.h"
|
2023-06-18 11:33:40 +02:00
|
|
|
#include "fs/tmpfs/Inode.h"
|
2023-02-03 22:18:52 +01:00
|
|
|
#include <luna/Alloc.h>
|
2023-02-27 15:04:29 +01:00
|
|
|
#include <luna/CString.h>
|
2023-02-25 19:06:50 +01:00
|
|
|
#include <luna/Ignore.h>
|
2023-02-03 22:18:52 +01:00
|
|
|
|
|
|
|
namespace TmpFS
|
|
|
|
{
|
|
|
|
Result<SharedPtr<VFS::FileSystem>> FileSystem::create()
|
|
|
|
{
|
2023-03-07 22:16:52 +01:00
|
|
|
SharedPtr<FileSystem> fs = TRY(adopt_shared_if_nonnull(new (std::nothrow) FileSystem()));
|
2023-08-01 17:20:28 +02:00
|
|
|
SharedPtr<VFS::Inode> root = TRY(fs->create_dir_inode({}, 0755));
|
2023-02-25 18:03:11 +01:00
|
|
|
fs->set_root(root);
|
2023-02-03 22:18:52 +01:00
|
|
|
return (SharedPtr<VFS::FileSystem>)fs;
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:36:36 +02:00
|
|
|
Result<u64> FileSystem::allocate_inode_number()
|
|
|
|
{
|
|
|
|
return m_next_inode_number++;
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:20:28 +02:00
|
|
|
Result<SharedPtr<VFS::Inode>> FileSystem::create_file_inode(mode_t mode)
|
2023-02-03 22:18:52 +01:00
|
|
|
{
|
2023-02-25 18:11:39 +01:00
|
|
|
SharedPtr<FileInode> inode = TRY(make_shared<FileInode>());
|
2023-02-25 18:03:11 +01:00
|
|
|
inode->set_fs(*this, {});
|
2023-03-18 09:10:33 +01:00
|
|
|
inode->set_inode_number(m_next_inode_number++, {});
|
2023-08-01 17:20:28 +02:00
|
|
|
inode->m_metadata.mode = mode;
|
2023-08-08 13:33:40 +02:00
|
|
|
inode->m_metadata.atime = inode->m_metadata.ctime = inode->m_metadata.mtime = *Timer::realtime_clock();
|
2023-02-25 18:03:11 +01:00
|
|
|
return (SharedPtr<VFS::Inode>)inode;
|
2023-02-03 22:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-05-20 21:46:31 +02:00
|
|
|
Result<SharedPtr<VFS::Inode>> FileSystem::create_symlink_inode(StringView link)
|
|
|
|
{
|
|
|
|
SharedPtr<SymlinkInode> inode = TRY(make_shared<SymlinkInode>());
|
|
|
|
inode->set_fs(*this, {});
|
|
|
|
TRY(inode->set_link(link, {}));
|
|
|
|
inode->set_inode_number(m_next_inode_number++, {});
|
2023-08-08 13:33:40 +02:00
|
|
|
inode->m_metadata.atime = inode->m_metadata.ctime = inode->m_metadata.mtime = *Timer::realtime_clock();
|
2023-05-20 21:46:31 +02:00
|
|
|
return (SharedPtr<VFS::Inode>)inode;
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:20:28 +02:00
|
|
|
Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode(SharedPtr<VFS::Inode> parent, mode_t mode)
|
2023-02-25 19:08:55 +01:00
|
|
|
{
|
|
|
|
SharedPtr<DirInode> inode = TRY(make_shared<DirInode>());
|
2023-03-10 21:09:08 +01:00
|
|
|
|
2023-02-25 19:22:05 +01:00
|
|
|
TRY(inode->add_entry(inode, "."));
|
2023-02-27 15:04:29 +01:00
|
|
|
TRY(inode->add_entry(parent ? parent : (SharedPtr<VFS::Inode>)inode, ".."));
|
2023-02-25 19:22:05 +01:00
|
|
|
|
2023-03-10 21:09:08 +01:00
|
|
|
inode->set_self(inode, {});
|
2023-02-25 19:08:55 +01:00
|
|
|
inode->set_fs(*this, {});
|
2023-03-18 09:10:33 +01:00
|
|
|
inode->set_inode_number(m_next_inode_number++, {});
|
2023-08-01 17:20:28 +02:00
|
|
|
inode->m_metadata.mode = mode;
|
2023-08-08 13:33:40 +02:00
|
|
|
inode->m_metadata.atime = inode->m_metadata.ctime = inode->m_metadata.mtime = *Timer::realtime_clock();
|
2023-03-10 21:09:08 +01:00
|
|
|
|
2023-03-18 09:10:33 +01:00
|
|
|
return (SharedPtr<VFS::Inode>)inode;
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:20:28 +02:00
|
|
|
Result<SharedPtr<VFS::Inode>> FileSystem::create_device_inode(u32 major, u32 minor, mode_t mode)
|
2023-03-18 09:10:33 +01:00
|
|
|
{
|
2023-05-09 18:31:27 +02:00
|
|
|
SharedPtr<Device> device = TRY(DeviceRegistry::fetch_special_device(major, minor));
|
2023-03-18 09:10:33 +01:00
|
|
|
|
|
|
|
SharedPtr<DeviceInode> inode = TRY(make_shared<DeviceInode>());
|
|
|
|
inode->set_fs(*this, {});
|
|
|
|
inode->set_inode_number(m_next_inode_number++, {});
|
|
|
|
inode->set_device(device, {});
|
2023-05-23 20:45:24 +02:00
|
|
|
// FIXME: This should be queried from Device directly, but Device doesn't have an API to store and retrieve its
|
|
|
|
// device ID atm.
|
|
|
|
inode->set_device_id(luna_dev_makedev(major, minor), {});
|
2023-08-01 17:20:28 +02:00
|
|
|
inode->m_metadata.mode = mode;
|
2023-08-08 13:33:40 +02:00
|
|
|
inode->m_metadata.atime = inode->m_metadata.ctime = inode->m_metadata.mtime = *Timer::realtime_clock();
|
2023-08-08 15:17:08 +02:00
|
|
|
inode->m_metadata.size = device->size();
|
2023-03-10 21:09:08 +01:00
|
|
|
|
2023-02-25 19:08:55 +01:00
|
|
|
return (SharedPtr<VFS::Inode>)inode;
|
|
|
|
}
|
|
|
|
|
2023-05-23 20:45:24 +02:00
|
|
|
FileSystem::FileSystem()
|
|
|
|
{
|
|
|
|
m_host_device_id = DeviceRegistry::next_null_device_id();
|
|
|
|
}
|
|
|
|
|
2023-05-17 19:40:37 +02:00
|
|
|
Result<void> FileSystem::set_mount_dir(SharedPtr<VFS::Inode> parent)
|
|
|
|
{
|
|
|
|
return m_root_inode->replace_entry(parent, "..");
|
|
|
|
}
|
|
|
|
|
2023-06-22 19:57:12 +02:00
|
|
|
Result<void> FileSystem::reset_mount_dir()
|
|
|
|
{
|
|
|
|
return m_root_inode->replace_entry(m_root_inode, "..");
|
|
|
|
}
|
|
|
|
|
2023-02-25 18:03:11 +01:00
|
|
|
void FileSystem::set_root(SharedPtr<VFS::Inode> root)
|
2023-02-03 22:18:52 +01:00
|
|
|
{
|
|
|
|
m_root_inode = root;
|
|
|
|
}
|
|
|
|
}
|