Kernel: Lock process functions instead of the ata controller

Process has to use locks at least on some functions so multithreaded
disk io is safe. This seemed to fix corrupted reads from disk
This commit is contained in:
Bananymous 2023-03-24 01:17:39 +02:00
parent 7d2ab53baa
commit 310713d203
4 changed files with 18 additions and 9 deletions

View File

@ -4,6 +4,7 @@
#include <BAN/StringView.h> #include <BAN/StringView.h>
#include <BAN/Vector.h> #include <BAN/Vector.h>
#include <kernel/FS/Inode.h> #include <kernel/FS/Inode.h>
#include <kernel/SpinLock.h>
#include <kernel/Thread.h> #include <kernel/Thread.h>
namespace Kernel namespace Kernel
@ -60,6 +61,8 @@ namespace Kernel
BAN::Vector<OpenFileDescription> m_open_files; BAN::Vector<OpenFileDescription> m_open_files;
mutable SpinLock m_lock;
pid_t m_pid = 0; pid_t m_pid = 0;
BAN::String m_working_directory; BAN::String m_working_directory;
BAN::Vector<BAN::RefPtr<Thread>> m_threads; BAN::Vector<BAN::RefPtr<Thread>> m_threads;

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <kernel/SpinLock.h>
#include <kernel/Storage/StorageController.h> #include <kernel/Storage/StorageController.h>
namespace Kernel namespace Kernel
@ -68,7 +67,6 @@ namespace Kernel
BAN::ErrorOr<void> write(ATADevice*, uint64_t, uint8_t, const uint8_t*); BAN::ErrorOr<void> write(ATADevice*, uint64_t, uint8_t, const uint8_t*);
private: private:
SpinLock m_lock;
ATABus m_buses[2]; ATABus m_buses[2];
const PCIDevice& m_pci_device; const PCIDevice& m_pci_device;

View File

@ -1,7 +1,8 @@
#include <BAN/StringView.h> #include <BAN/StringView.h>
#include <kernel/FS/VirtualFileSystem.h>
#include <kernel/LockGuard.h>
#include <kernel/Process.h> #include <kernel/Process.h>
#include <kernel/Scheduler.h> #include <kernel/Scheduler.h>
#include <kernel/FS/VirtualFileSystem.h>
#include <fcntl.h> #include <fcntl.h>
@ -37,6 +38,8 @@ namespace Kernel
BAN::ErrorOr<int> Process::open(BAN::StringView path, int flags) BAN::ErrorOr<int> Process::open(BAN::StringView path, int flags)
{ {
LockGuard _(m_lock);
if (flags != O_RDONLY) if (flags != O_RDONLY)
return BAN::Error::from_errno(ENOTSUP); return BAN::Error::from_errno(ENOTSUP);
@ -56,6 +59,8 @@ namespace Kernel
BAN::ErrorOr<void> Process::close(int fd) BAN::ErrorOr<void> Process::close(int fd)
{ {
LockGuard _(m_lock);
TRY(validate_fd(fd)); TRY(validate_fd(fd));
auto& open_file_description = this->open_file_description(fd); auto& open_file_description = this->open_file_description(fd);
open_file_description.inode = nullptr; open_file_description.inode = nullptr;
@ -64,6 +69,8 @@ namespace Kernel
BAN::ErrorOr<size_t> Process::read(int fd, void* buffer, size_t count) BAN::ErrorOr<size_t> Process::read(int fd, void* buffer, size_t count)
{ {
LockGuard _(m_lock);
TRY(validate_fd(fd)); TRY(validate_fd(fd));
auto& open_file_description = this->open_file_description(fd); auto& open_file_description = this->open_file_description(fd);
if (open_file_description.offset >= open_file_description.inode->size()) if (open_file_description.offset >= open_file_description.inode->size())
@ -74,6 +81,8 @@ namespace Kernel
BAN::ErrorOr<void> Process::creat(BAN::StringView path, mode_t mode) BAN::ErrorOr<void> Process::creat(BAN::StringView path, mode_t mode)
{ {
LockGuard _(m_lock);
auto absolute_path = TRY(absolute_path_of(path)); auto absolute_path = TRY(absolute_path_of(path));
while (absolute_path.sv().back() != '/') while (absolute_path.sv().back() != '/')
absolute_path.pop_back(); absolute_path.pop_back();
@ -86,12 +95,16 @@ namespace Kernel
Inode& Process::inode_for_fd(int fd) Inode& Process::inode_for_fd(int fd)
{ {
LockGuard _(m_lock);
MUST(validate_fd(fd)); MUST(validate_fd(fd));
return *open_file_description(fd).inode; return *open_file_description(fd).inode;
} }
BAN::ErrorOr<void> Process::set_working_directory(BAN::StringView path) BAN::ErrorOr<void> Process::set_working_directory(BAN::StringView path)
{ {
LockGuard _(m_lock);
BAN::String absolute_path = TRY(absolute_path_of(path)); BAN::String absolute_path = TRY(absolute_path_of(path));
auto file = TRY(VirtualFileSystem::get().file_from_absolute_path(absolute_path)); auto file = TRY(VirtualFileSystem::get().file_from_absolute_path(absolute_path));

View File

@ -1,5 +1,4 @@
#include <kernel/IO.h> #include <kernel/IO.h>
#include <kernel/LockGuard.h>
#include <kernel/Storage/ATAController.h> #include <kernel/Storage/ATAController.h>
#define ATA_PRIMARY 0 #define ATA_PRIMARY 0
@ -191,8 +190,6 @@ namespace Kernel
if (lba + sector_count > device->lba_count) if (lba + sector_count > device->lba_count)
return BAN::Error::from_c_string("Attempted to read outside of the device boundaries"); return BAN::Error::from_c_string("Attempted to read outside of the device boundaries");
LockGuard _(m_lock);
if (lba < (1 << 28)) if (lba < (1 << 28))
{ {
// LBA28 // LBA28
@ -223,8 +220,6 @@ namespace Kernel
if (lba + sector_count > device->lba_count) if (lba + sector_count > device->lba_count)
return BAN::Error::from_c_string("Attempted to read outside of the device boundaries"); return BAN::Error::from_c_string("Attempted to read outside of the device boundaries");
LockGuard _(m_lock);
if (lba < (1 << 28)) if (lba < (1 << 28))
{ {
// LBA28 // LBA28
@ -247,8 +242,8 @@ namespace Kernel
ASSERT(false); ASSERT(false);
} }
device->bus->write(ATA_PORT_COMMAND, ATA_COMMAND_CACHE_FLUSH);
TRY(device->bus->wait(false)); TRY(device->bus->wait(false));
device->bus->write(ATA_PORT_COMMAND, ATA_COMMAND_CACHE_FLUSH);
return {}; return {};
} }