diff --git a/src/LibHac/FsSystem/BufferedStorage.cs b/src/LibHac/FsSystem/BufferedStorage.cs
index 2bc7adf3..07d2803d 100644
--- a/src/LibHac/FsSystem/BufferedStorage.cs
+++ b/src/LibHac/FsSystem/BufferedStorage.cs
@@ -16,6 +16,7 @@ namespace LibHac.FsSystem;
///
/// An that provides buffered access to a base .
///
+/// Based on FS 13.1.0 (nnSdk 13.4.0)
public class BufferedStorage : IStorage
{
private const long InvalidOffset = long.MaxValue;
@@ -673,6 +674,8 @@ public class BufferedStorage : IStorage
ref Cache end = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches),
BufferedStorage._cacheCount);
+ using var lk = new ScopedLock(ref BufferedStorage._mutex);
+
// Validate the range.
Assert.SdkAssert(!Unsafe.IsAddressLessThan(ref start,
ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches)));
@@ -716,6 +719,8 @@ public class BufferedStorage : IStorage
ref Cache end = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches),
BufferedStorage._cacheCount);
+ using var lk = new ScopedLock(ref BufferedStorage._mutex);
+
// Validate the range.
Assert.SdkAssert(!Unsafe.IsAddressLessThan(ref start,
ref MemoryMarshal.GetArrayDataReference(BufferedStorage._caches)));
@@ -1161,10 +1166,7 @@ public class BufferedStorage : IStorage
// Invalidate caches if needed.
if (operationId == OperationId.InvalidateCache)
{
- using var cache = new SharedCache(this);
-
- while (cache.AcquireNextOverlappedCache(offset, size))
- cache.Invalidate();
+ InvalidateCaches();
}
return _baseStorage.OperateRange(outBuffer, operationId, offset, size, inBuffer);
@@ -1351,13 +1353,13 @@ public class BufferedStorage : IStorage
while (true)
{
if (!cache.AcquireFetchableCache())
- return ResultFs.OutOfResource.Value;
+ return ResultFs.OutOfResource.Log();
// Try to upgrade out SharedCache to a UniqueCache
using var fetchCache = new UniqueCache(this);
(Result Result, bool wasUpgradeSuccessful) upgradeResult = fetchCache.Upgrade(in cache);
if (upgradeResult.Result.IsFailure())
- return upgradeResult.Result;
+ return upgradeResult.Result.Miss();
// Fetch the data from the base storage into the cache buffer if successful
if (upgradeResult.wasUpgradeSuccessful)
@@ -1676,12 +1678,12 @@ public class BufferedStorage : IStorage
while (true)
{
if (!cache.AcquireFetchableCache())
- return ResultFs.OutOfResource.Value;
+ return ResultFs.OutOfResource.Log();
using var fetchCache = new UniqueCache(this);
(Result Result, bool wasUpgradeSuccessful) upgradeResult = fetchCache.Upgrade(in cache);
if (upgradeResult.Result.IsFailure())
- return upgradeResult.Result;
+ return upgradeResult.Result.Miss();
if (upgradeResult.wasUpgradeSuccessful)
{