From 1abf54d652c042444b3bc984aa0088002652aed5 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 8 Sep 2023 02:43:08 +0300 Subject: [PATCH] Kernel: Fix bug in disk writing I have used two weeks in locating a bug in my ext2 implementation while the bug was actually in disk write. If you called write_sectors on disk it would write the first sector_size bytes repeatedly to all asked sectors and this corrupted the disk... --- kernel/kernel/Storage/StorageDevice.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/kernel/Storage/StorageDevice.cpp b/kernel/kernel/Storage/StorageDevice.cpp index 98d12f0a..f59c32ab 100644 --- a/kernel/kernel/Storage/StorageDevice.cpp +++ b/kernel/kernel/Storage/StorageDevice.cpp @@ -279,12 +279,13 @@ namespace Kernel BAN::ErrorOr StorageDevice::write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer) { // TODO: use disk cache for dirty pages. I don't wanna think about how to do it safely now - for (uint8_t sector = 0; sector < sector_count; sector++) + for (uint8_t offset = 0; offset < sector_count; offset++) { Thread::TerminateBlocker _(Thread::current()); - TRY(write_sectors_impl(lba + sector, 1, buffer)); + const uint8_t* buffer_ptr = buffer + offset * sector_size(); + TRY(write_sectors_impl(lba + offset, 1, buffer_ptr)); if (m_disk_cache.has_value()) - (void)m_disk_cache->write_to_cache(lba + sector, buffer + sector * sector_size(), false); + (void)m_disk_cache->write_to_cache(lba + offset, buffer_ptr, false); } return {};