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