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 *******************************************************************/
44 static LIST_ENTRY DirtyVacbListHead
;
45 static LIST_ENTRY VacbListHead
;
46 static LIST_ENTRY VacbLruListHead
;
47 static LIST_ENTRY ClosedListHead
;
48 ULONG DirtyPageCount
= 0;
50 KGUARDED_MUTEX ViewLock
;
52 NPAGED_LOOKASIDE_LIST iBcbLookasideList
;
53 static NPAGED_LOOKASIDE_LIST BcbLookasideList
;
54 static NPAGED_LOOKASIDE_LIST VacbLookasideList
;
57 static void CcRosVacbIncRefCount_(PROS_VACB vacb
, const char* file
, int line
)
59 ++vacb
->ReferenceCount
;
62 DbgPrint("(%s:%i) VACB %p ++RefCount=%lu, Dirty %u, PageOut %lu\n",
63 file
, line
, vacb
, vacb
->ReferenceCount
, vacb
->Dirty
, vacb
->PageOut
);
66 static void CcRosVacbDecRefCount_(PROS_VACB vacb
, const char* file
, int line
)
68 --vacb
->ReferenceCount
;
71 DbgPrint("(%s:%i) VACB %p --RefCount=%lu, Dirty %u, PageOut %lu\n",
72 file
, line
, vacb
, vacb
->ReferenceCount
, vacb
->Dirty
, vacb
->PageOut
);
75 #define CcRosVacbIncRefCount(vacb) CcRosVacbIncRefCount_(vacb,__FILE__,__LINE__)
76 #define CcRosVacbDecRefCount(vacb) CcRosVacbDecRefCount_(vacb,__FILE__,__LINE__)
78 #define CcRosVacbIncRefCount(vacb) (++((vacb)->ReferenceCount))
79 #define CcRosVacbDecRefCount(vacb) (--((vacb)->ReferenceCount))
83 CcRosInternalFreeVacb(PROS_VACB Vacb
);
86 /* FUNCTIONS *****************************************************************/
96 PLIST_ENTRY current_entry
;
106 DPRINT1("Enabling Tracing for CacheMap 0x%p:\n", Bcb
);
108 KeAcquireGuardedMutex(&ViewLock
);
109 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
111 current_entry
= Bcb
->BcbVacbListHead
.Flink
;
112 while (current_entry
!= &Bcb
->BcbVacbListHead
)
114 current
= CONTAINING_RECORD(current_entry
, ROS_VACB
, BcbVacbListEntry
);
115 current_entry
= current_entry
->Flink
;
117 DPRINT1(" VACB 0x%p enabled, RefCount %lu, Dirty %u, PageOut %lu\n",
118 current
, current
->ReferenceCount
, current
->Dirty
, current
->PageOut
);
120 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
121 KeReleaseGuardedMutex(&ViewLock
);
125 DPRINT1("Disabling Tracing for CacheMap 0x%p:\n", Bcb
);
142 Status
= CcWriteVirtualAddress(Vacb
);
143 if (NT_SUCCESS(Status
))
145 KeAcquireGuardedMutex(&ViewLock
);
146 KeAcquireSpinLock(&Vacb
->Bcb
->BcbLock
, &oldIrql
);
149 RemoveEntryList(&Vacb
->DirtyVacbListEntry
);
150 DirtyPageCount
-= VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
;
151 CcRosVacbDecRefCount(Vacb
);
153 KeReleaseSpinLock(&Vacb
->Bcb
->BcbLock
, oldIrql
);
154 KeReleaseGuardedMutex(&ViewLock
);
162 CcRosFlushDirtyPages (
167 PLIST_ENTRY current_entry
;
171 LARGE_INTEGER ZeroTimeout
;
173 DPRINT("CcRosFlushDirtyPages(Target %lu)\n", Target
);
176 ZeroTimeout
.QuadPart
= 0;
178 KeEnterCriticalRegion();
179 KeAcquireGuardedMutex(&ViewLock
);
181 current_entry
= DirtyVacbListHead
.Flink
;
182 if (current_entry
== &DirtyVacbListHead
)
184 DPRINT("No Dirty pages\n");
187 while ((current_entry
!= &DirtyVacbListHead
) && (Target
> 0))
189 current
= CONTAINING_RECORD(current_entry
,
192 current_entry
= current_entry
->Flink
;
194 CcRosVacbIncRefCount(current
);
196 Locked
= current
->Bcb
->Callbacks
->AcquireForLazyWrite(
197 current
->Bcb
->LazyWriteContext
, Wait
);
200 CcRosVacbDecRefCount(current
);
204 Status
= KeWaitForSingleObject(¤t
->Mutex
,
208 Wait
? NULL
: &ZeroTimeout
);
209 if (Status
!= STATUS_SUCCESS
)
211 current
->Bcb
->Callbacks
->ReleaseFromLazyWrite(
212 current
->Bcb
->LazyWriteContext
);
213 CcRosVacbDecRefCount(current
);
217 ASSERT(current
->Dirty
);
219 /* One reference is added above */
220 if (current
->ReferenceCount
> 2)
222 KeReleaseMutex(¤t
->Mutex
, FALSE
);
223 current
->Bcb
->Callbacks
->ReleaseFromLazyWrite(
224 current
->Bcb
->LazyWriteContext
);
225 CcRosVacbDecRefCount(current
);
229 KeReleaseGuardedMutex(&ViewLock
);
231 Status
= CcRosFlushVacb(current
);
233 KeReleaseMutex(¤t
->Mutex
, FALSE
);
234 current
->Bcb
->Callbacks
->ReleaseFromLazyWrite(
235 current
->Bcb
->LazyWriteContext
);
237 KeAcquireGuardedMutex(&ViewLock
);
238 CcRosVacbDecRefCount(current
);
240 if (!NT_SUCCESS(Status
) && (Status
!= STATUS_END_OF_FILE
))
242 DPRINT1("CC: Failed to flush VACB.\n");
246 (*Count
) += VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
;
247 Target
-= VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
;
250 current_entry
= DirtyVacbListHead
.Flink
;
253 KeReleaseGuardedMutex(&ViewLock
);
254 KeLeaveCriticalRegion();
256 DPRINT("CcRosFlushDirtyPages() finished\n");
257 return STATUS_SUCCESS
;
266 * FUNCTION: Try to free some memory from the file cache.
268 * Target - The number of pages to be freed.
269 * Priority - The priority of free (currently unused).
270 * NrFreed - Points to a variable where the number of pages
271 * actually freed is returned.
274 PLIST_ENTRY current_entry
;
281 BOOLEAN FlushedPages
= FALSE
;
283 DPRINT("CcRosTrimCache(Target %lu)\n", Target
);
285 InitializeListHead(&FreeList
);
290 KeAcquireGuardedMutex(&ViewLock
);
292 current_entry
= VacbLruListHead
.Flink
;
293 while (current_entry
!= &VacbLruListHead
)
295 current
= CONTAINING_RECORD(current_entry
,
298 current_entry
= current_entry
->Flink
;
300 KeAcquireSpinLock(¤t
->Bcb
->BcbLock
, &oldIrql
);
302 /* Reference the VACB */
303 CcRosVacbIncRefCount(current
);
305 /* Check if it's mapped and not dirty */
306 if (current
->MappedCount
> 0 && !current
->Dirty
)
308 /* We have to break these locks because Cc sucks */
309 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
310 KeReleaseGuardedMutex(&ViewLock
);
312 /* Page out the VACB */
313 for (i
= 0; i
< VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
; i
++)
315 Page
= (PFN_NUMBER
)(MmGetPhysicalAddress((PUCHAR
)current
->BaseAddress
+ (i
* PAGE_SIZE
)).QuadPart
>> PAGE_SHIFT
);
317 MmPageOutPhysicalAddress(Page
);
320 /* Reacquire the locks */
321 KeAcquireGuardedMutex(&ViewLock
);
322 KeAcquireSpinLock(¤t
->Bcb
->BcbLock
, &oldIrql
);
325 /* Dereference the VACB */
326 CcRosVacbDecRefCount(current
);
328 /* Check if we can free this entry now */
329 if (current
->ReferenceCount
== 0)
331 ASSERT(!current
->Dirty
);
332 ASSERT(!current
->MappedCount
);
334 RemoveEntryList(¤t
->BcbVacbListEntry
);
335 RemoveEntryList(¤t
->VacbListEntry
);
336 RemoveEntryList(¤t
->VacbLruListEntry
);
337 InsertHeadList(&FreeList
, ¤t
->BcbVacbListEntry
);
339 /* Calculate how many pages we freed for Mm */
340 PagesFreed
= min(VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
, Target
);
341 Target
-= PagesFreed
;
342 (*NrFreed
) += PagesFreed
;
345 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
348 KeReleaseGuardedMutex(&ViewLock
);
350 /* Try flushing pages if we haven't met our target */
351 if ((Target
> 0) && !FlushedPages
)
353 /* Flush dirty pages to disk */
354 CcRosFlushDirtyPages(Target
, &PagesFreed
, FALSE
);
357 /* We can only swap as many pages as we flushed */
358 if (PagesFreed
< Target
) Target
= PagesFreed
;
360 /* Check if we flushed anything */
363 /* Try again after flushing dirty pages */
364 DPRINT("Flushed %lu dirty cache pages to disk\n", PagesFreed
);
369 while (!IsListEmpty(&FreeList
))
371 current_entry
= RemoveHeadList(&FreeList
);
372 current
= CONTAINING_RECORD(current_entry
,
375 CcRosInternalFreeVacb(current
);
378 DPRINT("Evicted %lu cache pages\n", (*NrFreed
));
380 return STATUS_SUCCESS
;
397 DPRINT("CcRosReleaseVacb(Bcb 0x%p, Vacb 0x%p, Valid %u)\n",
400 KeAcquireGuardedMutex(&ViewLock
);
401 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
405 WasDirty
= Vacb
->Dirty
;
406 Vacb
->Dirty
= Vacb
->Dirty
|| Dirty
;
408 if (!WasDirty
&& Vacb
->Dirty
)
410 InsertTailList(&DirtyVacbListHead
, &Vacb
->DirtyVacbListEntry
);
411 DirtyPageCount
+= VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
;
418 CcRosVacbDecRefCount(Vacb
);
419 if (Mapped
&& (Vacb
->MappedCount
== 1))
421 CcRosVacbIncRefCount(Vacb
);
423 if (!WasDirty
&& Vacb
->Dirty
)
425 CcRosVacbIncRefCount(Vacb
);
428 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
429 KeReleaseGuardedMutex(&ViewLock
);
430 KeReleaseMutex(&Vacb
->Mutex
, FALSE
);
432 return STATUS_SUCCESS
;
435 /* Returns with VACB Lock Held! */
442 PLIST_ENTRY current_entry
;
448 DPRINT("CcRosLookupVacb(Bcb -x%p, FileOffset %lu)\n", Bcb
, FileOffset
);
450 KeAcquireGuardedMutex(&ViewLock
);
451 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
453 current_entry
= Bcb
->BcbVacbListHead
.Flink
;
454 while (current_entry
!= &Bcb
->BcbVacbListHead
)
456 current
= CONTAINING_RECORD(current_entry
,
459 if (IsPointInRange(current
->FileOffset
, VACB_MAPPING_GRANULARITY
,
462 CcRosVacbIncRefCount(current
);
463 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
464 KeReleaseGuardedMutex(&ViewLock
);
465 KeWaitForSingleObject(¤t
->Mutex
,
472 if (current
->FileOffset
> FileOffset
)
474 current_entry
= current_entry
->Flink
;
477 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
478 KeReleaseGuardedMutex(&ViewLock
);
494 DPRINT("CcRosMarkDirtyVacb(Bcb 0x%p, FileOffset %lu)\n", Bcb
, FileOffset
);
496 Vacb
= CcRosLookupVacb(Bcb
, FileOffset
);
499 KeBugCheck(CACHE_MANAGER
);
502 KeAcquireGuardedMutex(&ViewLock
);
503 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
507 InsertTailList(&DirtyVacbListHead
, &Vacb
->DirtyVacbListEntry
);
508 DirtyPageCount
+= VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
;
512 CcRosVacbDecRefCount(Vacb
);
515 /* Move to the tail of the LRU list */
516 RemoveEntryList(&Vacb
->VacbLruListEntry
);
517 InsertTailList(&VacbLruListHead
, &Vacb
->VacbLruListEntry
);
521 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
522 KeReleaseGuardedMutex(&ViewLock
);
523 KeReleaseMutex(&Vacb
->Mutex
, FALSE
);
525 return STATUS_SUCCESS
;
541 DPRINT("CcRosUnmapVacb(Bcb 0x%p, FileOffset %lu, NowDirty %u)\n",
542 Bcb
, FileOffset
, NowDirty
);
544 Vacb
= CcRosLookupVacb(Bcb
, FileOffset
);
547 return STATUS_UNSUCCESSFUL
;
550 KeAcquireGuardedMutex(&ViewLock
);
551 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
553 WasDirty
= Vacb
->Dirty
;
554 Vacb
->Dirty
= Vacb
->Dirty
|| NowDirty
;
558 if (!WasDirty
&& NowDirty
)
560 InsertTailList(&DirtyVacbListHead
, &Vacb
->DirtyVacbListEntry
);
561 DirtyPageCount
+= VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
;
564 CcRosVacbDecRefCount(Vacb
);
565 if (!WasDirty
&& NowDirty
)
567 CcRosVacbIncRefCount(Vacb
);
569 if (Vacb
->MappedCount
== 0)
571 CcRosVacbDecRefCount(Vacb
);
574 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
575 KeReleaseGuardedMutex(&ViewLock
);
576 KeReleaseMutex(&Vacb
->Mutex
, FALSE
);
578 return STATUS_SUCCESS
;
590 PLIST_ENTRY current_entry
;
596 DPRINT("CcRosCreateVacb()\n");
598 if (FileOffset
>= Bcb
->FileSize
.u
.LowPart
)
601 return STATUS_INVALID_PARAMETER
;
604 current
= ExAllocateFromNPagedLookasideList(&VacbLookasideList
);
605 current
->Valid
= FALSE
;
606 current
->Dirty
= FALSE
;
607 current
->PageOut
= FALSE
;
608 current
->FileOffset
= ROUND_DOWN(FileOffset
, VACB_MAPPING_GRANULARITY
);
613 DPRINT1("CacheMap 0x%p: new VACB: 0x%p\n", Bcb
, current
);
616 current
->MappedCount
= 0;
617 current
->DirtyVacbListEntry
.Flink
= NULL
;
618 current
->DirtyVacbListEntry
.Blink
= NULL
;
619 current
->ReferenceCount
= 1;
620 KeInitializeMutex(¤t
->Mutex
, 0);
621 KeWaitForSingleObject(¤t
->Mutex
,
626 KeAcquireGuardedMutex(&ViewLock
);
629 /* There is window between the call to CcRosLookupVacb
630 * and CcRosCreateVacb. We must check if a VACB for the
631 * file offset exist. If there is a VACB, we release
632 * our newly created VACB and return the existing one.
634 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
635 current_entry
= Bcb
->BcbVacbListHead
.Flink
;
637 while (current_entry
!= &Bcb
->BcbVacbListHead
)
639 current
= CONTAINING_RECORD(current_entry
,
642 if (IsPointInRange(current
->FileOffset
, VACB_MAPPING_GRANULARITY
,
645 CcRosVacbIncRefCount(current
);
646 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
650 DPRINT1("CacheMap 0x%p: deleting newly created VACB 0x%p ( found existing one 0x%p )\n",
656 KeReleaseMutex(&(*Vacb
)->Mutex
, FALSE
);
657 KeReleaseGuardedMutex(&ViewLock
);
658 ExFreeToNPagedLookasideList(&VacbLookasideList
, *Vacb
);
660 KeWaitForSingleObject(¤t
->Mutex
,
665 return STATUS_SUCCESS
;
667 if (current
->FileOffset
< FileOffset
)
669 ASSERT(previous
== NULL
||
670 previous
->FileOffset
< current
->FileOffset
);
673 if (current
->FileOffset
> FileOffset
)
675 current_entry
= current_entry
->Flink
;
677 /* There was no existing VACB. */
681 InsertHeadList(&previous
->BcbVacbListEntry
, ¤t
->BcbVacbListEntry
);
685 InsertHeadList(&Bcb
->BcbVacbListHead
, ¤t
->BcbVacbListEntry
);
687 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
688 InsertTailList(&VacbListHead
, ¤t
->VacbListEntry
);
689 InsertTailList(&VacbLruListHead
, ¤t
->VacbLruListEntry
);
690 KeReleaseGuardedMutex(&ViewLock
);
692 MmLockAddressSpace(MmGetKernelAddressSpace());
693 current
->BaseAddress
= NULL
;
694 Status
= MmCreateMemoryArea(MmGetKernelAddressSpace(),
695 0, // nothing checks for VACB mareas, so set to 0
696 ¤t
->BaseAddress
,
697 VACB_MAPPING_GRANULARITY
,
699 (PMEMORY_AREA
*)¤t
->MemoryArea
,
703 MmUnlockAddressSpace(MmGetKernelAddressSpace());
704 if (!NT_SUCCESS(Status
))
706 KeBugCheck(CACHE_MANAGER
);
709 /* Create a virtual mapping for this memory area */
710 MI_SET_USAGE(MI_USAGE_CACHE
);
714 if ((Bcb
->FileObject
) && (Bcb
->FileObject
->FileName
.Buffer
))
716 pos
= wcsrchr(Bcb
->FileObject
->FileName
.Buffer
, '\\');
717 len
= wcslen(pos
) * sizeof(WCHAR
);
718 if (pos
) snprintf(MI_PFN_CURRENT_PROCESS_NAME
, min(16, len
), "%S", pos
);
722 MmMapMemoryArea(current
->BaseAddress
, VACB_MAPPING_GRANULARITY
,
723 MC_CACHE
, PAGE_READWRITE
);
725 return STATUS_SUCCESS
;
739 PROS_VACB Previous
= NULL
;
743 DPRINT("CcRosGetVacbChain()\n");
745 Length
= ROUND_UP(Length
, VACB_MAPPING_GRANULARITY
);
747 VacbList
= _alloca(sizeof(PROS_VACB
) *
748 (Length
/ VACB_MAPPING_GRANULARITY
));
751 * Look for a VACB already mapping the same data.
753 for (i
= 0; i
< (Length
/ VACB_MAPPING_GRANULARITY
); i
++)
755 ULONG CurrentOffset
= FileOffset
+ (i
* VACB_MAPPING_GRANULARITY
);
756 current
= CcRosLookupVacb(Bcb
, CurrentOffset
);
759 KeAcquireGuardedMutex(&ViewLock
);
761 /* Move to tail of LRU list */
762 RemoveEntryList(¤t
->VacbLruListEntry
);
763 InsertTailList(&VacbLruListHead
, ¤t
->VacbLruListEntry
);
765 KeReleaseGuardedMutex(&ViewLock
);
767 VacbList
[i
] = current
;
771 CcRosCreateVacb(Bcb
, CurrentOffset
, ¤t
);
772 VacbList
[i
] = current
;
776 for (i
= 0; i
< Length
/ VACB_MAPPING_GRANULARITY
; i
++)
781 Previous
= VacbList
[i
];
785 Previous
->NextInChain
= VacbList
[i
];
786 Previous
= VacbList
[i
];
790 Previous
->NextInChain
= NULL
;
792 return STATUS_SUCCESS
;
810 DPRINT("CcRosGetVacb()\n");
813 * Look for a VACB already mapping the same data.
815 current
= CcRosLookupVacb(Bcb
, FileOffset
);
819 * Otherwise create a new VACB.
821 Status
= CcRosCreateVacb(Bcb
, FileOffset
, ¤t
);
822 if (!NT_SUCCESS(Status
))
828 KeAcquireGuardedMutex(&ViewLock
);
830 /* Move to the tail of the LRU list */
831 RemoveEntryList(¤t
->VacbLruListEntry
);
832 InsertTailList(&VacbLruListHead
, ¤t
->VacbLruListEntry
);
834 KeReleaseGuardedMutex(&ViewLock
);
837 * Return information about the VACB to the caller.
839 *UptoDate
= current
->Valid
;
840 *BaseAddress
= current
->BaseAddress
;
841 DPRINT("*BaseAddress %p\n", *BaseAddress
);
843 *BaseOffset
= current
->FileOffset
;
844 return STATUS_SUCCESS
;
856 * FUNCTION: Request a page mapping for a BCB
863 if (FileOffset
% VACB_MAPPING_GRANULARITY
!= 0)
865 DPRINT1("Bad fileoffset %x should be multiple of %x",
866 FileOffset
, VACB_MAPPING_GRANULARITY
);
867 KeBugCheck(CACHE_MANAGER
);
870 return CcRosGetVacb(Bcb
,
882 MEMORY_AREA
* MemoryArea
,
888 ASSERT(SwapEntry
== 0);
891 ASSERT(MmGetReferenceCountPage(Page
) == 1);
892 MmReleasePageMemoryConsumer(MC_CACHE
, Page
);
897 CcRosInternalFreeVacb (
900 * FUNCTION: Releases a VACB associated with a BCB
903 DPRINT("Freeing VACB 0x%p\n", Vacb
);
905 if (Vacb
->Bcb
->Trace
)
907 DPRINT1("CacheMap 0x%p: deleting VACB: 0x%p\n", Vacb
->Bcb
, Vacb
);
911 MmLockAddressSpace(MmGetKernelAddressSpace());
912 MmFreeMemoryArea(MmGetKernelAddressSpace(),
916 MmUnlockAddressSpace(MmGetKernelAddressSpace());
918 ExFreeToNPagedLookasideList(&VacbLookasideList
, Vacb
);
919 return STATUS_SUCCESS
;
928 IN PSECTION_OBJECT_POINTERS SectionObjectPointers
,
929 IN PLARGE_INTEGER FileOffset OPTIONAL
,
931 OUT PIO_STATUS_BLOCK IoStatus
)
934 LARGE_INTEGER Offset
;
939 DPRINT("CcFlushCache(SectionObjectPointers 0x%p, FileOffset 0x%p, Length %lu, IoStatus 0x%p)\n",
940 SectionObjectPointers
, FileOffset
, Length
, IoStatus
);
942 if (SectionObjectPointers
&& SectionObjectPointers
->SharedCacheMap
)
944 Bcb
= (PBCB
)SectionObjectPointers
->SharedCacheMap
;
948 Offset
= *FileOffset
;
952 Offset
.QuadPart
= (LONGLONG
)0;
953 Length
= Bcb
->FileSize
.u
.LowPart
;
958 IoStatus
->Status
= STATUS_SUCCESS
;
959 IoStatus
->Information
= 0;
964 current
= CcRosLookupVacb(Bcb
, Offset
.u
.LowPart
);
969 Status
= CcRosFlushVacb(current
);
970 if (!NT_SUCCESS(Status
) && IoStatus
!= NULL
)
972 IoStatus
->Status
= Status
;
975 KeReleaseMutex(¤t
->Mutex
, FALSE
);
977 KeAcquireGuardedMutex(&ViewLock
);
978 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
979 CcRosVacbDecRefCount(current
);
980 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
981 KeReleaseGuardedMutex(&ViewLock
);
984 Offset
.QuadPart
+= VACB_MAPPING_GRANULARITY
;
985 if (Length
> VACB_MAPPING_GRANULARITY
)
987 Length
-= VACB_MAPPING_GRANULARITY
;
999 IoStatus
->Status
= STATUS_INVALID_PARAMETER
;
1006 CcRosDeleteFileCache (
1007 PFILE_OBJECT FileObject
,
1010 * FUNCTION: Releases the BCB associated with a file object
1013 PLIST_ENTRY current_entry
;
1015 LIST_ENTRY FreeList
;
1021 KeReleaseGuardedMutex(&ViewLock
);
1023 CcFlushCache(FileObject
->SectionObjectPointer
, NULL
, 0, NULL
);
1025 KeAcquireGuardedMutex(&ViewLock
);
1027 if (Bcb
->RefCount
== 0)
1029 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1031 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1032 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1035 FileObject
->SectionObjectPointer
->SharedCacheMap
= NULL
;
1040 InitializeListHead(&FreeList
);
1041 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
1042 while (!IsListEmpty(&Bcb
->BcbVacbListHead
))
1044 current_entry
= RemoveTailList(&Bcb
->BcbVacbListHead
);
1045 current
= CONTAINING_RECORD(current_entry
, ROS_VACB
, BcbVacbListEntry
);
1046 RemoveEntryList(¤t
->VacbListEntry
);
1047 RemoveEntryList(¤t
->VacbLruListEntry
);
1050 RemoveEntryList(¤t
->DirtyVacbListEntry
);
1051 DirtyPageCount
-= VACB_MAPPING_GRANULARITY
/ PAGE_SIZE
;
1052 DPRINT1("Freeing dirty VACB\n");
1054 InsertHeadList(&FreeList
, ¤t
->BcbVacbListEntry
);
1059 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
1061 KeReleaseGuardedMutex(&ViewLock
);
1062 ObDereferenceObject (Bcb
->FileObject
);
1064 while (!IsListEmpty(&FreeList
))
1066 current_entry
= RemoveTailList(&FreeList
);
1067 current
= CONTAINING_RECORD(current_entry
, ROS_VACB
, BcbVacbListEntry
);
1068 CcRosInternalFreeVacb(current
);
1070 ExFreeToNPagedLookasideList(&BcbLookasideList
, Bcb
);
1071 KeAcquireGuardedMutex(&ViewLock
);
1073 return STATUS_SUCCESS
;
1078 CcRosReferenceCache (
1079 PFILE_OBJECT FileObject
)
1082 KeAcquireGuardedMutex(&ViewLock
);
1083 Bcb
= (PBCB
)FileObject
->SectionObjectPointer
->SharedCacheMap
;
1085 if (Bcb
->RefCount
== 0)
1087 ASSERT(Bcb
->BcbRemoveListEntry
.Flink
!= NULL
);
1088 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1089 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1094 ASSERT(Bcb
->BcbRemoveListEntry
.Flink
== NULL
);
1097 KeReleaseGuardedMutex(&ViewLock
);
1102 CcRosSetRemoveOnClose (
1103 PSECTION_OBJECT_POINTERS SectionObjectPointer
)
1106 DPRINT("CcRosSetRemoveOnClose()\n");
1107 KeAcquireGuardedMutex(&ViewLock
);
1108 Bcb
= (PBCB
)SectionObjectPointer
->SharedCacheMap
;
1111 Bcb
->RemoveOnClose
= TRUE
;
1112 if (Bcb
->RefCount
== 0)
1114 CcRosDeleteFileCache(Bcb
->FileObject
, Bcb
);
1117 KeReleaseGuardedMutex(&ViewLock
);
1123 CcRosDereferenceCache (
1124 PFILE_OBJECT FileObject
)
1127 KeAcquireGuardedMutex(&ViewLock
);
1128 Bcb
= (PBCB
)FileObject
->SectionObjectPointer
->SharedCacheMap
;
1130 if (Bcb
->RefCount
> 0)
1133 if (Bcb
->RefCount
== 0)
1135 MmFreeSectionSegments(Bcb
->FileObject
);
1136 CcRosDeleteFileCache(FileObject
, Bcb
);
1139 KeReleaseGuardedMutex(&ViewLock
);
1144 CcRosReleaseFileCache (
1145 PFILE_OBJECT FileObject
)
1147 * FUNCTION: Called by the file system when a handle to a file object
1153 KeAcquireGuardedMutex(&ViewLock
);
1155 if (FileObject
->SectionObjectPointer
->SharedCacheMap
!= NULL
)
1157 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1158 if (FileObject
->PrivateCacheMap
!= NULL
)
1160 FileObject
->PrivateCacheMap
= NULL
;
1161 if (Bcb
->RefCount
> 0)
1164 if (Bcb
->RefCount
== 0)
1166 MmFreeSectionSegments(Bcb
->FileObject
);
1167 CcRosDeleteFileCache(FileObject
, Bcb
);
1172 KeReleaseGuardedMutex(&ViewLock
);
1173 return STATUS_SUCCESS
;
1178 CcTryToInitializeFileCache (
1179 PFILE_OBJECT FileObject
)
1184 KeAcquireGuardedMutex(&ViewLock
);
1186 ASSERT(FileObject
->SectionObjectPointer
);
1187 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1190 Status
= STATUS_UNSUCCESSFUL
;
1194 if (FileObject
->PrivateCacheMap
== NULL
)
1196 FileObject
->PrivateCacheMap
= Bcb
;
1199 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1201 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1202 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1204 Status
= STATUS_SUCCESS
;
1206 KeReleaseGuardedMutex(&ViewLock
);
1214 CcRosInitializeFileCache (
1215 PFILE_OBJECT FileObject
,
1216 PCACHE_MANAGER_CALLBACKS CallBacks
,
1217 PVOID LazyWriterContext
)
1219 * FUNCTION: Initializes a BCB for a file object
1224 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1225 DPRINT("CcRosInitializeFileCache(FileObject 0x%p, Bcb 0x%p)\n",
1228 KeAcquireGuardedMutex(&ViewLock
);
1231 Bcb
= ExAllocateFromNPagedLookasideList(&BcbLookasideList
);
1234 KeReleaseGuardedMutex(&ViewLock
);
1235 return STATUS_UNSUCCESSFUL
;
1237 RtlZeroMemory(Bcb
, sizeof(*Bcb
));
1238 ObReferenceObjectByPointer(FileObject
,
1242 Bcb
->FileObject
= FileObject
;
1243 Bcb
->Callbacks
= CallBacks
;
1244 Bcb
->LazyWriteContext
= LazyWriterContext
;
1245 if (FileObject
->FsContext
)
1247 Bcb
->AllocationSize
=
1248 ((PFSRTL_COMMON_FCB_HEADER
)FileObject
->FsContext
)->AllocationSize
;
1250 ((PFSRTL_COMMON_FCB_HEADER
)FileObject
->FsContext
)->FileSize
;
1252 KeInitializeSpinLock(&Bcb
->BcbLock
);
1253 InitializeListHead(&Bcb
->BcbVacbListHead
);
1254 FileObject
->SectionObjectPointer
->SharedCacheMap
= Bcb
;
1256 if (FileObject
->PrivateCacheMap
== NULL
)
1258 FileObject
->PrivateCacheMap
= Bcb
;
1261 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1263 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1264 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1266 KeReleaseGuardedMutex(&ViewLock
);
1268 return STATUS_SUCCESS
;
1276 CcGetFileObjectFromSectionPtrs (
1277 IN PSECTION_OBJECT_POINTERS SectionObjectPointers
)
1280 if (SectionObjectPointers
&& SectionObjectPointers
->SharedCacheMap
)
1282 Bcb
= (PBCB
)SectionObjectPointers
->SharedCacheMap
;
1284 return Bcb
->FileObject
;
1295 DPRINT("CcInitView()\n");
1297 InitializeListHead(&VacbListHead
);
1298 InitializeListHead(&DirtyVacbListHead
);
1299 InitializeListHead(&VacbLruListHead
);
1300 InitializeListHead(&ClosedListHead
);
1301 KeInitializeGuardedMutex(&ViewLock
);
1302 ExInitializeNPagedLookasideList (&iBcbLookasideList
,
1306 sizeof(INTERNAL_BCB
),
1309 ExInitializeNPagedLookasideList (&BcbLookasideList
,
1316 ExInitializeNPagedLookasideList (&VacbLookasideList
,
1324 MmInitializeMemoryConsumer(MC_CACHE
, CcRosTrimCache
);
1326 CcInitCacheZeroPage();