Kernel: add basic disk cache
ATADevices now add disk cache to themselves
This commit is contained in:
@@ -15,13 +15,15 @@ namespace Kernel
|
||||
{ }
|
||||
BAN::ErrorOr<void> initialize(ATABus::DeviceType, const uint16_t*);
|
||||
|
||||
virtual BAN::ErrorOr<void> read_sectors(uint64_t, uint8_t, uint8_t*) override;
|
||||
virtual BAN::ErrorOr<void> write_sectors(uint64_t, uint8_t, const uint8_t*) override;
|
||||
virtual uint32_t sector_size() const override { return m_sector_words * 2; }
|
||||
virtual uint64_t total_size() const override { return m_lba_count * sector_size(); }
|
||||
|
||||
BAN::StringView model() const { return m_model; }
|
||||
|
||||
protected:
|
||||
virtual BAN::ErrorOr<void> read_sectors_impl(uint64_t, uint8_t, uint8_t*) override;
|
||||
virtual BAN::ErrorOr<void> write_sectors_impl(uint64_t, uint8_t, const uint8_t*) override;
|
||||
|
||||
private:
|
||||
ATABus* m_bus;
|
||||
uint8_t m_index;
|
||||
|
||||
47
kernel/include/kernel/Storage/DiskCache.h
Normal file
47
kernel/include/kernel/Storage/DiskCache.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Array.h>
|
||||
#include <kernel/Memory/Types.h>
|
||||
#include <kernel/SpinLock.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class StorageDevice;
|
||||
|
||||
class DiskCache
|
||||
{
|
||||
public:
|
||||
DiskCache(StorageDevice&);
|
||||
~DiskCache();
|
||||
|
||||
BAN::ErrorOr<void> read_sector(uint64_t sector, uint8_t* buffer);
|
||||
BAN::ErrorOr<void> write_sector(uint64_t sector, const uint8_t* buffer);
|
||||
|
||||
size_t release_clean_pages(size_t);
|
||||
size_t release_pages(size_t);
|
||||
void release_all_pages();
|
||||
|
||||
private:
|
||||
struct SectorCache
|
||||
{
|
||||
uint64_t sector { 0 };
|
||||
bool dirty { false };
|
||||
};
|
||||
struct CacheBlock
|
||||
{
|
||||
paddr_t paddr { 0 };
|
||||
BAN::Array<SectorCache, 4> sectors;
|
||||
|
||||
void sync(StorageDevice&);
|
||||
void read_sector(StorageDevice&, size_t, uint8_t*);
|
||||
void write_sector(StorageDevice&, size_t, const uint8_t*);
|
||||
};
|
||||
|
||||
private:
|
||||
SpinLock m_lock;
|
||||
StorageDevice& m_device;
|
||||
BAN::Vector<CacheBlock> m_cache;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <BAN/Vector.h>
|
||||
#include <kernel/Device.h>
|
||||
#include <kernel/Storage/DiskCache.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
@@ -61,20 +62,29 @@ namespace Kernel
|
||||
class StorageDevice : public BlockDevice
|
||||
{
|
||||
public:
|
||||
virtual ~StorageDevice() {}
|
||||
virtual ~StorageDevice();
|
||||
|
||||
BAN::ErrorOr<void> initialize_partitions();
|
||||
|
||||
virtual BAN::ErrorOr<void> read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer) = 0;
|
||||
virtual BAN::ErrorOr<void> write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer) = 0;
|
||||
BAN::ErrorOr<void> read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer);
|
||||
BAN::ErrorOr<void> write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer);
|
||||
|
||||
virtual uint32_t sector_size() const = 0;
|
||||
virtual uint64_t total_size() const = 0;
|
||||
|
||||
BAN::Vector<Partition*>& partitions() { return m_partitions; }
|
||||
const BAN::Vector<Partition*>& partitions() const { return m_partitions; }
|
||||
|
||||
protected:
|
||||
virtual BAN::ErrorOr<void> read_sectors_impl(uint64_t lba, uint8_t sector_count, uint8_t* buffer) = 0;
|
||||
virtual BAN::ErrorOr<void> write_sectors_impl(uint64_t lba, uint8_t sector_count, const uint8_t* buffer) = 0;
|
||||
void add_disk_cache();
|
||||
|
||||
private:
|
||||
DiskCache* m_disk_cache { nullptr };
|
||||
BAN::Vector<Partition*> m_partitions;
|
||||
|
||||
friend class DiskCache;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user