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