ladybird/Kernel/Memory/SharedInodeVMObject.cpp
Mike Akers de980de0e4 Kernel: Lock the inode before writing in SharedInodeVMObject::sync
We ensure that when we call SharedInodeVMObject::sync we lock the inode
lock before calling Inode virtual write_bytes method directly to avoid
assertion on the unlocked inode lock, as it was regressed recently. This
is not a complete fix as the need to lock from each path before calling
the write_bytes method should be avoided because it can lead to
hard-to-find bugs, and this commit only fixes the problem temporarily.
2022-08-16 16:54:03 +02:00

63 lines
2.3 KiB
C++

/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/Locking/Spinlock.h>
#include <Kernel/Memory/SharedInodeVMObject.h>
namespace Kernel::Memory {
ErrorOr<NonnullRefPtr<SharedInodeVMObject>> SharedInodeVMObject::try_create_with_inode(Inode& inode)
{
size_t size = inode.size();
if (auto shared_vmobject = inode.shared_vmobject())
return shared_vmobject.release_nonnull();
auto new_physical_pages = TRY(VMObject::try_create_physical_pages(size));
auto dirty_pages = TRY(Bitmap::try_create(new_physical_pages.size(), false));
auto vmobject = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SharedInodeVMObject(inode, move(new_physical_pages), move(dirty_pages))));
TRY(vmobject->inode().set_shared_vmobject(*vmobject));
return vmobject;
}
ErrorOr<NonnullRefPtr<VMObject>> SharedInodeVMObject::try_clone()
{
auto new_physical_pages = TRY(this->try_clone_physical_pages());
auto dirty_pages = TRY(Bitmap::try_create(new_physical_pages.size(), false));
return adopt_nonnull_ref_or_enomem<VMObject>(new (nothrow) SharedInodeVMObject(*this, move(new_physical_pages), move(dirty_pages)));
}
SharedInodeVMObject::SharedInodeVMObject(Inode& inode, FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages, Bitmap dirty_pages)
: InodeVMObject(inode, move(new_physical_pages), move(dirty_pages))
{
}
SharedInodeVMObject::SharedInodeVMObject(SharedInodeVMObject const& other, FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages, Bitmap dirty_pages)
: InodeVMObject(other, move(new_physical_pages), move(dirty_pages))
{
}
ErrorOr<void> SharedInodeVMObject::sync(off_t offset_in_pages, size_t pages)
{
SpinlockLocker locker(m_lock);
size_t highest_page_to_flush = min(page_count(), offset_in_pages + pages);
for (size_t page_index = offset_in_pages; page_index < highest_page_to_flush; ++page_index) {
auto& physical_page = m_physical_pages[page_index];
if (!physical_page)
continue;
u8 page_buffer[PAGE_SIZE];
MM.copy_physical_page(*physical_page, page_buffer);
MutexLocker locker(m_inode->m_inode_lock);
TRY(m_inode->write_bytes(page_index * PAGE_SIZE, PAGE_SIZE, UserOrKernelBuffer::for_kernel_buffer(page_buffer), nullptr));
}
return {};
}
}