Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <Kernel/FileSystem/Inode.h>
28#include <Kernel/VM/InodeVMObject.h>
29#include <Kernel/VM/MemoryManager.h>
30#include <Kernel/VM/Region.h>
31
32namespace Kernel {
33
34InodeVMObject::InodeVMObject(Inode& inode, size_t size)
35 : VMObject(size)
36 , m_inode(inode)
37 , m_dirty_pages(page_count(), false)
38{
39}
40
41InodeVMObject::InodeVMObject(const InodeVMObject& other)
42 : VMObject(other)
43 , m_inode(other.m_inode)
44 , m_dirty_pages(page_count(), false)
45{
46 for (size_t i = 0; i < page_count(); ++i)
47 m_dirty_pages.set(i, other.m_dirty_pages.get(i));
48}
49
50InodeVMObject::~InodeVMObject()
51{
52}
53
54size_t InodeVMObject::amount_clean() const
55{
56 size_t count = 0;
57 ASSERT(page_count() == m_dirty_pages.size());
58 for (size_t i = 0; i < page_count(); ++i) {
59 if (!m_dirty_pages.get(i) && m_physical_pages[i])
60 ++count;
61 }
62 return count * PAGE_SIZE;
63}
64
65size_t InodeVMObject::amount_dirty() const
66{
67 size_t count = 0;
68 for (size_t i = 0; i < m_dirty_pages.size(); ++i) {
69 if (m_dirty_pages.get(i))
70 ++count;
71 }
72 return count * PAGE_SIZE;
73}
74
75void InodeVMObject::inode_size_changed(Badge<Inode>, size_t old_size, size_t new_size)
76{
77 dbg() << "VMObject::inode_size_changed: {" << m_inode->fsid() << ":" << m_inode->index() << "} " << old_size << " -> " << new_size;
78
79 InterruptDisabler disabler;
80
81 auto new_page_count = PAGE_ROUND_UP(new_size) / PAGE_SIZE;
82 m_physical_pages.resize(new_page_count);
83
84 m_dirty_pages.grow(new_page_count, false);
85
86 // FIXME: Consolidate with inode_contents_changed() so we only do a single walk.
87 for_each_region([](auto& region) {
88 region.remap();
89 });
90}
91
92void InodeVMObject::inode_contents_changed(Badge<Inode>, off_t offset, ssize_t size, const u8* data)
93{
94 (void)size;
95 (void)data;
96 InterruptDisabler disabler;
97 ASSERT(offset >= 0);
98
99 // FIXME: Only invalidate the parts that actually changed.
100 for (auto& physical_page : m_physical_pages)
101 physical_page = nullptr;
102
103#if 0
104 size_t current_offset = offset;
105 size_t remaining_bytes = size;
106 const u8* data_ptr = data;
107
108 auto to_page_index = [] (size_t offset) -> size_t {
109 return offset / PAGE_SIZE;
110 };
111
112 if (current_offset & PAGE_MASK) {
113 size_t page_index = to_page_index(current_offset);
114 size_t bytes_to_copy = min(size, PAGE_SIZE - (current_offset & PAGE_MASK));
115 if (m_physical_pages[page_index]) {
116 auto* ptr = MM.quickmap_page(*m_physical_pages[page_index]);
117 memcpy(ptr, data_ptr, bytes_to_copy);
118 MM.unquickmap_page();
119 }
120 current_offset += bytes_to_copy;
121 data += bytes_to_copy;
122 remaining_bytes -= bytes_to_copy;
123 }
124
125 for (size_t page_index = to_page_index(current_offset); page_index < m_physical_pages.size(); ++page_index) {
126 size_t bytes_to_copy = PAGE_SIZE - (current_offset & PAGE_MASK);
127 if (m_physical_pages[page_index]) {
128 auto* ptr = MM.quickmap_page(*m_physical_pages[page_index]);
129 memcpy(ptr, data_ptr, bytes_to_copy);
130 MM.unquickmap_page();
131 }
132 current_offset += bytes_to_copy;
133 data += bytes_to_copy;
134 }
135#endif
136
137 // FIXME: Consolidate with inode_size_changed() so we only do a single walk.
138 for_each_region([](auto& region) {
139 region.remap();
140 });
141}
142
143int InodeVMObject::release_all_clean_pages()
144{
145 LOCKER(m_paging_lock);
146 return release_all_clean_pages_impl();
147}
148
149int InodeVMObject::release_all_clean_pages_impl()
150{
151 int count = 0;
152 InterruptDisabler disabler;
153 for (size_t i = 0; i < page_count(); ++i) {
154 if (!m_dirty_pages.get(i) && m_physical_pages[i]) {
155 m_physical_pages[i] = nullptr;
156 ++count;
157 }
158 }
159 for_each_region([](auto& region) {
160 region.remap();
161 });
162 return count;
163}
164
165u32 InodeVMObject::writable_mappings() const
166{
167 u32 count = 0;
168 const_cast<InodeVMObject&>(*this).for_each_region([&](auto& region) {
169 if (region.is_writable())
170 ++count;
171 });
172 return count;
173}
174
175u32 InodeVMObject::executable_mappings() const
176{
177 u32 count = 0;
178 const_cast<InodeVMObject&>(*this).for_each_region([&](auto& region) {
179 if (region.is_executable())
180 ++count;
181 });
182 return count;
183}
184
185}