Skip to content

Commit

Permalink
[NTOS:CC] Rewrite CcRosFlushDirtyPages (attempt 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
TAN-Gaming committed Nov 16, 2024
1 parent 915306a commit 26d6592
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions ntoskrnl/cc/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,39 @@ CcRosDeleteFileCache(
return STATUS_SUCCESS;
}

static
BOOLEAN
CcpAcquireFileCacheForLazyWrite(
_In_ PROS_SHARED_CACHE_MAP SharedCacheMap,
_In_ BOOLEAN Wait)
{
BOOLEAN Locked;

if (!CcpAcquireFileCacheForFlush(SharedCacheMap, Wait))
{
ASSERT(!Wait);
return FALSE;
}

Locked = SharedCacheMap->Callbacks->AcquireForLazyWrite(SharedCacheMap->LazyWriteContext, Wait);
if (!Locked)
{
ASSERT(!Wait);
CcpReleaseFileCacheFromFlush(SharedCacheMap);
}

return Locked;
}

static
VOID
CcpReleaseFileCacheFromLazyWrite(
_In_ PROS_SHARED_CACHE_MAP SharedCacheMap)
{
SharedCacheMap->Callbacks->ReleaseFromLazyWrite(SharedCacheMap->LazyWriteContext);
CcpReleaseFileCacheFromFlush(SharedCacheMap);
}

NTSTATUS
CcRosFlushDirtyPages (
ULONG Target,
Expand Down Expand Up @@ -281,7 +314,6 @@ CcRosFlushDirtyPages (
while (((current_entry != &CcDirtySharedCacheMapList) && (Target > 0)) || FlushAll)
{
PROS_SHARED_CACHE_MAP current;
BOOLEAN Locked;
LARGE_INTEGER FlushOffset;
LONGLONG ValidDataGoal;
ULONG FlushLength;
Expand Down Expand Up @@ -309,10 +341,6 @@ CcRosFlushDirtyPages (
if (CalledFromLazy && BooleanFlagOn(current->Flags, WRITEBEHIND_DISABLED))
continue;

/* Do not lazy-write the same file concurrently. Fastfat ASSERTS on that */
if (BooleanFlagOn(current->Flags, SHARED_CACHE_MAP_IN_LAZYWRITE))
continue;

KeAcquireSpinLockAtDpcLevel(&current->CacheMapLock);

ASSERT(current->DirtyPages != 0);
Expand All @@ -334,8 +362,6 @@ CcRosFlushDirtyPages (

KeReleaseSpinLockFromDpcLevel(&current->CacheMapLock);

current->Flags |= SHARED_CACHE_MAP_IN_LAZYWRITE;

/* Keep a ref on the shared cache map */
current->OpenCount++;

Expand All @@ -350,16 +376,14 @@ CcRosFlushDirtyPages (
FlushLength = min(min((LONGLONG)Target * PAGE_SIZE, ValidDataGoal), MAX_FLUSH_LENGTH);
}

Locked = current->Callbacks->AcquireForLazyWrite(current->LazyWriteContext, Wait);
if (!Locked)
if (!CcpAcquireFileCacheForLazyWrite(current, Wait))
{
DPRINT("Not locked!");
ASSERT(!Wait);

OldIrql = KeAcquireQueuedSpinLock(LockQueueMasterLock);

current->Flags &= ~SHARED_CACHE_MAP_IN_LAZYWRITE;

/* Release the shared cache map */
if (--current->OpenCount == 0)
CcRosDeleteFileCache(current->FileObject, current, &OldIrql);

Expand All @@ -368,12 +392,11 @@ CcRosFlushDirtyPages (

CcpFlushFileCache(current, &FlushOffset, FlushLength, TRUE, &FlushIosb);

current->Callbacks->ReleaseFromLazyWrite(current->LazyWriteContext);
CcpReleaseFileCacheFromLazyWrite(current);

OldIrql = KeAcquireQueuedSpinLock(LockQueueMasterLock);

current->Flags &= ~SHARED_CACHE_MAP_IN_LAZYWRITE;

/* Release the shared cache map */
if (--current->OpenCount == 0)
CcRosDeleteFileCache(current->FileObject, current, &OldIrql);

Expand Down Expand Up @@ -403,10 +426,9 @@ CcRosFlushDirtyPages (
Target -= FlushedPages;
}
}

/* HACK? */
current_entry = CcDirtySharedCacheMapList.Flink;
}

current_entry = CcDirtySharedCacheMapList.Flink;
}

KeReleaseQueuedSpinLock(LockQueueMasterLock, OldIrql);
Expand Down

0 comments on commit 26d6592

Please sign in to comment.