| 75 | }; |
| 76 | |
| 77 | BAN::ErrorOr<void> DiskCache::write_to_cache(uint64_t sector, BAN::ConstByteSpan buffer, bool dirty) |
| 78 | { |
| 79 | ASSERT(buffer.size() >= m_sector_size); |
| 80 | |
| 81 | const uint64_t sectors_per_page = PAGE_SIZE / m_sector_size; |
| 82 | const uint64_t page_cache_offset = sector % sectors_per_page; |
| 83 | const uint64_t page_cache_start = sector - page_cache_offset; |
| 84 | |
| 85 | RWLockWRGuard _(m_rw_lock); |
| 86 | |
| 87 | const auto index = find_sector_cache_index(sector); |
| 88 | |
| 89 | if (index >= m_cache.size() || m_cache[index].first_sector != page_cache_start) |
| 90 | { |
| 91 | paddr_t paddr = Heap::get().take_free_page(); |
| 92 | if (paddr == 0) |
| 93 | return BAN::Error::from_errno(ENOMEM); |
| 94 | |
| 95 | PageCache cache { |
| 96 | .paddr = paddr, |
| 97 | .first_sector = page_cache_start, |
| 98 | .sector_mask = 0, |
| 99 | .dirty_mask = 0, |
| 100 | }; |
| 101 | |
| 102 | if (auto ret = m_cache.insert(index, cache); ret.is_error()) |
| 103 | { |
| 104 | Heap::get().release_page(paddr); |
| 105 | return ret.error(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | auto& cache = m_cache[index]; |
| 110 | |
| 111 | PageTable::with_per_cpu_fast_page(cache.paddr, [&](void* addr) { |
| 112 | memcpy(static_cast<uint8_t*>(addr) + page_cache_offset * m_sector_size, buffer.data(), m_sector_size); |
| 113 | }); |
| 114 | |
| 115 | cache.sector_mask |= 1 << page_cache_offset; |
| 116 | if (dirty) |
| 117 | cache.dirty_mask |= 1 << page_cache_offset; |
| 118 | |
| 119 | return {}; |
| 120 | } |
| 121 | |
| 122 | BAN::ErrorOr<void> DiskCache::sync_cache_index(size_t index) |
| 123 | { |
no test coverage detected