Files
serenity/Kernel/FileSystem/SysFS/Inode.cpp
implicitfield 0e94887b2c Kernel/VFS: Make filesystem implementations responsible for renames
Previously, the VFS layer would try to handle renames more-or-less by
itself, which really only worked for ext2, and even that was only due to
the replace_child kludge existing specifically for this purpose. This
never worked properly for FATFS, since the VFS layer effectively
depended on filesystems having some kind of reference-counting for
inodes, which is something that simply doesn't exist on any FAT variant
we support.

To resolve various issues with the existing scheme, this commit makes
filesystem implementations themselves responsible for the actual rename
operation, while keeping all the existing validation inside the VFS
layer. The only intended behavior change here is that rename operations
should actually properly work on FATFS.
2024-12-17 19:02:15 -05:00

115 lines
3.0 KiB
C++

/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringView.h>
#include <Kernel/FileSystem/SysFS/Inode.h>
#include <Kernel/Time/TimeManagement.h>
namespace Kernel {
ErrorOr<NonnullRefPtr<SysFSInode>> SysFSInode::try_create(SysFS const& fs, SysFSComponent const& component)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSInode(fs, component));
}
SysFSInode::SysFSInode(SysFS const& fs, SysFSComponent const& component)
: Inode(const_cast<SysFS&>(fs), component.component_index())
, m_associated_component(component)
{
}
void SysFSInode::did_seek(OpenFileDescription& description, off_t new_offset)
{
if (new_offset != 0)
return;
auto result = m_associated_component->refresh_data(description);
if (result.is_error()) {
// Subsequent calls to read will return EIO!
dbgln("SysFS: Could not refresh contents: {}", result.error());
}
}
ErrorOr<void> SysFSInode::attach(OpenFileDescription& description)
{
return m_associated_component->refresh_data(description);
}
ErrorOr<size_t> SysFSInode::read_bytes_locked(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* fd) const
{
return m_associated_component->read_bytes(offset, count, buffer, fd);
}
ErrorOr<void> SysFSInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const
{
VERIFY_NOT_REACHED();
}
ErrorOr<NonnullRefPtr<Inode>> SysFSInode::lookup(StringView)
{
VERIFY_NOT_REACHED();
}
InodeMetadata SysFSInode::metadata() const
{
// NOTE: No locking required as m_associated_component or its component index will never change during our lifetime.
InodeMetadata metadata;
metadata.inode = { fsid(), m_associated_component->component_index() };
metadata.mode = S_IFREG | m_associated_component->permissions();
metadata.uid = 0;
metadata.gid = 0;
metadata.size = m_associated_component->size();
metadata.mtime = TimeManagement::boot_time();
return metadata;
}
ErrorOr<void> SysFSInode::flush_metadata()
{
return {};
}
ErrorOr<size_t> SysFSInode::write_bytes_locked(off_t offset, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription* fd)
{
return m_associated_component->write_bytes(offset, count, buffer, fd);
}
ErrorOr<NonnullRefPtr<Inode>> SysFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID)
{
return EROFS;
}
ErrorOr<void> SysFSInode::add_child(Inode&, StringView, mode_t)
{
return EROFS;
}
ErrorOr<void> SysFSInode::remove_child(StringView)
{
return EROFS;
}
ErrorOr<void> SysFSInode::chmod(mode_t)
{
return EPERM;
}
ErrorOr<void> SysFSInode::chown(UserID, GroupID)
{
return EPERM;
}
ErrorOr<void> SysFSInode::truncate_locked(u64 size)
{
VERIFY(m_inode_lock.is_locked());
return m_associated_component->truncate(size);
}
ErrorOr<void> SysFSInode::update_timestamps(Optional<UnixDateTime>, Optional<UnixDateTime>, Optional<UnixDateTime>)
{
return {};
}
}