2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/cc/view.c
5 * PURPOSE: Cache manager
7 * PROGRAMMERS: David Welch (welch@mcmail.com)
10 /* NOTES **********************************************************************
12 * This is not the NT implementation of a file cache nor anything much like
15 * The general procedure for a filesystem to implement a read or write
16 * dispatch routine is as follows
18 * (1) If caching for the FCB hasn't been initiated then so do by calling
19 * CcInitializeFileCache.
21 * (2) For each 4k region which is being read or written obtain a cache page
22 * by calling CcRequestCachePage.
24 * (3) If either the page is being read or not completely written, and it is
25 * not up to date then read its data from the underlying medium. If the read
26 * fails then call CcReleaseCachePage with VALID as FALSE and return a error.
28 * (4) Copy the data into or out of the page as necessary.
30 * (5) Release the cache page
32 /* INCLUDES ******************************************************************/
38 #if defined (ALLOC_PRAGMA)
39 #pragma alloc_text(INIT, CcInitView)
42 /* GLOBALS *******************************************************************/
45 * If CACHE_BITMAP is defined, the cache manager uses one large memory region
46 * within the kernel address space and allocate/deallocate space from this block
47 * over a bitmap. If CACHE_BITMAP is used, the size of the mdl mapping region
48 * must be reduced (ntoskrnl\mm\mdl.c, MI_MDLMAPPING_REGION_SIZE).
50 //#define CACHE_BITMAP
52 static LIST_ENTRY DirtySegmentListHead
;
53 static LIST_ENTRY CacheSegmentListHead
;
54 static LIST_ENTRY CacheSegmentLRUListHead
;
55 static LIST_ENTRY ClosedListHead
;
56 ULONG DirtyPageCount
=0;
58 KGUARDED_MUTEX ViewLock
;
61 #define CI_CACHESEG_MAPPING_REGION_SIZE (128*1024*1024)
63 static PVOID CiCacheSegMappingRegionBase
= NULL
;
64 static RTL_BITMAP CiCacheSegMappingRegionAllocMap
;
65 static ULONG CiCacheSegMappingRegionHint
;
66 static KSPIN_LOCK CiCacheSegMappingRegionLock
;
69 NPAGED_LOOKASIDE_LIST iBcbLookasideList
;
70 static NPAGED_LOOKASIDE_LIST BcbLookasideList
;
71 static NPAGED_LOOKASIDE_LIST CacheSegLookasideList
;
74 static void CcRosCacheSegmentIncRefCount_ ( PCACHE_SEGMENT cs
, const char* file
, int line
)
79 DbgPrint("(%s:%i) CacheSegment %p ++RefCount=%d, Dirty %d, PageOut %d\n",
80 file
, line
, cs
, cs
->ReferenceCount
, cs
->Dirty
, cs
->PageOut
);
83 static void CcRosCacheSegmentDecRefCount_ ( PCACHE_SEGMENT cs
, const char* file
, int line
)
88 DbgPrint("(%s:%i) CacheSegment %p --RefCount=%d, Dirty %d, PageOut %d\n",
89 file
, line
, cs
, cs
->ReferenceCount
, cs
->Dirty
, cs
->PageOut
);
92 #define CcRosCacheSegmentIncRefCount(cs) CcRosCacheSegmentIncRefCount_(cs,__FILE__,__LINE__)
93 #define CcRosCacheSegmentDecRefCount(cs) CcRosCacheSegmentDecRefCount_(cs,__FILE__,__LINE__)
95 #define CcRosCacheSegmentIncRefCount(cs) (++((cs)->ReferenceCount))
96 #define CcRosCacheSegmentDecRefCount(cs) (--((cs)->ReferenceCount))
100 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg
);
103 /* FUNCTIONS *****************************************************************/
113 PLIST_ENTRY current_entry
;
114 PCACHE_SEGMENT current
;
123 DPRINT1("Enabling Tracing for CacheMap 0x%p:\n", Bcb
);
125 KeAcquireGuardedMutex(&ViewLock
);
126 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
128 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
129 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
131 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
132 current_entry
= current_entry
->Flink
;
134 DPRINT1(" CacheSegment 0x%p enabled, RefCount %d, Dirty %d, PageOut %d\n",
135 current
, current
->ReferenceCount
, current
->Dirty
, current
->PageOut
);
137 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
138 KeReleaseGuardedMutex(&ViewLock
);
142 DPRINT1("Disabling Tracing for CacheMap 0x%p:\n", Bcb
);
153 CcRosFlushCacheSegment(PCACHE_SEGMENT CacheSegment
)
158 Status
= WriteCacheSegment(CacheSegment
);
159 if (NT_SUCCESS(Status
))
161 KeAcquireGuardedMutex(&ViewLock
);
162 KeAcquireSpinLock(&CacheSegment
->Bcb
->BcbLock
, &oldIrql
);
164 CacheSegment
->Dirty
= FALSE
;
165 RemoveEntryList(&CacheSegment
->DirtySegmentListEntry
);
166 DirtyPageCount
-= CacheSegment
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
167 CcRosCacheSegmentDecRefCount ( CacheSegment
);
169 KeReleaseSpinLock(&CacheSegment
->Bcb
->BcbLock
, oldIrql
);
170 KeReleaseGuardedMutex(&ViewLock
);
178 CcRosFlushDirtyPages(ULONG Target
, PULONG Count
)
180 PLIST_ENTRY current_entry
;
181 PCACHE_SEGMENT current
;
182 ULONG PagesPerSegment
;
185 static ULONG WriteCount
[4] = {0, 0, 0, 0};
188 DPRINT("CcRosFlushDirtyPages(Target %d)\n", Target
);
192 KeAcquireGuardedMutex(&ViewLock
);
194 WriteCount
[0] = WriteCount
[1];
195 WriteCount
[1] = WriteCount
[2];
196 WriteCount
[2] = WriteCount
[3];
199 NewTarget
= WriteCount
[0] + WriteCount
[1] + WriteCount
[2];
201 if (NewTarget
< DirtyPageCount
)
203 NewTarget
= (DirtyPageCount
- NewTarget
+ 3) / 4;
204 WriteCount
[0] += NewTarget
;
205 WriteCount
[1] += NewTarget
;
206 WriteCount
[2] += NewTarget
;
207 WriteCount
[3] += NewTarget
;
210 NewTarget
= WriteCount
[0];
212 Target
= max(NewTarget
, Target
);
214 current_entry
= DirtySegmentListHead
.Flink
;
215 if (current_entry
== &DirtySegmentListHead
)
217 DPRINT("No Dirty pages\n");
220 while (current_entry
!= &DirtySegmentListHead
&& Target
> 0)
222 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
223 DirtySegmentListEntry
);
224 current_entry
= current_entry
->Flink
;
226 Locked
= current
->Bcb
->Callbacks
->AcquireForLazyWrite(
227 current
->Bcb
->LazyWriteContext
, FALSE
);
233 Locked
= ExTryToAcquirePushLockExclusive(¤t
->Lock
);
236 current
->Bcb
->Callbacks
->ReleaseFromLazyWrite(
237 current
->Bcb
->LazyWriteContext
);
242 ASSERT(current
->Dirty
);
243 if (current
->ReferenceCount
> 1)
245 ExReleasePushLock(¤t
->Lock
);
246 current
->Bcb
->Callbacks
->ReleaseFromLazyWrite(
247 current
->Bcb
->LazyWriteContext
);
251 PagesPerSegment
= current
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
253 KeReleaseGuardedMutex(&ViewLock
);
255 Status
= CcRosFlushCacheSegment(current
);
257 ExReleasePushLock(¤t
->Lock
);
258 current
->Bcb
->Callbacks
->ReleaseFromLazyWrite(
259 current
->Bcb
->LazyWriteContext
);
261 if (!NT_SUCCESS(Status
) && (Status
!= STATUS_END_OF_FILE
))
263 DPRINT1("CC: Failed to flush cache segment.\n");
267 (*Count
) += PagesPerSegment
;
268 Target
-= PagesPerSegment
;
271 KeAcquireGuardedMutex(&ViewLock
);
272 current_entry
= DirtySegmentListHead
.Flink
;
275 if (*Count
< NewTarget
)
277 WriteCount
[1] += (NewTarget
- *Count
);
280 KeReleaseGuardedMutex(&ViewLock
);
282 DPRINT("CcRosFlushDirtyPages() finished\n");
283 return(STATUS_SUCCESS
);
287 CcRosTrimCache(ULONG Target
, ULONG Priority
, PULONG NrFreed
)
289 * FUNCTION: Try to free some memory from the file cache.
291 * Target - The number of pages to be freed.
292 * Priority - The priority of free (currently unused).
293 * NrFreed - Points to a variable where the number of pages
294 * actually freed is returned.
297 PLIST_ENTRY current_entry
;
298 PCACHE_SEGMENT current
;
299 ULONG PagesPerSegment
;
304 DPRINT("CcRosTrimCache(Target %d)\n", Target
);
308 InitializeListHead(&FreeList
);
310 KeAcquireGuardedMutex(&ViewLock
);
311 current_entry
= CacheSegmentLRUListHead
.Flink
;
312 while (current_entry
!= &CacheSegmentLRUListHead
&& Target
> 0)
314 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
315 CacheSegmentLRUListEntry
);
316 current_entry
= current_entry
->Flink
;
318 KeAcquireSpinLock(¤t
->Bcb
->BcbLock
, &oldIrql
);
320 if (current
->MappedCount
> 0 && !current
->Dirty
&& !current
->PageOut
)
324 CcRosCacheSegmentIncRefCount(current
);
325 current
->PageOut
= TRUE
;
326 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
327 KeReleaseGuardedMutex(&ViewLock
);
328 for (i
= 0; i
< current
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
; i
++)
331 Page
= (PFN_NUMBER
)(MmGetPhysicalAddress((char*)current
->BaseAddress
+ i
* PAGE_SIZE
).QuadPart
>> PAGE_SHIFT
);
332 MmPageOutPhysicalAddress(Page
);
334 KeAcquireGuardedMutex(&ViewLock
);
335 KeAcquireSpinLock(¤t
->Bcb
->BcbLock
, &oldIrql
);
336 CcRosCacheSegmentDecRefCount(current
);
339 if (current
->ReferenceCount
== 0)
341 PagesPerSegment
= current
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
342 // PagesFreed = PagesPerSegment;
343 PagesFreed
= min(PagesPerSegment
, Target
);
344 Target
-= PagesFreed
;
345 (*NrFreed
) += PagesFreed
;
348 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
351 current_entry
= CacheSegmentLRUListHead
.Flink
;
352 while (current_entry
!= &CacheSegmentLRUListHead
)
354 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
355 CacheSegmentLRUListEntry
);
356 current
->PageOut
= FALSE
;
357 current_entry
= current_entry
->Flink
;
359 KeAcquireSpinLock(¤t
->Bcb
->BcbLock
, &oldIrql
);
360 if (current
->ReferenceCount
== 0)
362 RemoveEntryList(¤t
->BcbSegmentListEntry
);
363 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
364 RemoveEntryList(¤t
->CacheSegmentListEntry
);
365 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
366 InsertHeadList(&FreeList
, ¤t
->BcbSegmentListEntry
);
370 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
374 KeReleaseGuardedMutex(&ViewLock
);
376 while (!IsListEmpty(&FreeList
))
378 current_entry
= RemoveHeadList(&FreeList
);
379 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
380 BcbSegmentListEntry
);
381 CcRosInternalFreeCacheSegment(current
);
384 return(STATUS_SUCCESS
);
389 CcRosReleaseCacheSegment(PBCB Bcb
,
390 PCACHE_SEGMENT CacheSeg
,
395 BOOLEAN WasDirty
= CacheSeg
->Dirty
;
400 DPRINT("CcReleaseCacheSegment(Bcb 0x%p, CacheSeg 0x%p, Valid %d)\n",
401 Bcb
, CacheSeg
, Valid
);
403 CacheSeg
->Valid
= Valid
;
404 CacheSeg
->Dirty
= CacheSeg
->Dirty
|| Dirty
;
406 KeAcquireGuardedMutex(&ViewLock
);
407 if (!WasDirty
&& CacheSeg
->Dirty
)
409 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
410 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
412 RemoveEntryList(&CacheSeg
->CacheSegmentLRUListEntry
);
413 InsertTailList(&CacheSegmentLRUListHead
, &CacheSeg
->CacheSegmentLRUListEntry
);
417 CacheSeg
->MappedCount
++;
419 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
420 CcRosCacheSegmentDecRefCount(CacheSeg
);
421 if (Mapped
&& CacheSeg
->MappedCount
== 1)
423 CcRosCacheSegmentIncRefCount(CacheSeg
);
425 if (!WasDirty
&& CacheSeg
->Dirty
)
427 CcRosCacheSegmentIncRefCount(CacheSeg
);
429 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
430 KeReleaseGuardedMutex(&ViewLock
);
431 ExReleasePushLock(&CacheSeg
->Lock
);
433 return(STATUS_SUCCESS
);
436 /* Returns with Cache Segment Lock Held! */
439 CcRosLookupCacheSegment(PBCB Bcb
, ULONG FileOffset
)
441 PLIST_ENTRY current_entry
;
442 PCACHE_SEGMENT current
;
447 DPRINT("CcRosLookupCacheSegment(Bcb -x%p, FileOffset %d)\n", Bcb
, FileOffset
);
449 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
450 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
451 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
453 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
454 BcbSegmentListEntry
);
455 if (current
->FileOffset
<= FileOffset
&&
456 (current
->FileOffset
+ Bcb
->CacheSegmentSize
) > FileOffset
)
458 CcRosCacheSegmentIncRefCount(current
);
459 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
460 ExAcquirePushLockExclusive(¤t
->Lock
);
463 current_entry
= current_entry
->Flink
;
465 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
471 CcRosMarkDirtyCacheSegment(PBCB Bcb
, ULONG FileOffset
)
473 PCACHE_SEGMENT CacheSeg
;
478 DPRINT("CcRosMarkDirtyCacheSegment(Bcb 0x%p, FileOffset %d)\n", Bcb
, FileOffset
);
480 CacheSeg
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
481 if (CacheSeg
== NULL
)
483 KeBugCheck(CACHE_MANAGER
);
485 if (!CacheSeg
->Dirty
)
487 KeAcquireGuardedMutex(&ViewLock
);
488 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
489 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
490 KeReleaseGuardedMutex(&ViewLock
);
494 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
495 CcRosCacheSegmentDecRefCount(CacheSeg
);
496 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
500 CacheSeg
->Dirty
= TRUE
;
501 ExReleasePushLock(&CacheSeg
->Lock
);
503 return(STATUS_SUCCESS
);
508 CcRosUnmapCacheSegment(PBCB Bcb
, ULONG FileOffset
, BOOLEAN NowDirty
)
510 PCACHE_SEGMENT CacheSeg
;
516 DPRINT("CcRosUnmapCacheSegment(Bcb 0x%p, FileOffset %d, NowDirty %d)\n",
517 Bcb
, FileOffset
, NowDirty
);
519 CacheSeg
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
520 if (CacheSeg
== NULL
)
522 return(STATUS_UNSUCCESSFUL
);
525 WasDirty
= CacheSeg
->Dirty
;
526 CacheSeg
->Dirty
= CacheSeg
->Dirty
|| NowDirty
;
528 CacheSeg
->MappedCount
--;
530 if (!WasDirty
&& NowDirty
)
532 KeAcquireGuardedMutex(&ViewLock
);
533 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
534 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
535 KeReleaseGuardedMutex(&ViewLock
);
538 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
539 CcRosCacheSegmentDecRefCount(CacheSeg
);
540 if (!WasDirty
&& NowDirty
)
542 CcRosCacheSegmentIncRefCount(CacheSeg
);
544 if (CacheSeg
->MappedCount
== 0)
546 CcRosCacheSegmentDecRefCount(CacheSeg
);
548 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
550 ExReleasePushLock(&CacheSeg
->Lock
);
551 return(STATUS_SUCCESS
);
556 CcRosCreateCacheSegment(PBCB Bcb
,
558 PCACHE_SEGMENT
* CacheSeg
)
560 PCACHE_SEGMENT current
;
561 PCACHE_SEGMENT previous
;
562 PLIST_ENTRY current_entry
;
566 ULONG StartingOffset
;
568 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
572 DPRINT("CcRosCreateCacheSegment()\n");
574 BoundaryAddressMultiple
.QuadPart
= 0;
575 if (FileOffset
>= Bcb
->FileSize
.u
.LowPart
)
578 return STATUS_INVALID_PARAMETER
;
581 current
= ExAllocateFromNPagedLookasideList(&CacheSegLookasideList
);
582 current
->Valid
= FALSE
;
583 current
->Dirty
= FALSE
;
584 current
->PageOut
= FALSE
;
585 current
->FileOffset
= ROUND_DOWN(FileOffset
, Bcb
->CacheSegmentSize
);
590 DPRINT1("CacheMap 0x%p: new Cache Segment: 0x%p\n", Bcb
, current
);
593 current
->MappedCount
= 0;
594 current
->DirtySegmentListEntry
.Flink
= NULL
;
595 current
->DirtySegmentListEntry
.Blink
= NULL
;
596 current
->ReferenceCount
= 1;
597 ExInitializePushLock((PULONG_PTR
)¤t
->Lock
);
598 ExAcquirePushLockExclusive(¤t
->Lock
);
599 KeAcquireGuardedMutex(&ViewLock
);
602 /* There is window between the call to CcRosLookupCacheSegment
603 * and CcRosCreateCacheSegment. We must check if a segment on
604 * the fileoffset exist. If there exist a segment, we release
605 * our new created segment and return the existing one.
607 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
608 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
610 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
612 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
613 BcbSegmentListEntry
);
614 if (current
->FileOffset
<= FileOffset
&&
615 (current
->FileOffset
+ Bcb
->CacheSegmentSize
) > FileOffset
)
617 CcRosCacheSegmentIncRefCount(current
);
618 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
622 DPRINT1("CacheMap 0x%p: deleting newly created Cache Segment 0x%p ( found existing one 0x%p )\n",
628 ExReleasePushLock(&(*CacheSeg
)->Lock
);
629 KeReleaseGuardedMutex(&ViewLock
);
630 ExFreeToNPagedLookasideList(&CacheSegLookasideList
, *CacheSeg
);
632 ExAcquirePushLockExclusive(¤t
->Lock
);
633 return STATUS_SUCCESS
;
635 if (current
->FileOffset
< FileOffset
)
637 if (previous
== NULL
)
643 if (previous
->FileOffset
< current
->FileOffset
)
649 current_entry
= current_entry
->Flink
;
651 /* There was no existing segment. */
655 InsertHeadList(&previous
->BcbSegmentListEntry
, ¤t
->BcbSegmentListEntry
);
659 InsertHeadList(&Bcb
->BcbSegmentListHead
, ¤t
->BcbSegmentListEntry
);
661 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
662 InsertTailList(&CacheSegmentListHead
, ¤t
->CacheSegmentListEntry
);
663 InsertTailList(&CacheSegmentLRUListHead
, ¤t
->CacheSegmentLRUListEntry
);
664 KeReleaseGuardedMutex(&ViewLock
);
666 KeAcquireSpinLock(&CiCacheSegMappingRegionLock
, &oldIrql
);
668 StartingOffset
= RtlFindClearBitsAndSet(&CiCacheSegMappingRegionAllocMap
, Bcb
->CacheSegmentSize
/ PAGE_SIZE
, CiCacheSegMappingRegionHint
);
670 if (StartingOffset
== 0xffffffff)
672 DPRINT1("Out of CacheSeg mapping space\n");
673 KeBugCheck(CACHE_MANAGER
);
676 current
->BaseAddress
= CiCacheSegMappingRegionBase
+ StartingOffset
* PAGE_SIZE
;
678 if (CiCacheSegMappingRegionHint
== StartingOffset
)
680 CiCacheSegMappingRegionHint
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
683 KeReleaseSpinLock(&CiCacheSegMappingRegionLock
, oldIrql
);
685 MmLockAddressSpace(MmGetKernelAddressSpace());
686 current
->BaseAddress
= NULL
;
687 Status
= MmCreateMemoryArea(MmGetKernelAddressSpace(),
688 MEMORY_AREA_CACHE_SEGMENT
,
689 ¤t
->BaseAddress
,
690 Bcb
->CacheSegmentSize
,
692 (PMEMORY_AREA
*)¤t
->MemoryArea
,
695 BoundaryAddressMultiple
);
696 MmUnlockAddressSpace(MmGetKernelAddressSpace());
697 if (!NT_SUCCESS(Status
))
699 KeBugCheck(CACHE_MANAGER
);
703 /* Create a virtual mapping for this memory area */
704 MmMapMemoryArea(current
->BaseAddress
, Bcb
->CacheSegmentSize
,
705 MC_CACHE
, PAGE_READWRITE
);
707 return(STATUS_SUCCESS
);
712 CcRosGetCacheSegmentChain(PBCB Bcb
,
715 PCACHE_SEGMENT
* CacheSeg
)
717 PCACHE_SEGMENT current
;
719 PCACHE_SEGMENT
* CacheSegList
;
720 PCACHE_SEGMENT Previous
= NULL
;
724 DPRINT("CcRosGetCacheSegmentChain()\n");
726 Length
= ROUND_UP(Length
, Bcb
->CacheSegmentSize
);
728 CacheSegList
= _alloca(sizeof(PCACHE_SEGMENT
) *
729 (Length
/ Bcb
->CacheSegmentSize
));
732 * Look for a cache segment already mapping the same data.
734 for (i
= 0; i
< (Length
/ Bcb
->CacheSegmentSize
); i
++)
736 ULONG CurrentOffset
= FileOffset
+ (i
* Bcb
->CacheSegmentSize
);
737 current
= CcRosLookupCacheSegment(Bcb
, CurrentOffset
);
740 CacheSegList
[i
] = current
;
744 CcRosCreateCacheSegment(Bcb
, CurrentOffset
, ¤t
);
745 CacheSegList
[i
] = current
;
749 for (i
= 0; i
< (Length
/ Bcb
->CacheSegmentSize
); i
++)
753 *CacheSeg
= CacheSegList
[i
];
754 Previous
= CacheSegList
[i
];
758 Previous
->NextInChain
= CacheSegList
[i
];
759 Previous
= CacheSegList
[i
];
763 Previous
->NextInChain
= NULL
;
765 return(STATUS_SUCCESS
);
770 CcRosGetCacheSegment(PBCB Bcb
,
775 PCACHE_SEGMENT
* CacheSeg
)
777 PCACHE_SEGMENT current
;
782 DPRINT("CcRosGetCacheSegment()\n");
785 * Look for a cache segment already mapping the same data.
787 current
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
791 * Otherwise create a new segment.
793 Status
= CcRosCreateCacheSegment(Bcb
, FileOffset
, ¤t
);
794 if (!NT_SUCCESS(Status
))
800 * Return information about the segment to the caller.
802 *UptoDate
= current
->Valid
;
803 *BaseAddress
= current
->BaseAddress
;
804 DPRINT("*BaseAddress 0x%.8X\n", *BaseAddress
);
806 *BaseOffset
= current
->FileOffset
;
807 return(STATUS_SUCCESS
);
811 CcRosRequestCacheSegment(PBCB Bcb
,
815 PCACHE_SEGMENT
* CacheSeg
)
817 * FUNCTION: Request a page mapping for a BCB
824 if ((FileOffset
% Bcb
->CacheSegmentSize
) != 0)
826 DPRINT1("Bad fileoffset %x should be multiple of %x",
827 FileOffset
, Bcb
->CacheSegmentSize
);
828 KeBugCheck(CACHE_MANAGER
);
831 return(CcRosGetCacheSegment(Bcb
,
841 CcFreeCachePage(PVOID Context
, MEMORY_AREA
* MemoryArea
, PVOID Address
,
842 PFN_NUMBER Page
, SWAPENTRY SwapEntry
, BOOLEAN Dirty
)
844 ASSERT(SwapEntry
== 0);
847 MmReleasePageMemoryConsumer(MC_CACHE
, Page
);
852 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg
)
854 * FUNCTION: Releases a cache segment associated with a BCB
864 DPRINT("Freeing cache segment 0x%p\n", CacheSeg
);
866 if ( CacheSeg
->Bcb
->Trace
)
868 DPRINT1("CacheMap 0x%p: deleting Cache Segment: 0x%p\n", CacheSeg
->Bcb
, CacheSeg
);
872 RegionSize
= CacheSeg
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
874 /* Unmap all the pages. */
875 for (i
= 0; i
< RegionSize
; i
++)
877 MmDeleteVirtualMapping(NULL
,
878 CacheSeg
->BaseAddress
+ (i
* PAGE_SIZE
),
882 MmReleasePageMemoryConsumer(MC_CACHE
, Page
);
885 KeAcquireSpinLock(&CiCacheSegMappingRegionLock
, &oldIrql
);
886 /* Deallocate all the pages used. */
887 Base
= (ULONG
)(CacheSeg
->BaseAddress
- CiCacheSegMappingRegionBase
) / PAGE_SIZE
;
889 RtlClearBits(&CiCacheSegMappingRegionAllocMap
, Base
, RegionSize
);
891 CiCacheSegMappingRegionHint
= min (CiCacheSegMappingRegionHint
, Base
);
893 KeReleaseSpinLock(&CiCacheSegMappingRegionLock
, oldIrql
);
895 MmLockAddressSpace(MmGetKernelAddressSpace());
896 MmFreeMemoryArea(MmGetKernelAddressSpace(),
897 CacheSeg
->MemoryArea
,
900 MmUnlockAddressSpace(MmGetKernelAddressSpace());
902 ExFreeToNPagedLookasideList(&CacheSegLookasideList
, CacheSeg
);
903 return(STATUS_SUCCESS
);
908 CcRosFreeCacheSegment(PBCB Bcb
, PCACHE_SEGMENT CacheSeg
)
915 DPRINT("CcRosFreeCacheSegment(Bcb 0x%p, CacheSeg 0x%p)\n",
918 KeAcquireGuardedMutex(&ViewLock
);
919 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
920 RemoveEntryList(&CacheSeg
->BcbSegmentListEntry
);
921 RemoveEntryList(&CacheSeg
->CacheSegmentListEntry
);
922 RemoveEntryList(&CacheSeg
->CacheSegmentLRUListEntry
);
925 RemoveEntryList(&CacheSeg
->DirtySegmentListEntry
);
926 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
929 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
930 KeReleaseGuardedMutex(&ViewLock
);
932 Status
= CcRosInternalFreeCacheSegment(CacheSeg
);
940 CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointers
,
941 IN PLARGE_INTEGER FileOffset OPTIONAL
,
943 OUT PIO_STATUS_BLOCK IoStatus
)
946 LARGE_INTEGER Offset
;
947 PCACHE_SEGMENT current
;
951 DPRINT("CcFlushCache(SectionObjectPointers 0x%p, FileOffset 0x%p, Length %d, IoStatus 0x%p)\n",
952 SectionObjectPointers
, FileOffset
, Length
, IoStatus
);
954 if (SectionObjectPointers
&& SectionObjectPointers
->SharedCacheMap
)
956 Bcb
= (PBCB
)SectionObjectPointers
->SharedCacheMap
;
960 Offset
= *FileOffset
;
964 Offset
.QuadPart
= (LONGLONG
)0;
965 Length
= Bcb
->FileSize
.u
.LowPart
;
970 IoStatus
->Status
= STATUS_SUCCESS
;
971 IoStatus
->Information
= 0;
976 current
= CcRosLookupCacheSegment (Bcb
, Offset
.u
.LowPart
);
981 Status
= CcRosFlushCacheSegment(current
);
982 if (!NT_SUCCESS(Status
) && IoStatus
!= NULL
)
984 IoStatus
->Status
= Status
;
987 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
988 ExReleasePushLock(¤t
->Lock
);
989 CcRosCacheSegmentDecRefCount(current
);
990 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
993 Offset
.QuadPart
+= Bcb
->CacheSegmentSize
;
994 if (Length
> Bcb
->CacheSegmentSize
)
996 Length
-= Bcb
->CacheSegmentSize
;
1008 IoStatus
->Status
= STATUS_INVALID_PARAMETER
;
1015 CcRosDeleteFileCache(PFILE_OBJECT FileObject
, PBCB Bcb
)
1017 * FUNCTION: Releases the BCB associated with a file object
1020 PLIST_ENTRY current_entry
;
1021 PCACHE_SEGMENT current
;
1022 LIST_ENTRY FreeList
;
1028 KeReleaseGuardedMutex(&ViewLock
);
1030 CcFlushCache(FileObject
->SectionObjectPointer
, NULL
, 0, NULL
);
1032 KeAcquireGuardedMutex(&ViewLock
);
1034 if (Bcb
->RefCount
== 0)
1036 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1038 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1039 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1042 FileObject
->SectionObjectPointer
->SharedCacheMap
= NULL
;
1045 * Release all cache segments.
1047 InitializeListHead(&FreeList
);
1048 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
1049 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
1050 while (!IsListEmpty(&Bcb
->BcbSegmentListHead
))
1052 current_entry
= RemoveTailList(&Bcb
->BcbSegmentListHead
);
1053 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
1054 RemoveEntryList(¤t
->CacheSegmentListEntry
);
1055 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
1058 RemoveEntryList(¤t
->DirtySegmentListEntry
);
1059 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
1060 DPRINT1("Freeing dirty segment\n");
1062 InsertHeadList(&FreeList
, ¤t
->BcbSegmentListEntry
);
1067 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
1069 KeReleaseGuardedMutex(&ViewLock
);
1070 ObDereferenceObject (Bcb
->FileObject
);
1072 while (!IsListEmpty(&FreeList
))
1074 current_entry
= RemoveTailList(&FreeList
);
1075 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
1076 CcRosInternalFreeCacheSegment(current
);
1078 ExFreeToNPagedLookasideList(&BcbLookasideList
, Bcb
);
1079 KeAcquireGuardedMutex(&ViewLock
);
1081 return(STATUS_SUCCESS
);
1086 CcRosReferenceCache(PFILE_OBJECT FileObject
)
1089 KeAcquireGuardedMutex(&ViewLock
);
1090 Bcb
= (PBCB
)FileObject
->SectionObjectPointer
->SharedCacheMap
;
1092 if (Bcb
->RefCount
== 0)
1094 ASSERT(Bcb
->BcbRemoveListEntry
.Flink
!= NULL
);
1095 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1096 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1101 ASSERT(Bcb
->BcbRemoveListEntry
.Flink
== NULL
);
1104 KeReleaseGuardedMutex(&ViewLock
);
1109 CcRosSetRemoveOnClose(PSECTION_OBJECT_POINTERS SectionObjectPointer
)
1112 DPRINT("CcRosSetRemoveOnClose()\n");
1113 KeAcquireGuardedMutex(&ViewLock
);
1114 Bcb
= (PBCB
)SectionObjectPointer
->SharedCacheMap
;
1117 Bcb
->RemoveOnClose
= TRUE
;
1118 if (Bcb
->RefCount
== 0)
1120 CcRosDeleteFileCache(Bcb
->FileObject
, Bcb
);
1123 KeReleaseGuardedMutex(&ViewLock
);
1129 CcRosDereferenceCache(PFILE_OBJECT FileObject
)
1132 KeAcquireGuardedMutex(&ViewLock
);
1133 Bcb
= (PBCB
)FileObject
->SectionObjectPointer
->SharedCacheMap
;
1135 if (Bcb
->RefCount
> 0)
1138 if (Bcb
->RefCount
== 0)
1140 MmFreeSectionSegments(Bcb
->FileObject
);
1141 CcRosDeleteFileCache(FileObject
, Bcb
);
1144 KeReleaseGuardedMutex(&ViewLock
);
1148 CcRosReleaseFileCache(PFILE_OBJECT FileObject
)
1150 * FUNCTION: Called by the file system when a handle to a file object
1156 KeAcquireGuardedMutex(&ViewLock
);
1158 if (FileObject
->SectionObjectPointer
->SharedCacheMap
!= NULL
)
1160 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1161 if (FileObject
->PrivateCacheMap
!= NULL
)
1163 FileObject
->PrivateCacheMap
= NULL
;
1164 if (Bcb
->RefCount
> 0)
1167 if (Bcb
->RefCount
== 0)
1169 MmFreeSectionSegments(Bcb
->FileObject
);
1170 CcRosDeleteFileCache(FileObject
, Bcb
);
1175 KeReleaseGuardedMutex(&ViewLock
);
1176 return(STATUS_SUCCESS
);
1181 CcTryToInitializeFileCache(PFILE_OBJECT FileObject
)
1186 KeAcquireGuardedMutex(&ViewLock
);
1188 ASSERT(FileObject
->SectionObjectPointer
);
1189 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1192 Status
= STATUS_UNSUCCESSFUL
;
1196 if (FileObject
->PrivateCacheMap
== NULL
)
1198 FileObject
->PrivateCacheMap
= Bcb
;
1201 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1203 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1204 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1206 Status
= STATUS_SUCCESS
;
1208 KeReleaseGuardedMutex(&ViewLock
);
1215 CcRosInitializeFileCache(PFILE_OBJECT FileObject
,
1216 ULONG CacheSegmentSize
,
1217 PCACHE_MANAGER_CALLBACKS CallBacks
,
1218 PVOID LazyWriterContext
)
1220 * FUNCTION: Initializes a BCB for a file object
1225 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1226 DPRINT("CcRosInitializeFileCache(FileObject 0x%p, Bcb 0x%p, CacheSegmentSize %d)\n",
1227 FileObject
, Bcb
, CacheSegmentSize
);
1229 KeAcquireGuardedMutex(&ViewLock
);
1232 Bcb
= ExAllocateFromNPagedLookasideList(&BcbLookasideList
);
1235 KeReleaseGuardedMutex(&ViewLock
);
1236 return(STATUS_UNSUCCESSFUL
);
1238 memset(Bcb
, 0, sizeof(BCB
));
1239 ObReferenceObjectByPointer(FileObject
,
1243 Bcb
->FileObject
= FileObject
;
1244 Bcb
->CacheSegmentSize
= CacheSegmentSize
;
1245 Bcb
->Callbacks
= CallBacks
;
1246 Bcb
->LazyWriteContext
= LazyWriterContext
;
1247 if (FileObject
->FsContext
)
1249 Bcb
->AllocationSize
=
1250 ((PFSRTL_COMMON_FCB_HEADER
)FileObject
->FsContext
)->AllocationSize
;
1252 ((PFSRTL_COMMON_FCB_HEADER
)FileObject
->FsContext
)->FileSize
;
1254 KeInitializeSpinLock(&Bcb
->BcbLock
);
1255 InitializeListHead(&Bcb
->BcbSegmentListHead
);
1256 FileObject
->SectionObjectPointer
->SharedCacheMap
= Bcb
;
1258 if (FileObject
->PrivateCacheMap
== NULL
)
1260 FileObject
->PrivateCacheMap
= Bcb
;
1263 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1265 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1266 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1268 KeReleaseGuardedMutex(&ViewLock
);
1270 return(STATUS_SUCCESS
);
1277 CcGetFileObjectFromSectionPtrs(IN PSECTION_OBJECT_POINTERS SectionObjectPointers
)
1280 if (SectionObjectPointers
&& SectionObjectPointers
->SharedCacheMap
)
1282 Bcb
= (PBCB
)SectionObjectPointers
->SharedCacheMap
;
1284 return Bcb
->FileObject
;
1297 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
1300 DPRINT("CcInitView()\n");
1302 BoundaryAddressMultiple
.QuadPart
= 0;
1303 CiCacheSegMappingRegionHint
= 0;
1304 CiCacheSegMappingRegionBase
= NULL
;
1306 MmLockAddressSpace(MmGetKernelAddressSpace());
1308 Status
= MmCreateMemoryArea(MmGetKernelAddressSpace(),
1309 MEMORY_AREA_CACHE_SEGMENT
,
1310 &CiCacheSegMappingRegionBase
,
1311 CI_CACHESEG_MAPPING_REGION_SIZE
,
1316 BoundaryAddressMultiple
);
1317 MmUnlockAddressSpace(MmGetKernelAddressSpace());
1318 if (!NT_SUCCESS(Status
))
1320 KeBugCheck(CACHE_MANAGER
);
1323 Buffer
= ExAllocatePool(NonPagedPool
, CI_CACHESEG_MAPPING_REGION_SIZE
/ (PAGE_SIZE
* 8));
1326 KeBugCheck(CACHE_MANAGER
);
1329 RtlInitializeBitMap(&CiCacheSegMappingRegionAllocMap
, Buffer
, CI_CACHESEG_MAPPING_REGION_SIZE
/ PAGE_SIZE
);
1330 RtlClearAllBits(&CiCacheSegMappingRegionAllocMap
);
1332 KeInitializeSpinLock(&CiCacheSegMappingRegionLock
);
1334 InitializeListHead(&CacheSegmentListHead
);
1335 InitializeListHead(&DirtySegmentListHead
);
1336 InitializeListHead(&CacheSegmentLRUListHead
);
1337 InitializeListHead(&ClosedListHead
);
1338 KeInitializeGuardedMutex(&ViewLock
);
1339 ExInitializeNPagedLookasideList (&iBcbLookasideList
,
1343 sizeof(INTERNAL_BCB
),
1346 ExInitializeNPagedLookasideList (&BcbLookasideList
,
1353 ExInitializeNPagedLookasideList (&CacheSegLookasideList
,
1357 sizeof(CACHE_SEGMENT
),
1361 MmInitializeMemoryConsumer(MC_CACHE
, CcRosTrimCache
);
1363 CcInitCacheZeroPage();