fs: move bitmap classes out of save::

This commit is contained in:
Michael Scire
2022-03-12 14:08:39 -08:00
parent d76e678da2
commit d6dbfd356d
14 changed files with 52 additions and 85 deletions

View File

@@ -1,180 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os.hpp>
#include <stratosphere/fs/fs_storage_type.hpp>
#include <stratosphere/fs/fs_istorage.hpp>
#include <stratosphere/fs/fs_memory_management.hpp>
#include <stratosphere/fssystem/save/fssystem_i_save_file_system_driver.hpp>
#include <stratosphere/fssystem/buffers/fssystem_file_system_buffer_manager.hpp>
#include <stratosphere/fssystem/impl/fssystem_block_cache_manager.hpp>
namespace ams::fssystem::save {
constexpr inline size_t IntegrityMinLayerCount = 2;
constexpr inline size_t IntegrityMaxLayerCount = 7;
constexpr inline size_t IntegrityLayerCountSave = 5;
constexpr inline size_t IntegrityLayerCountSaveDataMeta = 4;
struct FileSystemBufferManagerSet {
fs::IBufferManager *buffers[IntegrityMaxLayerCount];
};
static_assert(util::is_pod<FileSystemBufferManagerSet>::value);
class BlockCacheBufferedStorage : public ::ams::fs::IStorage {
NON_COPYABLE(BlockCacheBufferedStorage);
NON_MOVEABLE(BlockCacheBufferedStorage);
public:
static constexpr size_t DefaultMaxCacheEntryCount = 24;
private:
using MemoryRange = fs::IBufferManager::MemoryRange;
struct AccessRange {
s64 offset;
size_t size;
s64 GetEndOffset() const {
return this->offset + this->size;
}
bool IsIncluded(s64 ofs) const {
return this->offset <= ofs && ofs < this->GetEndOffset();
}
};
static_assert(util::is_pod<AccessRange>::value);
struct CacheEntry {
AccessRange range;
bool is_valid;
bool is_write_back;
bool is_cached;
bool is_flushing;
u16 lru_counter;
fs::IBufferManager::CacheHandle handle;
uintptr_t memory_address;
size_t memory_size;
void Invalidate() {
this->is_write_back = false;
this->is_flushing = false;
}
bool IsAllocated() const {
return this->is_valid && (this->is_write_back ? this->memory_address != 0 : this->handle != 0);
}
bool IsWriteBack() const {
return this->is_write_back;
}
};
static_assert(util::is_pod<CacheEntry>::value);
using BlockCacheManager = ::ams::fssystem::impl::BlockCacheManager<CacheEntry, fs::IBufferManager>;
using CacheIndex = BlockCacheManager::CacheIndex;
enum Flag : s32 {
Flag_KeepBurstMode = (1 << 8),
Flag_RealData = (1 << 10),
};
private:
os::SdkRecursiveMutex *m_mutex;
IStorage *m_data_storage;
Result m_last_result;
s64 m_data_size;
size_t m_verification_block_size;
size_t m_verification_block_shift;
s32 m_flags;
s32 m_buffer_level;
fs::StorageType m_storage_type;
BlockCacheManager m_block_cache_manager;
public:
BlockCacheBufferedStorage();
virtual ~BlockCacheBufferedStorage() override;
Result Initialize(fs::IBufferManager *bm, os::SdkRecursiveMutex *mtx, IStorage *data, s64 data_size, size_t verif_block_size, s32 max_cache_entries, bool is_real_data, s8 buffer_level, bool is_keep_burst_mode, fs::StorageType storage_type);
void Finalize();
virtual Result Read(s64 offset, void *buffer, size_t size) override;
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
virtual Result SetSize(s64) override { R_THROW(fs::ResultUnsupportedOperationInBlockCacheBufferedStorageA()); }
virtual Result GetSize(s64 *out) override;
virtual Result Flush() override;
virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
using IStorage::OperateRange;
Result Commit();
Result OnRollback();
bool IsEnabledKeepBurstMode() const {
return (m_flags & Flag_KeepBurstMode) != 0;
}
bool IsRealDataCache() const {
return (m_flags & Flag_RealData) != 0;
}
void SetKeepBurstMode(bool en) {
if (en) {
m_flags |= Flag_KeepBurstMode;
} else {
m_flags &= ~Flag_KeepBurstMode;
}
}
void SetRealDataCache(bool en) {
if (en) {
m_flags |= Flag_RealData;
} else {
m_flags &= ~Flag_RealData;
}
}
private:
Result FillZeroImpl(s64 offset, s64 size);
Result DestroySignatureImpl(s64 offset, s64 size);
Result InvalidateImpl();
Result QueryRangeImpl(void *dst, size_t dst_size, s64 offset, s64 size);
Result GetAssociateBuffer(MemoryRange *out_range, CacheEntry *out_entry, s64 offset, size_t ideal_size, bool is_allocate_for_write);
Result StoreOrDestroyBuffer(CacheIndex *out, const MemoryRange &range, CacheEntry *entry);
Result StoreOrDestroyBuffer(const MemoryRange &range, CacheEntry *entry) {
AMS_ASSERT(entry != nullptr);
CacheIndex dummy;
R_RETURN(this->StoreOrDestroyBuffer(std::addressof(dummy), range, entry));
}
Result FlushCacheEntry(CacheIndex index, bool invalidate);
Result FlushRangeCacheEntries(s64 offset, s64 size, bool invalidate);
Result FlushAllCacheEntries();
Result InvalidateAllCacheEntries();
Result ControlDirtiness();
Result UpdateLastResult(Result result);
Result ReadHeadCache(MemoryRange *out_range, CacheEntry *out_entry, bool *out_cache_needed, s64 *offset, s64 *aligned_offset, s64 aligned_offset_end, char **buffer, size_t *size);
Result ReadTailCache(MemoryRange *out_range, CacheEntry *out_entry, bool *out_cache_needed, s64 offset, s64 aligned_offset, s64 *aligned_offset_end, char *buffer, size_t *size);
Result BulkRead(s64 offset, void *buffer, size_t size, MemoryRange *range_head, MemoryRange *range_tail, CacheEntry *entry_head, CacheEntry *entry_tail, bool head_cache_needed, bool tail_cache_needed);
};
}

