2008-03-09 14:11:42 +00:00
|
|
|
/*
|
2001-10-10 21:49:15 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* FILE: ntoskrnl/cc/pin.c
|
|
|
|
* PURPOSE: Implements cache managers pinning interface
|
2005-01-26 13:58:37 +00:00
|
|
|
*
|
2016-05-26 12:09:05 +00:00
|
|
|
* PROGRAMMERS: ?
|
|
|
|
Pierre Schweitzer (pierre@reactos.org)
|
2001-10-10 21:49:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2001-10-10 21:49:15 +00:00
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
2001-10-10 21:49:15 +00:00
|
|
|
|
|
|
|
/* GLOBALS *******************************************************************/
|
|
|
|
|
2003-01-11 15:22:31 +00:00
|
|
|
extern NPAGED_LOOKASIDE_LIST iBcbLookasideList;
|
2001-10-10 21:49:15 +00:00
|
|
|
|
2018-02-24 12:35:17 +00:00
|
|
|
/* Counters:
|
|
|
|
* - Number of calls to CcMapData that could wait
|
|
|
|
* - Number of calls to CcMapData that couldn't wait
|
2018-02-24 12:47:15 +00:00
|
|
|
* - Number of calls to CcPinRead that could wait
|
|
|
|
* - Number of calls to CcPinRead that couldn't wait
|
2018-10-13 20:50:49 +00:00
|
|
|
* - Number of calls to CcPinMappedDataCount
|
2018-02-24 12:35:17 +00:00
|
|
|
*/
|
2018-02-24 12:28:45 +00:00
|
|
|
ULONG CcMapDataWait = 0;
|
|
|
|
ULONG CcMapDataNoWait = 0;
|
2018-02-24 12:47:15 +00:00
|
|
|
ULONG CcPinReadWait = 0;
|
|
|
|
ULONG CcPinReadNoWait = 0;
|
2018-10-13 20:50:49 +00:00
|
|
|
ULONG CcPinMappedDataCount = 0;
|
2018-02-24 12:28:45 +00:00
|
|
|
|
2003-01-11 15:22:31 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
2001-10-10 21:49:15 +00:00
|
|
|
|
2018-10-11 21:15:01 +00:00
|
|
|
static
|
|
|
|
PINTERNAL_BCB
|
|
|
|
NTAPI
|
|
|
|
CcpFindBcb(
|
|
|
|
IN PROS_SHARED_CACHE_MAP SharedCacheMap,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN BOOLEAN Pinned)
|
|
|
|
{
|
|
|
|
PINTERNAL_BCB Bcb;
|
|
|
|
BOOLEAN Found = FALSE;
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
|
|
|
|
for (NextEntry = SharedCacheMap->BcbList.Flink;
|
|
|
|
NextEntry != &SharedCacheMap->BcbList;
|
|
|
|
NextEntry = NextEntry->Flink)
|
|
|
|
{
|
|
|
|
Bcb = CONTAINING_RECORD(NextEntry, INTERNAL_BCB, BcbEntry);
|
|
|
|
|
|
|
|
if (Bcb->PFCB.MappedFileOffset.QuadPart <= FileOffset->QuadPart &&
|
|
|
|
(Bcb->PFCB.MappedFileOffset.QuadPart + Bcb->PFCB.MappedLength) >=
|
|
|
|
(FileOffset->QuadPart + Length))
|
|
|
|
{
|
|
|
|
if ((Pinned && Bcb->PinCount > 0) || (!Pinned && Bcb->PinCount == 0))
|
|
|
|
{
|
|
|
|
Found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Found ? Bcb : NULL);
|
|
|
|
}
|
|
|
|
|
2018-10-28 19:48:01 +00:00
|
|
|
static
|
|
|
|
VOID
|
|
|
|
CcpDereferenceBcb(
|
|
|
|
IN PROS_SHARED_CACHE_MAP SharedCacheMap,
|
|
|
|
IN PINTERNAL_BCB Bcb)
|
|
|
|
{
|
|
|
|
ULONG RefCount;
|
|
|
|
KIRQL OldIrql;
|
|
|
|
|
|
|
|
KeAcquireSpinLock(&SharedCacheMap->BcbSpinLock, &OldIrql);
|
|
|
|
RefCount = --Bcb->RefCount;
|
|
|
|
if (RefCount == 0)
|
|
|
|
{
|
|
|
|
RemoveEntryList(&Bcb->BcbEntry);
|
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
|
|
|
|
|
|
|
ASSERT(Bcb->PinCount == 0);
|
2019-04-17 20:23:57 +00:00
|
|
|
/*
|
|
|
|
* Don't mark dirty, if it was dirty,
|
|
|
|
* the VACB was already marked as such
|
|
|
|
* following the call to CcSetDirtyPinnedData
|
|
|
|
*/
|
2018-10-28 19:48:01 +00:00
|
|
|
CcRosReleaseVacb(SharedCacheMap,
|
|
|
|
Bcb->Vacb,
|
2019-04-17 20:23:57 +00:00
|
|
|
FALSE,
|
2018-10-28 19:48:01 +00:00
|
|
|
FALSE);
|
|
|
|
|
|
|
|
ExDeleteResourceLite(&Bcb->Lock);
|
|
|
|
ExFreeToNPagedLookasideList(&iBcbLookasideList, Bcb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 20:46:10 +00:00
|
|
|
static
|
|
|
|
PVOID
|
|
|
|
CcpGetAppropriateBcb(
|
|
|
|
IN PROS_SHARED_CACHE_MAP SharedCacheMap,
|
|
|
|
IN PROS_VACB Vacb,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG PinFlags,
|
|
|
|
IN BOOLEAN ToPin)
|
|
|
|
{
|
|
|
|
KIRQL OldIrql;
|
|
|
|
BOOLEAN Result;
|
|
|
|
PINTERNAL_BCB iBcb, DupBcb;
|
|
|
|
|
2010-12-28 14:49:53 +00:00
|
|
|
iBcb = ExAllocateFromNPagedLookasideList(&iBcbLookasideList);
|
|
|
|
if (iBcb == NULL)
|
2002-07-17 21:04:57 +00:00
|
|
|
{
|
2021-01-05 09:41:41 +00:00
|
|
|
CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE);
|
2018-10-13 20:46:10 +00:00
|
|
|
return NULL;
|
2002-07-17 21:04:57 +00:00
|
|
|
}
|
2012-12-19 11:11:34 +00:00
|
|
|
|
2013-09-30 20:06:18 +00:00
|
|
|
RtlZeroMemory(iBcb, sizeof(*iBcb));
|
2020-12-30 17:25:02 +00:00
|
|
|
iBcb->PFCB.NodeTypeCode = 0x2FD; /* As per KMTests */
|
|
|
|
iBcb->PFCB.NodeByteSize = 0;
|
2010-12-28 14:49:53 +00:00
|
|
|
iBcb->PFCB.MappedLength = Length;
|
|
|
|
iBcb->PFCB.MappedFileOffset = *FileOffset;
|
2014-04-12 09:31:07 +00:00
|
|
|
iBcb->Vacb = Vacb;
|
2018-10-05 17:43:10 +00:00
|
|
|
iBcb->PinCount = 0;
|
2010-12-28 14:49:53 +00:00
|
|
|
iBcb->RefCount = 1;
|
2016-05-26 12:09:05 +00:00
|
|
|
ExInitializeResourceLite(&iBcb->Lock);
|
2012-12-19 11:11:34 +00:00
|
|
|
|
2018-10-13 20:46:10 +00:00
|
|
|
KeAcquireSpinLock(&SharedCacheMap->BcbSpinLock, &OldIrql);
|
|
|
|
|
|
|
|
/* Check if we raced with another BCB creation */
|
|
|
|
DupBcb = CcpFindBcb(SharedCacheMap, FileOffset, Length, ToPin);
|
|
|
|
/* Yes, and we've lost */
|
|
|
|
if (DupBcb != NULL)
|
2018-10-05 19:01:02 +00:00
|
|
|
{
|
2018-10-28 19:48:01 +00:00
|
|
|
/* We will return that BCB */
|
|
|
|
++DupBcb->RefCount;
|
2018-10-13 20:46:10 +00:00
|
|
|
Result = TRUE;
|
2018-10-28 19:48:01 +00:00
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
2018-10-13 20:46:10 +00:00
|
|
|
|
|
|
|
if (ToPin)
|
|
|
|
{
|
|
|
|
if (BooleanFlagOn(PinFlags, PIN_EXCLUSIVE))
|
|
|
|
{
|
|
|
|
Result = ExAcquireResourceExclusiveLite(&iBcb->Lock, BooleanFlagOn(PinFlags, PIN_WAIT));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Result = ExAcquireSharedStarveExclusive(&iBcb->Lock, BooleanFlagOn(PinFlags, PIN_WAIT));
|
|
|
|
}
|
|
|
|
|
2018-10-28 19:48:01 +00:00
|
|
|
if (Result)
|
|
|
|
{
|
|
|
|
DupBcb->PinCount++;
|
|
|
|
}
|
|
|
|
else
|
2018-10-13 20:46:10 +00:00
|
|
|
{
|
2018-10-28 19:48:01 +00:00
|
|
|
CcpDereferenceBcb(SharedCacheMap, DupBcb);
|
2018-10-13 20:46:10 +00:00
|
|
|
DupBcb = NULL;
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 19:01:02 +00:00
|
|
|
|
2018-10-28 19:48:01 +00:00
|
|
|
if (DupBcb != NULL)
|
2018-10-05 19:01:02 +00:00
|
|
|
{
|
2018-10-28 19:48:01 +00:00
|
|
|
/* Delete the loser */
|
2021-01-05 09:41:41 +00:00
|
|
|
CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE);
|
2018-10-28 19:48:01 +00:00
|
|
|
ExDeleteResourceLite(&iBcb->Lock);
|
|
|
|
ExFreeToNPagedLookasideList(&iBcbLookasideList, iBcb);
|
2018-10-13 20:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the winner - no need to update buffer address, it's
|
|
|
|
* relative to the VACB, which is unchanged.
|
|
|
|
*/
|
|
|
|
iBcb = DupBcb;
|
|
|
|
}
|
|
|
|
/* Nope, insert ourselves */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ToPin)
|
|
|
|
{
|
|
|
|
iBcb->PinCount++;
|
|
|
|
|
|
|
|
if (BooleanFlagOn(PinFlags, PIN_EXCLUSIVE))
|
|
|
|
{
|
|
|
|
Result = ExAcquireResourceExclusiveLite(&iBcb->Lock, BooleanFlagOn(PinFlags, PIN_WAIT));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Result = ExAcquireSharedStarveExclusive(&iBcb->Lock, BooleanFlagOn(PinFlags, PIN_WAIT));
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertTailList(&SharedCacheMap->BcbList, &iBcb->BcbEntry);
|
2018-10-28 19:48:01 +00:00
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
2018-10-13 20:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return iBcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
BOOLEAN
|
|
|
|
CcpPinData(
|
|
|
|
IN PROS_SHARED_CACHE_MAP SharedCacheMap,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG Flags,
|
|
|
|
OUT PVOID * Bcb,
|
|
|
|
OUT PVOID * Buffer)
|
|
|
|
{
|
|
|
|
PINTERNAL_BCB NewBcb;
|
|
|
|
KIRQL OldIrql;
|
2020-11-06 08:39:31 +00:00
|
|
|
ULONG VacbOffset;
|
|
|
|
NTSTATUS Status;
|
2022-12-27 15:43:10 +00:00
|
|
|
_SEH2_VOLATILE BOOLEAN Result;
|
2020-11-06 08:39:31 +00:00
|
|
|
|
|
|
|
VacbOffset = (ULONG)(FileOffset->QuadPart % VACB_MAPPING_GRANULARITY);
|
2020-12-18 14:24:09 +00:00
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
if ((VacbOffset + Length) > VACB_MAPPING_GRANULARITY)
|
2020-12-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
/* Complain loudly, we shoud pin the whole range */
|
|
|
|
DPRINT1("TRUNCATING DATA PIN FROM %lu to %lu!\n", Length, VACB_MAPPING_GRANULARITY - VacbOffset);
|
2020-11-06 08:39:31 +00:00
|
|
|
Length = VACB_MAPPING_GRANULARITY - VacbOffset;
|
2020-12-18 14:24:09 +00:00
|
|
|
}
|
2018-10-13 20:46:10 +00:00
|
|
|
|
|
|
|
KeAcquireSpinLock(&SharedCacheMap->BcbSpinLock, &OldIrql);
|
|
|
|
NewBcb = CcpFindBcb(SharedCacheMap, FileOffset, Length, TRUE);
|
2018-10-11 21:15:01 +00:00
|
|
|
|
2018-10-13 20:46:10 +00:00
|
|
|
if (NewBcb != NULL)
|
|
|
|
{
|
2020-11-06 08:39:31 +00:00
|
|
|
BOOLEAN Result;
|
|
|
|
|
2018-10-28 19:48:01 +00:00
|
|
|
++NewBcb->RefCount;
|
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
2018-10-13 20:46:10 +00:00
|
|
|
|
|
|
|
if (BooleanFlagOn(Flags, PIN_EXCLUSIVE))
|
|
|
|
Result = ExAcquireResourceExclusiveLite(&NewBcb->Lock, BooleanFlagOn(Flags, PIN_WAIT));
|
|
|
|
else
|
|
|
|
Result = ExAcquireSharedStarveExclusive(&NewBcb->Lock, BooleanFlagOn(Flags, PIN_WAIT));
|
2018-10-11 21:15:01 +00:00
|
|
|
|
2018-10-13 20:46:10 +00:00
|
|
|
if (!Result)
|
|
|
|
{
|
2018-10-28 19:48:01 +00:00
|
|
|
CcpDereferenceBcb(SharedCacheMap, NewBcb);
|
2020-11-06 08:39:31 +00:00
|
|
|
return FALSE;
|
2018-10-05 19:01:02 +00:00
|
|
|
}
|
2018-10-13 20:46:10 +00:00
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
NewBcb->PinCount++;
|
2018-10-11 21:15:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-06 08:39:31 +00:00
|
|
|
LONGLONG ROffset;
|
|
|
|
PROS_VACB Vacb;
|
|
|
|
|
2018-10-28 19:48:01 +00:00
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
|
|
|
|
2018-10-13 20:46:10 +00:00
|
|
|
if (BooleanFlagOn(Flags, PIN_IF_BCB))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
/* Properly round offset and call internal helper for getting a VACB */
|
|
|
|
ROffset = ROUND_DOWN(FileOffset->QuadPart, VACB_MAPPING_GRANULARITY);
|
|
|
|
Status = CcRosGetVacb(SharedCacheMap, ROffset, &Vacb);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2018-10-23 19:48:01 +00:00
|
|
|
{
|
2020-11-06 08:39:31 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "FileObject=%p FileOffset=%p Length=%lu Flags=0x%lx -> FALSE\n",
|
|
|
|
SharedCacheMap->FileObject, FileOffset, Length, Flags);
|
|
|
|
ExRaiseStatus(Status);
|
|
|
|
return FALSE;
|
2018-10-23 19:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
NewBcb = CcpGetAppropriateBcb(SharedCacheMap, Vacb, FileOffset, Length, Flags, TRUE);
|
|
|
|
if (NewBcb == NULL)
|
2018-10-13 20:46:10 +00:00
|
|
|
{
|
2021-01-05 09:41:41 +00:00
|
|
|
CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE);
|
2020-11-06 08:39:31 +00:00
|
|
|
return FALSE;
|
2018-10-13 20:46:10 +00:00
|
|
|
}
|
2018-10-05 19:01:02 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
Result = FALSE;
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Ensure the pages are resident */
|
|
|
|
Result = CcRosEnsureVacbResident(NewBcb->Vacb,
|
|
|
|
BooleanFlagOn(Flags, PIN_WAIT),
|
|
|
|
BooleanFlagOn(Flags, PIN_NO_READ),
|
|
|
|
VacbOffset, Length);
|
|
|
|
}
|
|
|
|
_SEH2_FINALLY
|
|
|
|
{
|
|
|
|
if (!Result)
|
|
|
|
{
|
|
|
|
CCTRACE(CC_API_DEBUG, "FileObject=%p FileOffset=%p Length=%lu Flags=0x%lx -> FALSE\n",
|
|
|
|
SharedCacheMap->FileObject, FileOffset, Length, Flags);
|
2021-01-28 14:41:28 +00:00
|
|
|
CcUnpinData(&NewBcb->PFCB);
|
2022-12-27 15:43:10 +00:00
|
|
|
*Bcb = NULL;
|
|
|
|
*Buffer = NULL;
|
2020-11-06 08:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
2022-12-27 15:43:10 +00:00
|
|
|
if (Result)
|
|
|
|
{
|
|
|
|
*Bcb = &NewBcb->PFCB;
|
|
|
|
*Buffer = (PVOID)((ULONG_PTR)NewBcb->Vacb->BaseAddress + VacbOffset);
|
|
|
|
}
|
2020-12-30 17:25:02 +00:00
|
|
|
|
2022-12-27 15:43:10 +00:00
|
|
|
return Result;
|
2018-10-05 19:01:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 09:14:42 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
CcMapData (
|
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG Flags,
|
|
|
|
OUT PVOID *pBcb,
|
|
|
|
OUT PVOID *pBuffer)
|
|
|
|
{
|
2018-10-05 19:01:02 +00:00
|
|
|
KIRQL OldIrql;
|
|
|
|
PINTERNAL_BCB iBcb;
|
2018-10-13 20:46:10 +00:00
|
|
|
PROS_VACB Vacb;
|
2018-10-05 19:01:02 +00:00
|
|
|
PROS_SHARED_CACHE_MAP SharedCacheMap;
|
2020-11-06 08:39:31 +00:00
|
|
|
ULONG VacbOffset;
|
|
|
|
NTSTATUS Status;
|
2022-12-27 15:43:10 +00:00
|
|
|
_SEH2_VOLATILE BOOLEAN Result;
|
2018-09-09 09:14:42 +00:00
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "CcMapData(FileObject 0x%p, FileOffset 0x%I64x, Length %lu, Flags 0x%lx,"
|
2018-09-09 09:14:42 +00:00
|
|
|
" pBcb 0x%p, pBuffer 0x%p)\n", FileObject, FileOffset->QuadPart,
|
|
|
|
Length, Flags, pBcb, pBuffer);
|
|
|
|
|
2018-10-05 19:01:02 +00:00
|
|
|
ASSERT(FileObject);
|
|
|
|
ASSERT(FileObject->SectionObjectPointer);
|
|
|
|
ASSERT(FileObject->SectionObjectPointer->SharedCacheMap);
|
|
|
|
|
|
|
|
SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
|
|
|
|
ASSERT(SharedCacheMap);
|
|
|
|
|
2018-09-09 09:14:42 +00:00
|
|
|
if (Flags & MAP_WAIT)
|
|
|
|
{
|
|
|
|
++CcMapDataWait;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++CcMapDataNoWait;
|
|
|
|
}
|
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
VacbOffset = (ULONG)(FileOffset->QuadPart % VACB_MAPPING_GRANULARITY);
|
|
|
|
/* KMTests seem to show that it is allowed to call accross mapping granularity */
|
|
|
|
if ((VacbOffset + Length) > VACB_MAPPING_GRANULARITY)
|
2020-12-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
DPRINT1("TRUNCATING DATA MAP FROM %lu to %lu!\n", Length, VACB_MAPPING_GRANULARITY - VacbOffset);
|
2020-11-06 08:39:31 +00:00
|
|
|
Length = VACB_MAPPING_GRANULARITY - VacbOffset;
|
2020-12-18 14:24:09 +00:00
|
|
|
}
|
2020-11-06 08:39:31 +00:00
|
|
|
|
2018-10-05 19:01:02 +00:00
|
|
|
KeAcquireSpinLock(&SharedCacheMap->BcbSpinLock, &OldIrql);
|
|
|
|
iBcb = CcpFindBcb(SharedCacheMap, FileOffset, Length, FALSE);
|
|
|
|
|
|
|
|
if (iBcb == NULL)
|
|
|
|
{
|
2018-10-28 19:48:01 +00:00
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
/* Call internal helper for getting a VACB */
|
|
|
|
Status = CcRosGetVacb(SharedCacheMap, FileOffset->QuadPart, &Vacb);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2018-10-13 20:46:10 +00:00
|
|
|
{
|
2020-11-06 08:39:31 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "FileObject=%p FileOffset=%p Length=%lu Flags=0x%lx -> FALSE\n",
|
|
|
|
SharedCacheMap->FileObject, FileOffset, Length, Flags);
|
|
|
|
ExRaiseStatus(Status);
|
|
|
|
}
|
|
|
|
|
|
|
|
iBcb = CcpGetAppropriateBcb(SharedCacheMap, Vacb, FileOffset, Length, 0, FALSE);
|
|
|
|
if (iBcb == NULL)
|
|
|
|
{
|
2021-01-05 09:41:41 +00:00
|
|
|
CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE);
|
2020-11-06 08:39:31 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "FileObject=%p FileOffset=%p Length=%lu Flags=0x%lx -> FALSE\n",
|
|
|
|
SharedCacheMap->FileObject, FileOffset, Length, Flags);
|
2021-02-09 08:29:48 +00:00
|
|
|
*pBcb = NULL; // If you ever remove this for compat, make sure to review all callers for using an unititialized value
|
|
|
|
ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES);
|
2018-10-13 20:46:10 +00:00
|
|
|
}
|
2018-10-05 19:01:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iBcb->RefCount;
|
2018-10-28 19:48:01 +00:00
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
2020-11-06 08:39:31 +00:00
|
|
|
}
|
2018-10-28 19:48:01 +00:00
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
Result = FALSE;
|
|
|
|
/* Ensure the pages are resident */
|
|
|
|
Result = CcRosEnsureVacbResident(iBcb->Vacb, BooleanFlagOn(Flags, MAP_WAIT),
|
|
|
|
BooleanFlagOn(Flags, MAP_NO_READ), VacbOffset, Length);
|
|
|
|
}
|
|
|
|
_SEH2_FINALLY
|
|
|
|
{
|
|
|
|
if (!Result)
|
|
|
|
{
|
|
|
|
CcpDereferenceBcb(SharedCacheMap, iBcb);
|
2022-12-27 15:43:10 +00:00
|
|
|
*pBcb = NULL;
|
|
|
|
*pBuffer = NULL;
|
2020-11-06 08:39:31 +00:00
|
|
|
}
|
2018-10-05 19:01:02 +00:00
|
|
|
}
|
2020-11-06 08:39:31 +00:00
|
|
|
_SEH2_END;
|
|
|
|
|
2022-12-27 15:43:10 +00:00
|
|
|
if (Result)
|
|
|
|
{
|
|
|
|
*pBcb = &iBcb->PFCB;
|
|
|
|
*pBuffer = (PVOID)((ULONG_PTR)iBcb->Vacb->BaseAddress + VacbOffset);
|
|
|
|
}
|
2018-09-09 09:14:42 +00:00
|
|
|
|
2020-11-06 08:39:31 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "FileObject=%p FileOffset=%p Length=%lu Flags=0x%lx -> TRUE Bcb=%p, Buffer %p\n",
|
|
|
|
FileObject, FileOffset, Length, Flags, *pBcb, *pBuffer);
|
|
|
|
return Result;
|
2018-09-09 09:14:42 +00:00
|
|
|
}
|
|
|
|
|
2003-07-10 06:27:13 +00:00
|
|
|
/*
|
2004-06-21 04:11:44 +00:00
|
|
|
* @unimplemented
|
2003-07-10 06:27:13 +00:00
|
|
|
*/
|
2004-06-21 04:11:44 +00:00
|
|
|
BOOLEAN
|
2008-11-29 20:47:48 +00:00
|
|
|
NTAPI
|
2004-06-21 04:11:44 +00:00
|
|
|
CcPinMappedData (
|
2012-12-19 11:11:34 +00:00
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG Flags,
|
|
|
|
OUT PVOID * Bcb)
|
2001-10-10 21:49:15 +00:00
|
|
|
{
|
2018-08-26 20:04:46 +00:00
|
|
|
BOOLEAN Result;
|
2018-10-13 20:46:10 +00:00
|
|
|
PVOID Buffer;
|
2018-08-26 19:49:05 +00:00
|
|
|
PINTERNAL_BCB iBcb;
|
2016-05-26 11:50:42 +00:00
|
|
|
PROS_SHARED_CACHE_MAP SharedCacheMap;
|
|
|
|
|
2018-08-31 17:48:03 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "FileObject=%p FileOffset=%p Length=%lu Flags=0x%lx\n",
|
2014-11-29 21:43:39 +00:00
|
|
|
FileObject, FileOffset, Length, Flags);
|
|
|
|
|
2016-05-26 11:50:42 +00:00
|
|
|
ASSERT(FileObject);
|
|
|
|
ASSERT(FileObject->SectionObjectPointer);
|
|
|
|
ASSERT(FileObject->SectionObjectPointer->SharedCacheMap);
|
|
|
|
|
|
|
|
SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
|
|
|
|
ASSERT(SharedCacheMap);
|
2018-09-01 10:39:05 +00:00
|
|
|
if (!SharedCacheMap->PinAccess)
|
|
|
|
{
|
|
|
|
DPRINT1("FIXME: Pinning a file with no pin access!\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-05-26 11:50:42 +00:00
|
|
|
|
2020-12-30 17:25:02 +00:00
|
|
|
iBcb = *Bcb ? CONTAINING_RECORD(*Bcb, INTERNAL_BCB, PFCB) : NULL;
|
2018-08-26 19:49:05 +00:00
|
|
|
|
2018-10-13 20:50:49 +00:00
|
|
|
++CcPinMappedDataCount;
|
|
|
|
|
2018-10-13 20:46:10 +00:00
|
|
|
Result = CcpPinData(SharedCacheMap, FileOffset, Length, Flags, Bcb, &Buffer);
|
|
|
|
if (Result)
|
2018-08-26 20:56:25 +00:00
|
|
|
{
|
2020-12-30 17:25:02 +00:00
|
|
|
CcUnpinData(&iBcb->PFCB);
|
2018-08-26 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 20:04:46 +00:00
|
|
|
return Result;
|
2004-06-21 04:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @unimplemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
2008-11-29 20:47:48 +00:00
|
|
|
NTAPI
|
2004-06-21 04:11:44 +00:00
|
|
|
CcPinRead (
|
2012-12-19 11:11:34 +00:00
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG Flags,
|
|
|
|
OUT PVOID * Bcb,
|
|
|
|
OUT PVOID * Buffer)
|
2004-06-21 04:11:44 +00:00
|
|
|
{
|
2018-10-05 19:01:02 +00:00
|
|
|
PROS_SHARED_CACHE_MAP SharedCacheMap;
|
|
|
|
|
2014-11-29 21:43:39 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "FileOffset=%p FileOffset=%p Length=%lu Flags=0x%lx\n",
|
|
|
|
FileObject, FileOffset, Length, Flags);
|
|
|
|
|
2018-10-05 19:01:02 +00:00
|
|
|
ASSERT(FileObject);
|
|
|
|
ASSERT(FileObject->SectionObjectPointer);
|
|
|
|
ASSERT(FileObject->SectionObjectPointer->SharedCacheMap);
|
|
|
|
|
|
|
|
SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
|
|
|
|
ASSERT(SharedCacheMap);
|
2018-10-13 20:46:10 +00:00
|
|
|
if (!SharedCacheMap->PinAccess)
|
|
|
|
{
|
|
|
|
DPRINT1("FIXME: Pinning a file with no pin access!\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-10-05 19:01:02 +00:00
|
|
|
|
2018-02-24 12:47:15 +00:00
|
|
|
if (Flags & PIN_WAIT)
|
|
|
|
{
|
|
|
|
++CcPinReadWait;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++CcPinReadNoWait;
|
|
|
|
}
|
|
|
|
|
2018-10-13 20:46:10 +00:00
|
|
|
return CcpPinData(SharedCacheMap, FileOffset, Length, Flags, Bcb, Buffer);
|
2004-06-21 04:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @unimplemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
2008-11-29 20:47:48 +00:00
|
|
|
NTAPI
|
2004-06-21 04:11:44 +00:00
|
|
|
CcPreparePinWrite (
|
2012-12-19 11:11:34 +00:00
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN BOOLEAN Zero,
|
|
|
|
IN ULONG Flags,
|
|
|
|
OUT PVOID * Bcb,
|
|
|
|
OUT PVOID * Buffer)
|
2004-06-21 04:11:44 +00:00
|
|
|
{
|
2014-11-29 21:43:39 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "FileOffset=%p FileOffset=%p Length=%lu Zero=%d Flags=0x%lx\n",
|
|
|
|
FileObject, FileOffset, Length, Zero, Flags);
|
|
|
|
|
2012-12-19 11:11:34 +00:00
|
|
|
/*
|
|
|
|
* FIXME: This is function is similar to CcPinRead, but doesn't
|
|
|
|
* read the data if they're not present. Instead it should just
|
2014-11-15 22:47:51 +00:00
|
|
|
* prepare the VACBs and zero them out if Zero != FALSE.
|
2012-12-19 11:11:34 +00:00
|
|
|
*
|
|
|
|
* For now calling CcPinRead is better than returning error or
|
|
|
|
* just having UNIMPLEMENTED here.
|
|
|
|
*/
|
|
|
|
return CcPinRead(FileObject, FileOffset, Length, Flags, Bcb, Buffer);
|
2001-10-10 21:49:15 +00:00
|
|
|
}
|
|
|
|
|
2003-07-10 06:27:13 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2008-11-29 20:47:48 +00:00
|
|
|
VOID NTAPI
|
2012-12-19 11:11:34 +00:00
|
|
|
CcSetDirtyPinnedData (
|
|
|
|
IN PVOID Bcb,
|
|
|
|
IN PLARGE_INTEGER Lsn)
|
2001-10-10 21:49:15 +00:00
|
|
|
{
|
2020-12-30 17:25:02 +00:00
|
|
|
PINTERNAL_BCB iBcb = CONTAINING_RECORD(Bcb, INTERNAL_BCB, PFCB);
|
2024-11-24 14:37:27 +00:00
|
|
|
PROS_VACB Vacb = iBcb->Vacb;
|
2014-11-29 21:43:39 +00:00
|
|
|
|
2020-12-30 17:25:02 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "Bcb=%p Lsn=%p\n", Bcb, Lsn);
|
2014-11-29 21:43:39 +00:00
|
|
|
|
2020-12-02 11:30:48 +00:00
|
|
|
/* Tell Mm */
|
2024-11-24 14:37:27 +00:00
|
|
|
MmMakeSegmentDirty(Vacb->SharedCacheMap->FileObject->SectionObjectPointer,
|
|
|
|
iBcb->PFCB.MappedFileOffset.QuadPart,
|
|
|
|
iBcb->PFCB.MappedLength);
|
2020-12-02 11:30:48 +00:00
|
|
|
|
2024-11-24 14:37:27 +00:00
|
|
|
if (!Vacb->Dirty)
|
2018-04-29 18:34:28 +00:00
|
|
|
{
|
2024-11-24 14:37:27 +00:00
|
|
|
CcRosMarkDirtyVacb(Vacb);
|
2018-04-29 18:34:28 +00:00
|
|
|
}
|
2001-10-10 21:49:15 +00:00
|
|
|
}
|
|
|
|
|
2004-06-21 04:11:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2008-11-29 20:47:48 +00:00
|
|
|
VOID NTAPI
|
2012-12-19 11:11:34 +00:00
|
|
|
CcUnpinData (
|
|
|
|
IN PVOID Bcb)
|
2004-06-21 04:11:44 +00:00
|
|
|
{
|
2014-11-29 21:43:39 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "Bcb=%p\n", Bcb);
|
|
|
|
|
2016-05-26 12:09:05 +00:00
|
|
|
CcUnpinDataForThread(Bcb, (ERESOURCE_THREAD)PsGetCurrentThread());
|
2004-06-21 04:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @unimplemented
|
|
|
|
*/
|
|
|
|
VOID
|
2008-11-29 20:47:48 +00:00
|
|
|
NTAPI
|
2004-06-21 04:11:44 +00:00
|
|
|
CcUnpinDataForThread (
|
2012-12-19 11:11:34 +00:00
|
|
|
IN PVOID Bcb,
|
|
|
|
IN ERESOURCE_THREAD ResourceThreadId)
|
2004-06-21 04:11:44 +00:00
|
|
|
{
|
2020-12-30 17:25:02 +00:00
|
|
|
PINTERNAL_BCB iBcb = CONTAINING_RECORD(Bcb, INTERNAL_BCB, PFCB);
|
2016-03-20 18:33:54 +00:00
|
|
|
|
2014-11-29 21:43:39 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "Bcb=%p ResourceThreadId=%lu\n", Bcb, ResourceThreadId);
|
|
|
|
|
2018-10-05 17:43:10 +00:00
|
|
|
if (iBcb->PinCount != 0)
|
2016-03-20 18:33:54 +00:00
|
|
|
{
|
2016-05-26 12:09:05 +00:00
|
|
|
ExReleaseResourceForThreadLite(&iBcb->Lock, ResourceThreadId);
|
2018-10-05 17:43:10 +00:00
|
|
|
iBcb->PinCount--;
|
2016-03-20 18:33:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 15:55:19 +00:00
|
|
|
CcpDereferenceBcb(iBcb->Vacb->SharedCacheMap, iBcb);
|
2004-06-21 04:11:44 +00:00
|
|
|
}
|
2004-08-25 15:08:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
2008-11-29 20:47:48 +00:00
|
|
|
NTAPI
|
2004-08-25 15:08:29 +00:00
|
|
|
CcRepinBcb (
|
2012-12-19 11:11:34 +00:00
|
|
|
IN PVOID Bcb)
|
2004-08-25 15:08:29 +00:00
|
|
|
{
|
2020-12-30 17:25:02 +00:00
|
|
|
PINTERNAL_BCB iBcb = CONTAINING_RECORD(Bcb, INTERNAL_BCB, PFCB);
|
2014-11-29 21:43:39 +00:00
|
|
|
|
|
|
|
CCTRACE(CC_API_DEBUG, "Bcb=%p\n", Bcb);
|
|
|
|
|
2012-12-19 11:11:34 +00:00
|
|
|
iBcb->RefCount++;
|
2004-08-25 15:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @unimplemented
|
|
|
|
*/
|
|
|
|
VOID
|
2008-11-29 20:47:48 +00:00
|
|
|
NTAPI
|
2004-08-25 15:08:29 +00:00
|
|
|
CcUnpinRepinnedBcb (
|
2012-12-19 11:11:34 +00:00
|
|
|
IN PVOID Bcb,
|
|
|
|
IN BOOLEAN WriteThrough,
|
|
|
|
IN PIO_STATUS_BLOCK IoStatus)
|
2004-08-25 15:08:29 +00:00
|
|
|
{
|
2020-12-30 17:25:02 +00:00
|
|
|
PINTERNAL_BCB iBcb = CONTAINING_RECORD(Bcb, INTERNAL_BCB, PFCB);
|
2018-09-02 12:37:47 +00:00
|
|
|
KIRQL OldIrql;
|
|
|
|
PROS_SHARED_CACHE_MAP SharedCacheMap;
|
2004-08-25 15:08:29 +00:00
|
|
|
|
2014-11-29 21:43:39 +00:00
|
|
|
CCTRACE(CC_API_DEBUG, "Bcb=%p WriteThrough=%d\n", Bcb, WriteThrough);
|
|
|
|
|
2018-10-28 19:48:01 +00:00
|
|
|
SharedCacheMap = iBcb->Vacb->SharedCacheMap;
|
2012-12-19 23:49:13 +00:00
|
|
|
IoStatus->Status = STATUS_SUCCESS;
|
2018-10-28 19:48:01 +00:00
|
|
|
|
2020-12-29 09:07:57 +00:00
|
|
|
if (WriteThrough)
|
|
|
|
{
|
|
|
|
CcFlushCache(iBcb->Vacb->SharedCacheMap->FileObject->SectionObjectPointer,
|
|
|
|
&iBcb->PFCB.MappedFileOffset,
|
|
|
|
iBcb->PFCB.MappedLength,
|
|
|
|
IoStatus);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IoStatus->Status = STATUS_SUCCESS;
|
|
|
|
IoStatus->Information = 0;
|
|
|
|
}
|
|
|
|
|
2018-10-28 19:48:01 +00:00
|
|
|
KeAcquireSpinLock(&SharedCacheMap->BcbSpinLock, &OldIrql);
|
2012-12-19 11:11:34 +00:00
|
|
|
if (--iBcb->RefCount == 0)
|
2004-08-25 15:08:29 +00:00
|
|
|
{
|
2018-10-28 19:48:01 +00:00
|
|
|
RemoveEntryList(&iBcb->BcbEntry);
|
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
|
|
|
|
2018-10-05 17:43:10 +00:00
|
|
|
if (iBcb->PinCount != 0)
|
2016-05-26 12:09:05 +00:00
|
|
|
{
|
|
|
|
ExReleaseResourceLite(&iBcb->Lock);
|
2018-10-05 17:43:10 +00:00
|
|
|
iBcb->PinCount--;
|
|
|
|
ASSERT(iBcb->PinCount == 0);
|
2016-05-26 12:09:05 +00:00
|
|
|
}
|
2018-03-17 10:56:25 +00:00
|
|
|
|
2019-04-17 20:23:57 +00:00
|
|
|
/*
|
|
|
|
* Don't mark dirty, if it was dirty,
|
|
|
|
* the VACB was already marked as such
|
|
|
|
* following the call to CcSetDirtyPinnedData
|
|
|
|
*/
|
2018-03-17 10:56:25 +00:00
|
|
|
CcRosReleaseVacb(iBcb->Vacb->SharedCacheMap,
|
|
|
|
iBcb->Vacb,
|
2019-04-17 20:23:57 +00:00
|
|
|
FALSE,
|
2018-03-17 10:56:25 +00:00
|
|
|
FALSE);
|
|
|
|
|
2016-05-26 12:09:05 +00:00
|
|
|
ExDeleteResourceLite(&iBcb->Lock);
|
2012-12-19 11:11:34 +00:00
|
|
|
ExFreeToNPagedLookasideList(&iBcbLookasideList, iBcb);
|
2004-08-25 15:08:29 +00:00
|
|
|
}
|
2018-10-28 19:48:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
KeReleaseSpinLock(&SharedCacheMap->BcbSpinLock, OldIrql);
|
|
|
|
}
|
2004-08-25 15:08:29 +00:00
|
|
|
}
|