2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2020-02-28 22:20:35 +03:00
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-08-07 19:06:17 +03:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2021-11-17 21:33:00 +03:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/SharedInodeVMObject.h>
|
2019-08-07 19:06:17 +03:00
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullRefPtr<SharedInodeVMObject>> SharedInodeVMObject::try_create_with_inode(Inode& inode)
|
2019-08-07 19:06:17 +03:00
|
|
|
{
|
2020-01-03 17:40:03 +03:00
|
|
|
size_t size = inode.size();
|
2021-01-23 19:11:45 +03:00
|
|
|
if (auto shared_vmobject = inode.shared_vmobject())
|
|
|
|
return shared_vmobject.release_nonnull();
|
2022-01-12 22:52:39 +03:00
|
|
|
auto new_physical_pages = TRY(VMObject::try_create_physical_pages(size));
|
|
|
|
auto vmobject = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SharedInodeVMObject(inode, move(new_physical_pages))));
|
2020-02-28 22:07:51 +03:00
|
|
|
vmobject->inode().set_shared_vmobject(*vmobject);
|
2019-12-19 21:13:44 +03:00
|
|
|
return vmobject;
|
2019-08-07 19:06:17 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullRefPtr<VMObject>> SharedInodeVMObject::try_clone()
|
2019-08-07 19:06:17 +03:00
|
|
|
{
|
2022-01-12 22:52:39 +03:00
|
|
|
auto new_physical_pages = TRY(this->try_clone_physical_pages());
|
|
|
|
return adopt_nonnull_ref_or_enomem<VMObject>(new (nothrow) SharedInodeVMObject(*this, move(new_physical_pages)));
|
2019-08-07 19:06:17 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 22:52:39 +03:00
|
|
|
SharedInodeVMObject::SharedInodeVMObject(Inode& inode, FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages)
|
|
|
|
: InodeVMObject(inode, move(new_physical_pages))
|
2019-08-07 19:06:17 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-12 22:52:39 +03:00
|
|
|
SharedInodeVMObject::SharedInodeVMObject(SharedInodeVMObject const& other, FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages)
|
|
|
|
: InodeVMObject(other, move(new_physical_pages))
|
2019-08-07 19:06:17 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-18 17:54:39 +03:00
|
|
|
ErrorOr<void> SharedInodeVMObject::sync(off_t offset_in_pages, size_t pages)
|
2021-11-17 21:33:00 +03:00
|
|
|
{
|
|
|
|
SpinlockLocker locker(m_lock);
|
|
|
|
|
2021-11-18 17:54:39 +03:00
|
|
|
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) {
|
2021-11-17 21:33:00 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
TRY(m_inode->write_bytes(page_index * PAGE_SIZE, PAGE_SIZE, UserOrKernelBuffer::for_kernel_buffer(page_buffer), nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|