View File

@@ -1,80 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os.hpp>
#include <stratosphere/fs/fs_istorage.hpp>
#include <stratosphere/fs/fs_substorage.hpp>
#include <stratosphere/fs/fs_i_buffer_manager.hpp>
namespace ams::fssystem::save {
class BufferedStorage : public ::ams::fs::IStorage {
NON_COPYABLE(BufferedStorage);
NON_MOVEABLE(BufferedStorage);
private:
class Cache;
class UniqueCache;
class SharedCache;
private:
fs::SubStorage m_base_storage;
fs::IBufferManager *m_buffer_manager;
size_t m_block_size;
s64 m_base_storage_size;
std::unique_ptr<Cache[]> m_caches;
s32 m_cache_count;
Cache *m_next_acquire_cache;
Cache *m_next_fetch_cache;
os::SdkMutex m_mutex;
bool m_bulk_read_enabled;
public:
BufferedStorage();
virtual ~BufferedStorage();
Result Initialize(fs::SubStorage base_storage, fs::IBufferManager *buffer_manager, size_t block_size, s32 buffer_count);
void Finalize();
bool IsInitialized() const { return m_caches != nullptr; }
virtual Result Read(s64 offset, void *buffer, size_t size) override;
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
virtual Result GetSize(s64 *out) override;
virtual Result SetSize(s64 size) override;
virtual Result Flush() override;
virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
using IStorage::OperateRange;
void InvalidateCaches();
fs::IBufferManager *GetBufferManager() const { return m_buffer_manager; }
void EnableBulkRead() { m_bulk_read_enabled = true; }
private:
Result PrepareAllocation();
Result ControlDirtiness();
Result ReadCore(s64 offset, void *buffer, size_t size);
bool ReadHeadCache(s64 *offset, void *buffer, size_t *size, s64 *buffer_offset);
bool ReadTailCache(s64 offset, void *buffer, size_t *size, s64 buffer_offset);
Result BulkRead(s64 offset, void *buffer, size_t size, bool head_cache_needed, bool tail_cache_needed);
Result WriteCore(s64 offset, const void *buffer, size_t size);
};
}

