-/*
- * ReactOS kernel
- * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that 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, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-/* $Id: view.c,v 1.74 2004/08/01 21:57:34 navaraf Exp $
+/* $Id$
*
+ * COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* FILE: ntoskrnl/cc/view.c
* PURPOSE: Cache manager
- * PROGRAMMER: David Welch (welch@mcmail.com)
- * PORTABILITY: Checked
- * UPDATE HISTORY:
- * Created 22/05/98
+ *
+ * PROGRAMMERS: David Welch (welch@mcmail.com)
*/
/* NOTES **********************************************************************
*
- * This is not the NT implementation of a file cache nor anything much like
- * it.
+ * This is not the NT implementation of a file cache nor anything much like
+ * it.
*
- * The general procedure for a filesystem to implement a read or write
+ * The general procedure for a filesystem to implement a read or write
* dispatch routine is as follows
- *
+ *
* (1) If caching for the FCB hasn't been initiated then so do by calling
* CcInitializeFileCache.
- *
+ *
* (2) For each 4k region which is being read or written obtain a cache page
- * by calling CcRequestCachePage.
+ * by calling CcRequestCachePage.
*
- * (3) If either the page is being read or not completely written, and it is
+ * (3) If either the page is being read or not completely written, and it is
* not up to date then read its data from the underlying medium. If the read
- * fails then call CcReleaseCachePage with VALID as FALSE and return a error.
- *
+ * fails then call CcReleaseCachePage with VALID as FALSE and return a error.
+ *
* (4) Copy the data into or out of the page as necessary.
- *
+ *
* (5) Release the cache page
*/
/* INCLUDES ******************************************************************/
-#include <ddk/ntddk.h>
-#include <ddk/ntifs.h>
-#include <internal/mm.h>
-#include <internal/cc.h>
-#include <internal/pool.h>
-#include <ntos/minmax.h>
-
+#include <ntoskrnl.h>
#define NDEBUG
#include <internal/debug.h>
/* GLOBALS *******************************************************************/
/*
- * If CACHE_BITMAP is defined, the cache manager uses one large memory region
- * within the kernel address space and allocate/deallocate space from this block
- * over a bitmap. If CACHE_BITMAP is used, the size of the mdl mapping region
+ * If CACHE_BITMAP is defined, the cache manager uses one large memory region
+ * within the kernel address space and allocate/deallocate space from this block
+ * over a bitmap. If CACHE_BITMAP is used, the size of the mdl mapping region
* must be reduced (ntoskrnl\mm\mdl.c, MI_MDLMAPPING_REGION_SIZE).
*/
//#define CACHE_BITMAP
#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
#define ROUND_DOWN(N, S) (((N) % (S)) ? ROUND_UP(N, S) - S : N)
-#define TAG_CSEG TAG('C', 'S', 'E', 'G')
-#define TAG_BCB TAG('B', 'C', 'B', ' ')
-#define TAG_IBCB TAG('i', 'B', 'C', 'B')
-
static LIST_ENTRY DirtySegmentListHead;
static LIST_ENTRY CacheSegmentListHead;
static LIST_ENTRY CacheSegmentLRUListHead;
static volatile BOOLEAN LazyCloseThreadShouldTerminate;
#if defined(__GNUC__)
-void * alloca(size_t size);
+/* void * alloca(size_t size); */
#elif defined(_MSC_VER)
void* _alloca(size_t size);
#else
/* FUNCTIONS *****************************************************************/
-NTSTATUS STATIC
+NTSTATUS
CcRosFlushCacheSegment(PCACHE_SEGMENT CacheSegment)
{
NTSTATUS Status;
}
NewTarget = WriteCount[0];
-
+
Target = max(NewTarget, Target);
current_entry = DirtySegmentListHead.Flink;
}
while (current_entry != &DirtySegmentListHead && Target > 0)
{
- current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
+ current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
DirtySegmentListEntry);
current_entry = current_entry->Flink;
Locked = ExTryToAcquireFastMutex(¤t->Lock);
{
continue;
}
- assert(current->Dirty);
+ ASSERT(current->Dirty);
if (current->ReferenceCount > 1)
{
ExReleaseFastMutex(¤t->Lock);
}
ExReleaseFastMutex(&ViewLock);
PagesPerSegment = current->Bcb->CacheSegmentSize / PAGE_SIZE;
- Status = CcRosFlushCacheSegment(current);
+ Status = CcRosFlushCacheSegment(current);
ExReleaseFastMutex(¤t->Lock);
if (!NT_SUCCESS(Status) && (Status != STATUS_END_OF_FILE))
{
else
{
(*Count) += PagesPerSegment;
- Target -= PagesPerSegment;
+ Target -= PagesPerSegment;
}
ExAcquireFastMutex(&ViewLock);
current_entry = DirtySegmentListHead.Flink;
* ARGUMENTS:
* Target - The number of pages to be freed.
* Priority - The priority of free (currently unused).
- * NrFreed - Points to a variable where the number of pages
+ * NrFreed - Points to a variable where the number of pages
* actually freed is returned.
*/
{
DPRINT("CcRosTrimCache(Target %d)\n", Target);
*NrFreed = 0;
-
+
InitializeListHead(&FreeList);
-
+
ExAcquireFastMutex(&ViewLock);
current_entry = CacheSegmentLRUListHead.Flink;
while (current_entry != &CacheSegmentLRUListHead && Target > 0)
{
- current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
+ current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
CacheSegmentLRUListEntry);
current_entry = current_entry->Flink;
-
+
KeAcquireSpinLock(¤t->Bcb->BcbLock, &oldIrql);
if (current->ReferenceCount == 0)
{
{
ULONG i;
NTSTATUS Status;
-
+
current->ReferenceCount++;
last = current;
current->PageOut = TRUE;
while (!IsListEmpty(&FreeList))
{
current_entry = RemoveHeadList(&FreeList);
- current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
+ current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
BcbSegmentListEntry);
CcRosInternalFreeCacheSegment(current);
}
return(STATUS_SUCCESS);
}
-NTSTATUS
+NTSTATUS
CcRosReleaseCacheSegment(PBCB Bcb,
PCACHE_SEGMENT CacheSeg,
BOOLEAN Valid,
BOOLEAN WasDirty = CacheSeg->Dirty;
KIRQL oldIrql;
- assert(Bcb);
+ ASSERT(Bcb);
DPRINT("CcReleaseCacheSegment(Bcb %x, CacheSeg %x, Valid %d)\n",
Bcb, CacheSeg, Valid);
KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
ExReleaseFastMutex(&ViewLock);
ExReleaseFastMutex(&CacheSeg->Lock);
-
+
return(STATUS_SUCCESS);
}
-PCACHE_SEGMENT
+PCACHE_SEGMENT
CcRosLookupCacheSegment(PBCB Bcb, ULONG FileOffset)
{
PLIST_ENTRY current_entry;
PCACHE_SEGMENT current;
KIRQL oldIrql;
- assert(Bcb);
+ ASSERT(Bcb);
DPRINT("CcRosLookupCacheSegment(Bcb %x, FileOffset %d)\n", Bcb, FileOffset);
current_entry = Bcb->BcbSegmentListHead.Flink;
while (current_entry != &Bcb->BcbSegmentListHead)
{
- current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
+ current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
BcbSegmentListEntry);
if (current->FileOffset <= FileOffset &&
(current->FileOffset + Bcb->CacheSegmentSize) > FileOffset)
PCACHE_SEGMENT CacheSeg;
KIRQL oldIrql;
- assert(Bcb);
+ ASSERT(Bcb);
DPRINT("CcRosMarkDirtyCacheSegment(Bcb %x, FileOffset %d)\n", Bcb, FileOffset);
BOOLEAN WasDirty;
KIRQL oldIrql;
- assert(Bcb);
+ ASSERT(Bcb);
DPRINT("CcRosUnmapCacheSegment(Bcb %x, FileOffset %d, NowDirty %d)\n",
Bcb, FileOffset, NowDirty);
#else
#endif
PHYSICAL_ADDRESS BoundaryAddressMultiple;
-
- assert(Bcb);
+
+ ASSERT(Bcb);
DPRINT("CcRosCreateCacheSegment()\n");
/* There is window between the call to CcRosLookupCacheSegment
* and CcRosCreateCacheSegment. We must check if a segment on
* the fileoffset exist. If there exist a segment, we release
- * our new created segment and return the existing one.
+ * our new created segment and return the existing one.
*/
KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
current_entry = Bcb->BcbSegmentListHead.Flink;
previous = NULL;
while (current_entry != &Bcb->BcbSegmentListHead)
{
- current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
+ current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
BcbSegmentListEntry);
if (current->FileOffset <= FileOffset &&
(current->FileOffset + Bcb->CacheSegmentSize) > FileOffset)
KeAcquireSpinLock(&CiCacheSegMappingRegionLock, &oldIrql);
StartingOffset = RtlFindClearBitsAndSet(&CiCacheSegMappingRegionAllocMap, Bcb->CacheSegmentSize / PAGE_SIZE, CiCacheSegMappingRegionHint);
-
+
if (StartingOffset == 0xffffffff)
{
DPRINT1("Out of CacheSeg mapping space\n");
if (CiCacheSegMappingRegionHint == StartingOffset)
{
- CiCacheSegMappingRegionHint += Bcb->CacheSegmentSize / PAGE_SIZE;
+ CiCacheSegMappingRegionHint += Bcb->CacheSegmentSize / PAGE_SIZE;
}
KeReleaseSpinLock(&CiCacheSegMappingRegionLock, oldIrql);
PCACHE_SEGMENT* CacheSegList;
PCACHE_SEGMENT Previous = NULL;
- assert(Bcb);
+ ASSERT(Bcb);
DPRINT("CcRosGetCacheSegmentChain()\n");
Length = ROUND_UP(Length, Bcb->CacheSegmentSize);
#if defined(__GNUC__)
- CacheSegList = alloca(sizeof(PCACHE_SEGMENT) *
+ CacheSegList = alloca(sizeof(PCACHE_SEGMENT) *
(Length / Bcb->CacheSegmentSize));
#elif defined(_MSC_VER)
- CacheSegList = _alloca(sizeof(PCACHE_SEGMENT) *
+ CacheSegList = _alloca(sizeof(PCACHE_SEGMENT) *
(Length / Bcb->CacheSegmentSize));
#else
#error Unknown compiler for alloca intrinsic stack allocation "function"
}
}
Previous->NextInChain = NULL;
-
+
return(STATUS_SUCCESS);
}
PCACHE_SEGMENT current;
NTSTATUS Status;
- assert(Bcb);
+ ASSERT(Bcb);
DPRINT("CcRosGetCacheSegment()\n");
return(STATUS_SUCCESS);
}
-NTSTATUS STDCALL
+NTSTATUS STDCALL
CcRosRequestCacheSegment(PBCB Bcb,
ULONG FileOffset,
PVOID* BaseAddress,
{
ULONG BaseOffset;
- assert(Bcb);
+ ASSERT(Bcb);
if ((FileOffset % Bcb->CacheSegmentSize) != 0)
{
}
#ifdef CACHE_BITMAP
#else
-STATIC VOID
-CcFreeCachePage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
+STATIC VOID
+CcFreeCachePage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
PFN_TYPE Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
{
- assert(SwapEntry == 0);
+ ASSERT(SwapEntry == 0);
if (Page != 0)
{
MmReleasePageMemoryConsumer(MC_CACHE, Page);
}
}
#endif
-NTSTATUS
+NTSTATUS
CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg)
/*
* FUNCTION: Releases a cache segment associated with a BCB
/* Unmap all the pages. */
for (i = 0; i < RegionSize; i++)
{
- MmDeleteVirtualMapping(NULL,
+ MmDeleteVirtualMapping(NULL,
CacheSeg->BaseAddress + (i * PAGE_SIZE),
FALSE,
NULL,
KeAcquireSpinLock(&CiCacheSegMappingRegionLock, &oldIrql);
/* Deallocate all the pages used. */
Base = (ULONG)(CacheSeg->BaseAddress - CiCacheSegMappingRegionBase) / PAGE_SIZE;
-
+
RtlClearBits(&CiCacheSegMappingRegionAllocMap, Base, RegionSize);
CiCacheSegMappingRegionHint = min (CiCacheSegMappingRegionHint, Base);
#else
MmLockAddressSpace(MmGetKernelAddressSpace());
MmFreeMemoryArea(MmGetKernelAddressSpace(),
- CacheSeg->BaseAddress,
- CacheSeg->Bcb->CacheSegmentSize,
+ CacheSeg->MemoryArea,
CcFreeCachePage,
NULL);
MmUnlockAddressSpace(MmGetKernelAddressSpace());
NTSTATUS Status;
KIRQL oldIrql;
- assert(Bcb);
+ ASSERT(Bcb);
DPRINT("CcRosFreeCacheSegment(Bcb %x, CacheSeg %x)\n",
Bcb, CacheSeg);
if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
{
Bcb = (PBCB)SectionObjectPointers->SharedCacheMap;
- assert(Bcb);
+ ASSERT(Bcb);
if (FileOffset)
{
Offset = *FileOffset;
}
- else
+ else
{
-#if defined(__GNUC__)
- Offset.QuadPart = 0LL;
-#else
- Offset.QuadPart = 0;
-#endif
+ Offset.QuadPart = (LONGLONG)0;
Length = Bcb->FileSize.u.LowPart;
}
-
+
if (IoStatus)
{
IoStatus->Status = STATUS_SUCCESS;
}
}
-NTSTATUS
+NTSTATUS
CcRosDeleteFileCache(PFILE_OBJECT FileObject, PBCB Bcb)
/*
* FUNCTION: Releases the BCB associated with a file object
LIST_ENTRY FreeList;
KIRQL oldIrql;
- assert(Bcb);
-
+ ASSERT(Bcb);
+
Bcb->RefCount++;
ExReleaseFastMutex(&ViewLock);
Bcb->BcbRemoveListEntry.Flink = NULL;
}
- FileObject->SectionObjectPointer->SharedCacheMap = NULL;
+ FileObject->SectionObjectPointer->SharedCacheMap = NULL;
/*
* Release all cache segments.
}
InsertHeadList(&FreeList, ¤t->BcbSegmentListEntry);
}
- KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
+ KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
ExReleaseFastMutex(&ViewLock);
ObDereferenceObject (Bcb->FileObject);
current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry);
Status = CcRosInternalFreeCacheSegment(current);
}
- ExFreeToNPagedLookasideList(&BcbLookasideList, Bcb);
+ ExFreeToNPagedLookasideList(&BcbLookasideList, Bcb);
ExAcquireFastMutex(&ViewLock);
}
return(STATUS_SUCCESS);
PBCB Bcb;
ExAcquireFastMutex(&ViewLock);
Bcb = (PBCB)FileObject->SectionObjectPointer->SharedCacheMap;
- assert(Bcb);
+ ASSERT(Bcb);
if (Bcb->RefCount == 0)
{
- assert(Bcb->BcbRemoveListEntry.Flink != NULL);
+ ASSERT(Bcb->BcbRemoveListEntry.Flink != NULL);
RemoveEntryList(&Bcb->BcbRemoveListEntry);
Bcb->BcbRemoveListEntry.Flink = NULL;
}
else
{
- assert(Bcb->BcbRemoveListEntry.Flink == NULL);
+ ASSERT(Bcb->BcbRemoveListEntry.Flink == NULL);
}
Bcb->RefCount++;
ExReleaseFastMutex(&ViewLock);
PBCB Bcb;
ExAcquireFastMutex(&ViewLock);
Bcb = (PBCB)FileObject->SectionObjectPointer->SharedCacheMap;
- assert(Bcb);
+ ASSERT(Bcb);
if (Bcb->RefCount > 0)
{
Bcb->RefCount--;
ExReleaseFastMutex(&ViewLock);
}
-NTSTATUS STDCALL
+NTSTATUS STDCALL
CcRosReleaseFileCache(PFILE_OBJECT FileObject)
/*
* FUNCTION: Called by the file system when a handle to a file object
Bcb->RefCount--;
if (Bcb->RefCount == 0)
{
+ MmFreeSectionSegments(Bcb->FileObject);
if (Bcb->RemoveOnClose)
{
CcRosDeleteFileCache(FileObject, Bcb);
return(STATUS_SUCCESS);
}
-NTSTATUS
+NTSTATUS
CcTryToInitializeFileCache(PFILE_OBJECT FileObject)
{
PBCB Bcb;
}
-NTSTATUS STDCALL
+NTSTATUS STDCALL
CcRosInitializeFileCache(PFILE_OBJECT FileObject,
ULONG CacheSegmentSize)
/*
*/
{
PBCB Bcb;
+
+ Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
DPRINT("CcRosInitializeFileCache(FileObject %x, *Bcb %x, CacheSegmentSize %d)\n",
FileObject, Bcb, CacheSegmentSize);
ExAcquireFastMutex(&ViewLock);
-
- Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
if (Bcb == NULL)
{
- Bcb = ExAllocateFromNPagedLookasideList(&BcbLookasideList);
+ Bcb = ExAllocateFromNPagedLookasideList(&BcbLookasideList);
if (Bcb == NULL)
{
ExReleaseFastMutex(&ViewLock);
return(STATUS_UNSUCCESSFUL);
}
- memset(Bcb, 0, sizeof(BCB));
+ memset(Bcb, 0, sizeof(BCB));
ObReferenceObjectByPointer(FileObject,
FILE_ALL_ACCESS,
NULL,
Bcb->CacheSegmentSize = CacheSegmentSize;
if (FileObject->FsContext)
{
- Bcb->AllocationSize =
+ Bcb->AllocationSize =
((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->AllocationSize;
- Bcb->FileSize =
+ Bcb->FileSize =
((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->FileSize;
}
KeInitializeSpinLock(&Bcb->BcbLock);
if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
{
Bcb = (PBCB)SectionObjectPointers->SharedCacheMap;
- assert(Bcb);
+ ASSERT(Bcb);
return Bcb->FileObject;
}
return NULL;
while (1)
{
-#if defined(__GNUC__)
- Timeout.QuadPart += 100000000LL; // 10sec
-#else
- Timeout.QuadPart += 100000000; // 10sec
-#endif
+ Timeout.QuadPart += (LONGLONG)100000000; // 10sec
Status = KeWaitForSingleObject(&LazyCloseThreadEvent,
0,
KernelMode,
DbgPrint("LazyCloseThread: Terminating\n");
break;
}
-
+
ExAcquireFastMutex(&ViewLock);
CcTimeStamp++;
if (CcTimeStamp >= 30)
#endif
NTSTATUS Status;
KPRIORITY Priority;
-
+
DPRINT("CcInitView()\n");
#ifdef CACHE_BITMAP
BoundaryAddressMultiple.QuadPart = 0;
RtlClearAllBits(&CiCacheSegMappingRegionAllocMap);
KeInitializeSpinLock(&CiCacheSegMappingRegionLock);
-#endif
+#endif
InitializeListHead(&CacheSegmentListHead);
InitializeListHead(&DirtySegmentListHead);
InitializeListHead(&CacheSegmentLRUListHead);
20);
MmInitializeMemoryConsumer(MC_CACHE, CcRosTrimCache);
-
+
CcInitCacheZeroPage();
- CcTimeStamp = 0;
+ CcTimeStamp = 0;
LazyCloseThreadShouldTerminate = FALSE;
KeInitializeEvent (&LazyCloseThreadEvent, SynchronizationEvent, FALSE);
Status = PsCreateSystemThread(&LazyCloseThreadHandle,