InodeVMObject.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <Kernel/FileSystem/Inode.h>
  27. #include <Kernel/VM/InodeVMObject.h>
  28. #include <Kernel/VM/MemoryManager.h>
  29. #include <Kernel/VM/Region.h>
  30. namespace Kernel {
  31. InodeVMObject::InodeVMObject(Inode& inode, size_t size)
  32. : VMObject(size)
  33. , m_inode(inode)
  34. , m_dirty_pages(page_count(), false)
  35. {
  36. }
  37. InodeVMObject::InodeVMObject(const InodeVMObject& other)
  38. : VMObject(other)
  39. , m_inode(other.m_inode)
  40. , m_dirty_pages(page_count(), false)
  41. {
  42. for (size_t i = 0; i < page_count(); ++i)
  43. m_dirty_pages.set(i, other.m_dirty_pages.get(i));
  44. }
  45. InodeVMObject::~InodeVMObject()
  46. {
  47. }
  48. size_t InodeVMObject::amount_clean() const
  49. {
  50. size_t count = 0;
  51. ASSERT(page_count() == m_dirty_pages.size());
  52. for (size_t i = 0; i < page_count(); ++i) {
  53. if (!m_dirty_pages.get(i) && m_physical_pages[i])
  54. ++count;
  55. }
  56. return count * PAGE_SIZE;
  57. }
  58. size_t InodeVMObject::amount_dirty() const
  59. {
  60. size_t count = 0;
  61. for (size_t i = 0; i < m_dirty_pages.size(); ++i) {
  62. if (m_dirty_pages.get(i))
  63. ++count;
  64. }
  65. return count * PAGE_SIZE;
  66. }
  67. void InodeVMObject::inode_size_changed(Badge<Inode>, size_t old_size, size_t new_size)
  68. {
  69. dbgln("VMObject::inode_size_changed: ({}:{}) {} -> {}", m_inode->fsid(), m_inode->index(), old_size, new_size);
  70. InterruptDisabler disabler;
  71. auto new_page_count = PAGE_ROUND_UP(new_size) / PAGE_SIZE;
  72. m_physical_pages.resize(new_page_count);
  73. m_dirty_pages.grow(new_page_count, false);
  74. // FIXME: Consolidate with inode_contents_changed() so we only do a single walk.
  75. for_each_region([](auto& region) {
  76. region.remap();
  77. });
  78. }
  79. void InodeVMObject::inode_contents_changed(Badge<Inode>, off_t offset, [[maybe_unused]] ssize_t size, [[maybe_unused]] const UserOrKernelBuffer& data)
  80. {
  81. InterruptDisabler disabler;
  82. ASSERT(offset >= 0);
  83. // FIXME: Only invalidate the parts that actually changed.
  84. for (auto& physical_page : m_physical_pages)
  85. physical_page = nullptr;
  86. #if 0
  87. size_t current_offset = offset;
  88. size_t remaining_bytes = size;
  89. const u8* data_ptr = data;
  90. auto to_page_index = [] (size_t offset) -> size_t {
  91. return offset / PAGE_SIZE;
  92. };
  93. if (current_offset & PAGE_MASK) {
  94. size_t page_index = to_page_index(current_offset);
  95. size_t bytes_to_copy = min(size, PAGE_SIZE - (current_offset & PAGE_MASK));
  96. if (m_physical_pages[page_index]) {
  97. auto* ptr = MM.quickmap_page(*m_physical_pages[page_index]);
  98. memcpy(ptr, data_ptr, bytes_to_copy);
  99. MM.unquickmap_page();
  100. }
  101. current_offset += bytes_to_copy;
  102. data += bytes_to_copy;
  103. remaining_bytes -= bytes_to_copy;
  104. }
  105. for (size_t page_index = to_page_index(current_offset); page_index < m_physical_pages.size(); ++page_index) {
  106. size_t bytes_to_copy = PAGE_SIZE - (current_offset & PAGE_MASK);
  107. if (m_physical_pages[page_index]) {
  108. auto* ptr = MM.quickmap_page(*m_physical_pages[page_index]);
  109. memcpy(ptr, data_ptr, bytes_to_copy);
  110. MM.unquickmap_page();
  111. }
  112. current_offset += bytes_to_copy;
  113. data += bytes_to_copy;
  114. }
  115. #endif
  116. // FIXME: Consolidate with inode_size_changed() so we only do a single walk.
  117. for_each_region([](auto& region) {
  118. region.remap();
  119. });
  120. }
  121. int InodeVMObject::release_all_clean_pages()
  122. {
  123. LOCKER(m_paging_lock);
  124. return release_all_clean_pages_impl();
  125. }
  126. int InodeVMObject::release_all_clean_pages_impl()
  127. {
  128. int count = 0;
  129. InterruptDisabler disabler;
  130. for (size_t i = 0; i < page_count(); ++i) {
  131. if (!m_dirty_pages.get(i) && m_physical_pages[i]) {
  132. m_physical_pages[i] = nullptr;
  133. ++count;
  134. }
  135. }
  136. for_each_region([](auto& region) {
  137. region.remap();
  138. });
  139. return count;
  140. }
  141. u32 InodeVMObject::writable_mappings() const
  142. {
  143. u32 count = 0;
  144. const_cast<InodeVMObject&>(*this).for_each_region([&](auto& region) {
  145. if (region.is_writable())
  146. ++count;
  147. });
  148. return count;
  149. }
  150. u32 InodeVMObject::executable_mappings() const
  151. {
  152. u32 count = 0;
  153. const_cast<InodeVMObject&>(*this).for_each_region([&](auto& region) {
  154. if (region.is_executable())
  155. ++count;
  156. });
  157. return count;
  158. }
  159. }