View File

@@ -1,205 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os.hpp>
#include <stratosphere/fs/fs_istorage.hpp>
#include <stratosphere/fs/fs_substorage.hpp>
#include <stratosphere/fs/fs_storage_type.hpp>
#include <stratosphere/fssystem/save/fssystem_i_save_file.hpp>
#include <stratosphere/fssystem/save/fssystem_integrity_verification_storage.hpp>
#include <stratosphere/fssystem/save/fssystem_block_cache_buffered_storage.hpp>
namespace ams::fssystem::save {
struct HierarchicalIntegrityVerificationLevelInformation {
fs::Int64 offset;
fs::Int64 size;
s32 block_order;
u8 reserved[4];
};
static_assert(util::is_pod<HierarchicalIntegrityVerificationLevelInformation>::value);
static_assert(sizeof(HierarchicalIntegrityVerificationLevelInformation) == 0x18);
static_assert(alignof(HierarchicalIntegrityVerificationLevelInformation) == 0x4);
struct HierarchicalIntegrityVerificationInformation {
u32 max_layers;
HierarchicalIntegrityVerificationLevelInformation info[IntegrityMaxLayerCount - 1];
fs::HashSalt seed;
s64 GetLayeredHashSize() const {
return this->info[this->max_layers - 2].offset;
}
s64 GetDataOffset() const {
return this->info[this->max_layers - 2].offset;
}
s64 GetDataSize() const {
return this->info[this->max_layers - 2].size;
}
};
static_assert(util::is_pod<HierarchicalIntegrityVerificationInformation>::value);
struct HierarchicalIntegrityVerificationMetaInformation {
u32 magic;
u32 version;
u32 master_hash_size;
HierarchicalIntegrityVerificationInformation level_hash_info;
/* TODO: Format */
};
static_assert(util::is_pod<HierarchicalIntegrityVerificationMetaInformation>::value);
struct HierarchicalIntegrityVerificationSizeSet {
s64 control_size;
s64 master_hash_size;
s64 layered_hash_sizes[IntegrityMaxLayerCount - 1];
};
static_assert(util::is_pod<HierarchicalIntegrityVerificationSizeSet>::value);
class HierarchicalIntegrityVerificationStorageControlArea {
NON_COPYABLE(HierarchicalIntegrityVerificationStorageControlArea);
NON_MOVEABLE(HierarchicalIntegrityVerificationStorageControlArea);
public:
static constexpr size_t HashSize = crypto::Sha256Generator::HashSize;
struct InputParam {
size_t level_block_size[IntegrityMaxLayerCount - 1];
};
static_assert(util::is_pod<InputParam>::value);
private:
fs::SubStorage m_storage;
HierarchicalIntegrityVerificationMetaInformation m_meta;
public:
static Result QuerySize(HierarchicalIntegrityVerificationSizeSet *out, const InputParam &input_param, s32 layer_count, s64 data_size);
/* TODO Format */
static Result Expand(fs::SubStorage meta_storage, const HierarchicalIntegrityVerificationMetaInformation &meta);
public:
HierarchicalIntegrityVerificationStorageControlArea() { /* ... */ }
Result Initialize(fs::SubStorage meta_storage);
void Finalize();
u32 GetMasterHashSize() const { return m_meta.master_hash_size; }
void GetLevelHashInfo(HierarchicalIntegrityVerificationInformation *out) {
AMS_ASSERT(out != nullptr);
*out = m_meta.level_hash_info;
}
};
class HierarchicalIntegrityVerificationStorage : public ::ams::fs::IStorage {
NON_COPYABLE(HierarchicalIntegrityVerificationStorage);
NON_MOVEABLE(HierarchicalIntegrityVerificationStorage);
private:
friend struct HierarchicalIntegrityVerificationMetaInformation;
protected:
static constexpr s64 HashSize = crypto::Sha256Generator::HashSize;
static constexpr size_t MaxLayers = IntegrityMaxLayerCount;
public:
using GenerateRandomFunction = void (*)(void *dst, size_t size);
class HierarchicalStorageInformation {
public:
enum {
MasterStorage = 0,
Layer1Storage = 1,
Layer2Storage = 2,
Layer3Storage = 3,
Layer4Storage = 4,
Layer5Storage = 5,
DataStorage = 6,
};
private:
fs::SubStorage m_storages[DataStorage + 1];
public:
void SetMasterHashStorage(fs::SubStorage s) { m_storages[MasterStorage] = s; }
void SetLayer1HashStorage(fs::SubStorage s) { m_storages[Layer1Storage] = s; }
void SetLayer2HashStorage(fs::SubStorage s) { m_storages[Layer2Storage] = s; }
void SetLayer3HashStorage(fs::SubStorage s) { m_storages[Layer3Storage] = s; }
void SetLayer4HashStorage(fs::SubStorage s) { m_storages[Layer4Storage] = s; }
void SetLayer5HashStorage(fs::SubStorage s) { m_storages[Layer5Storage] = s; }
void SetDataStorage(fs::SubStorage s) { m_storages[DataStorage] = s; }
fs::SubStorage &operator[](s32 index) {
AMS_ASSERT(MasterStorage <= index && index <= DataStorage);
return m_storages[index];
}
};
private:
static GenerateRandomFunction s_generate_random;
static void SetGenerateRandomFunction(GenerateRandomFunction func) {
s_generate_random = func;
}
private:
FileSystemBufferManagerSet *m_buffers;
os::SdkRecursiveMutex *m_mutex;
IntegrityVerificationStorage m_verify_storages[MaxLayers - 1];
BlockCacheBufferedStorage m_buffer_storages[MaxLayers - 1];
s64 m_data_size;
s32 m_max_layers;
bool m_is_written_for_rollback;
public:
HierarchicalIntegrityVerificationStorage() : m_buffers(nullptr), m_mutex(nullptr), m_data_size(-1), m_is_written_for_rollback(false) { /* ... */ }
virtual ~HierarchicalIntegrityVerificationStorage() override { this->Finalize(); }
Result Initialize(const HierarchicalIntegrityVerificationInformation &info, HierarchicalStorageInformation storage, FileSystemBufferManagerSet *bufs, IHash256GeneratorFactory *hgf, os::SdkRecursiveMutex *mtx, fs::StorageType storage_type);
void Finalize();
virtual Result Read(s64 offset, void *buffer, size_t size) override;
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
virtual Result SetSize(s64 size) override { AMS_UNUSED(size); return fs::ResultUnsupportedOperationInHierarchicalIntegrityVerificationStorageA(); }
virtual Result GetSize(s64 *out) override;
virtual Result Flush() override;
virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
using IStorage::OperateRange;
Result Commit();
Result OnRollback();
bool IsInitialized() const {
return m_data_size >= 0;
}
bool IsWrittenForRollback() const {
return m_is_written_for_rollback;
}
FileSystemBufferManagerSet *GetBuffers() {
return m_buffers;
}
void GetParameters(HierarchicalIntegrityVerificationStorageControlArea::InputParam *out) const {
AMS_ASSERT(out != nullptr);
for (auto level = 0; level <= m_max_layers - 2; ++level) {
out->level_block_size[level] = static_cast<size_t>(m_verify_storages[level].GetBlockSize());
}
}
s64 GetL1HashVerificationBlockSize() const {
return m_verify_storages[m_max_layers - 2].GetBlockSize();
}
fs::SubStorage GetL1HashStorage() {
return fs::SubStorage(std::addressof(m_buffer_storages[m_max_layers - 3]), 0, util::DivideUp(m_data_size, this->GetL1HashVerificationBlockSize()));
}
};
}

View File

@@ -1,101 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os.hpp>
#include <stratosphere/fs/fs_istorage.hpp>
#include <stratosphere/fs/fs_substorage.hpp>
#include <stratosphere/fs/fs_storage_type.hpp>
#include <stratosphere/fs/fs_save_data_types.hpp>
#include <stratosphere/fssystem/save/fssystem_save_types.hpp>
#include <stratosphere/fssystem/save/fssystem_i_save_file_system_driver.hpp>
#include <stratosphere/fssystem/save/fssystem_block_cache_buffered_storage.hpp>
namespace ams::fssystem::save {
class IntegrityVerificationStorage : public ::ams::fs::IStorage {
NON_COPYABLE(IntegrityVerificationStorage);
NON_MOVEABLE(IntegrityVerificationStorage);
public:
static constexpr s64 HashSize = crypto::Sha256Generator::HashSize;
struct BlockHash {
u8 hash[HashSize];
};
static_assert(util::is_pod<BlockHash>::value);
private:
fs::SubStorage m_hash_storage;
fs::SubStorage m_data_storage;
s64 m_verification_block_size;
s64 m_verification_block_order;
s64 m_upper_layer_verification_block_size;
s64 m_upper_layer_verification_block_order;
fs::IBufferManager *m_buffer_manager;
fs::HashSalt m_salt;
bool m_is_real_data;
fs::StorageType m_storage_type;
fssystem::IHash256GeneratorFactory *m_hash_generator_factory;
public:
IntegrityVerificationStorage() : m_verification_block_size(0), m_verification_block_order(0), m_upper_layer_verification_block_size(0), m_upper_layer_verification_block_order(0), m_buffer_manager(nullptr) { /* ... */ }
virtual ~IntegrityVerificationStorage() override { this->Finalize(); }
Result Initialize(fs::SubStorage hs, fs::SubStorage ds, s64 verif_block_size, s64 upper_layer_verif_block_size, fs::IBufferManager *bm, fssystem::IHash256GeneratorFactory *hgf, const fs::HashSalt &salt, bool is_real_data, fs::StorageType storage_type);
void Finalize();
virtual Result Read(s64 offset, void *buffer, size_t size) override;
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
virtual Result SetSize(s64 size) override { AMS_UNUSED(size); return fs::ResultUnsupportedOperationInIntegrityVerificationStorageA(); }
virtual Result GetSize(s64 *out) override;
virtual Result Flush() override;
virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
using IStorage::OperateRange;
void CalcBlockHash(BlockHash *out, const void *buffer, size_t block_size) const {
auto generator = m_hash_generator_factory->Create();
return this->CalcBlockHash(out, buffer, block_size, generator);
}
s64 GetBlockSize() const {
return m_verification_block_size;
}
private:
Result ReadBlockSignature(void *dst, size_t dst_size, s64 offset, size_t size);
Result WriteBlockSignature(const void *src, size_t src_size, s64 offset, size_t size);
Result VerifyHash(const void *buf, BlockHash *hash, std::unique_ptr<fssystem::IHash256Generator> &generator);
void CalcBlockHash(BlockHash *out, const void *buffer, size_t block_size, std::unique_ptr<fssystem::IHash256Generator> &generator) const;
void CalcBlockHash(BlockHash *out, const void *buffer, std::unique_ptr<fssystem::IHash256Generator> &generator) const {
return this->CalcBlockHash(out, buffer, static_cast<size_t>(m_verification_block_size), generator);
}
Result IsCleared(bool *is_cleared, const BlockHash &hash);
private:
static void SetValidationBit(BlockHash *hash) {
AMS_ASSERT(hash != nullptr);
hash->hash[HashSize - 1] |= 0x80;
}
static bool IsValidationBit(const BlockHash *hash) {
AMS_ASSERT(hash != nullptr);
return (hash->hash[HashSize - 1] & 0x80) != 0;
}
};
}

View File

@@ -1,41 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/lmem.hpp>
#include <stratosphere/fs/fs_directory.hpp>
#include <stratosphere/fs/fs_filesystem.hpp>
#include <stratosphere/fssystem/dbm/fssystem_dbm_utils.hpp>
namespace ams::fssystem::save {
constexpr inline bool IsPowerOfTwo(s32 val) {
return util::IsPowerOfTwo(val);
}
constexpr inline u32 ILog2(u32 val) {
AMS_ASSERT(val > 0);
return (BITSIZEOF(u32) - 1 - dbm::CountLeadingZeros(val));
}
constexpr inline u32 CeilPowerOfTwo(u32 val) {
if (val == 0) {
return 1;
}
return ((1u << (BITSIZEOF(u32) - 1)) >> (dbm::CountLeadingZeros(val - 1) - 1));
}
}