3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: view.c,v 1.57 2003/02/13 22:24:18 hbirr Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/cc/view.c
23 * PURPOSE: Cache manager
24 * PROGRAMMER: David Welch (welch@mcmail.com)
25 * PORTABILITY: Checked
30 /* NOTES **********************************************************************
32 * This is not the NT implementation of a file cache nor anything much like
35 * The general procedure for a filesystem to implement a read or write
36 * dispatch routine is as follows
38 * (1) If caching for the FCB hasn't been initiated then so do by calling
39 * CcInitializeFileCache.
41 * (2) For each 4k region which is being read or written obtain a cache page
42 * by calling CcRequestCachePage.
44 * (3) If either the page is being read or not completely written, and it is
45 * not up to date then read its data from the underlying medium. If the read
46 * fails then call CcReleaseCachePage with VALID as FALSE and return a error.
48 * (4) Copy the data into or out of the page as necessary.
50 * (5) Release the cache page
52 /* INCLUDES ******************************************************************/
54 #include <ddk/ntddk.h>
55 #include <ddk/ntifs.h>
56 #include <internal/mm.h>
57 #include <internal/cc.h>
58 #include <internal/pool.h>
59 #include <ntos/minmax.h>
62 #include <internal/debug.h>
64 /* GLOBALS *******************************************************************/
67 * If CACHE_BITMAP is defined, the cache manager uses one large memory region
68 * within the kernel address space and allocate/deallocate space from this block
69 * over a bitmap. If CACHE_BITMAP is used, the size of the mdl mapping region
70 * must be reduced (ntoskrnl\mm\mdl.c, MI_MDLMAPPING_REGION_SIZE).
72 //#define CACHE_BITMAP
74 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
75 #define ROUND_DOWN(N, S) (((N) % (S)) ? ROUND_UP(N, S) - S : N)
77 #define TAG_CSEG TAG('C', 'S', 'E', 'G')
78 #define TAG_BCB TAG('B', 'C', 'B', ' ')
79 #define TAG_IBCB TAG('i', 'B', 'C', 'B')
81 static LIST_ENTRY DirtySegmentListHead
;
82 static LIST_ENTRY CacheSegmentListHead
;
83 static LIST_ENTRY CacheSegmentLRUListHead
;
84 static LIST_ENTRY ClosedListHead
;
85 ULONG DirtyPageCount
=0;
90 #define CI_CACHESEG_MAPPING_REGION_SIZE (128*1024*1024)
92 static PVOID CiCacheSegMappingRegionBase
= NULL
;
93 static RTL_BITMAP CiCacheSegMappingRegionAllocMap
;
94 static ULONG CiCacheSegMappingRegionHint
;
95 static KSPIN_LOCK CiCacheSegMappingRegionLock
;
98 NPAGED_LOOKASIDE_LIST iBcbLookasideList
;
99 static NPAGED_LOOKASIDE_LIST BcbLookasideList
;
100 static NPAGED_LOOKASIDE_LIST CacheSegLookasideList
;
102 static ULONG CcTimeStamp
;
103 static KEVENT LazyCloseThreadEvent
;
104 static HANDLE LazyCloseThreadHandle
;
105 static CLIENT_ID LazyCloseThreadId
;
106 static volatile BOOLEAN LazyCloseThreadShouldTerminate
;
108 void * alloca(size_t size
);
111 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg
);
113 /* FUNCTIONS *****************************************************************/
116 CcRosFlushCacheSegment(PCACHE_SEGMENT CacheSegment
)
120 Status
= WriteCacheSegment(CacheSegment
);
121 if (NT_SUCCESS(Status
))
123 ExAcquireFastMutex(&ViewLock
);
124 KeAcquireSpinLock(&CacheSegment
->Bcb
->BcbLock
, &oldIrql
);
125 CacheSegment
->Dirty
= FALSE
;
126 RemoveEntryList(&CacheSegment
->DirtySegmentListEntry
);
127 DirtyPageCount
-= CacheSegment
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
128 CacheSegment
->ReferenceCount
--;
129 KeReleaseSpinLock(&CacheSegment
->Bcb
->BcbLock
, oldIrql
);
130 ExReleaseFastMutex(&ViewLock
);
135 VOID
CcRosRemoveUnusedFiles(VOID
);
139 CcRosFlushDirtyPages(ULONG Target
, PULONG Count
)
141 PLIST_ENTRY current_entry
;
142 PCACHE_SEGMENT current
;
143 ULONG PagesPerSegment
;
146 static ULONG WriteCount
[4] = {0, 0, 0, 0};
149 DPRINT("CcRosFlushDirtyPages(Target %d)\n", Target
);
153 ExAcquireFastMutex(&ViewLock
);
155 WriteCount
[0] = WriteCount
[1];
156 WriteCount
[1] = WriteCount
[2];
157 WriteCount
[2] = WriteCount
[3];
160 NewTarget
= WriteCount
[0] + WriteCount
[1] + WriteCount
[2];
162 if (NewTarget
< DirtyPageCount
)
164 NewTarget
= (DirtyPageCount
- NewTarget
+ 3) / 4;
165 WriteCount
[0] += NewTarget
;
166 WriteCount
[1] += NewTarget
;
167 WriteCount
[2] += NewTarget
;
168 WriteCount
[3] += NewTarget
;
171 NewTarget
= WriteCount
[0];
173 Target
= max(NewTarget
, Target
);
175 current_entry
= DirtySegmentListHead
.Flink
;
176 if (current_entry
== &DirtySegmentListHead
)
178 DPRINT("No Dirty pages\n");
180 while (current_entry
!= &DirtySegmentListHead
&& Target
> 0)
182 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
183 DirtySegmentListEntry
);
184 current_entry
= current_entry
->Flink
;
185 Locked
= ExTryToAcquireFastMutex(¤t
->Lock
);
190 assert(current
->Dirty
);
191 if (current
->ReferenceCount
> 1)
193 ExReleaseFastMutex(¤t
->Lock
);
196 ExReleaseFastMutex(&ViewLock
);
197 PagesPerSegment
= current
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
198 Status
= CcRosFlushCacheSegment(current
);
199 ExReleaseFastMutex(¤t
->Lock
);
200 if (!NT_SUCCESS(Status
))
202 DPRINT1("CC: Failed to flush cache segment.\n");
206 (*Count
) += PagesPerSegment
;
207 Target
-= PagesPerSegment
;
209 ExAcquireFastMutex(&ViewLock
);
210 current_entry
= DirtySegmentListHead
.Flink
;
212 if (*Count
< NewTarget
)
214 WriteCount
[1] += (NewTarget
- *Count
);
216 ExReleaseFastMutex(&ViewLock
);
217 DPRINT("CcRosFlushDirtyPages() finished\n");
219 return(STATUS_SUCCESS
);
223 CcRosTrimCache(ULONG Target
, ULONG Priority
, PULONG NrFreed
)
225 * FUNCTION: Try to free some memory from the file cache.
227 * Target - The number of pages to be freed.
228 * Priority - The priority of free (currently unused).
229 * NrFreed - Points to a variable where the number of pages
230 * actually freed is returned.
233 PLIST_ENTRY current_entry
;
234 PCACHE_SEGMENT current
;
235 ULONG PagesPerSegment
;
240 DPRINT("CcRosTrimCache(Target %d)\n", Target
);
244 InitializeListHead(&FreeList
);
246 ExAcquireFastMutex(&ViewLock
);
247 current_entry
= CacheSegmentLRUListHead
.Flink
;
248 while (current_entry
!= &CacheSegmentLRUListHead
&& Target
> 0)
250 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
251 CacheSegmentLRUListEntry
);
252 current_entry
= current_entry
->Flink
;
254 KeAcquireSpinLock(¤t
->Bcb
->BcbLock
, &oldIrql
);
255 if (current
->ReferenceCount
== 0)
257 RemoveEntryList(¤t
->BcbSegmentListEntry
);
258 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
259 RemoveEntryList(¤t
->CacheSegmentListEntry
);
260 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
261 InsertHeadList(&FreeList
, ¤t
->BcbSegmentListEntry
);
265 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
268 ExReleaseFastMutex(&ViewLock
);
270 while (!IsListEmpty(&FreeList
))
272 current_entry
= RemoveHeadList(&FreeList
);
273 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
274 BcbSegmentListEntry
);
275 PagesPerSegment
= current
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
276 PagesFreed
= min(PagesPerSegment
, Target
);
277 Target
-= PagesFreed
;
278 (*NrFreed
) += PagesFreed
;
279 CcRosInternalFreeCacheSegment(current
);
282 DPRINT("CcRosTrimCache() finished\n");
283 return(STATUS_SUCCESS
);
287 CcRosReleaseCacheSegment(PBCB Bcb
,
288 PCACHE_SEGMENT CacheSeg
,
293 BOOLEAN WasDirty
= CacheSeg
->Dirty
;
298 DPRINT("CcReleaseCacheSegment(Bcb %x, CacheSeg %x, Valid %d)\n",
299 Bcb
, CacheSeg
, Valid
);
301 CacheSeg
->Valid
= Valid
;
302 CacheSeg
->Dirty
= CacheSeg
->Dirty
|| Dirty
;
304 ExAcquireFastMutex(&ViewLock
);
305 if (!WasDirty
&& CacheSeg
->Dirty
)
307 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
308 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
310 RemoveEntryList(&CacheSeg
->CacheSegmentLRUListEntry
);
311 InsertTailList(&CacheSegmentLRUListHead
, &CacheSeg
->CacheSegmentLRUListEntry
);
315 CacheSeg
->MappedCount
++;
317 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
318 CacheSeg
->ReferenceCount
--;
319 if (Mapped
&& CacheSeg
->MappedCount
== 1)
321 CacheSeg
->ReferenceCount
++;
323 if (!WasDirty
&& CacheSeg
->Dirty
)
325 CacheSeg
->ReferenceCount
++;
327 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
328 ExReleaseFastMutex(&ViewLock
);
329 ExReleaseFastMutex(&CacheSeg
->Lock
);
331 return(STATUS_SUCCESS
);
335 CcRosLookupCacheSegment(PBCB Bcb
, ULONG FileOffset
)
337 PLIST_ENTRY current_entry
;
338 PCACHE_SEGMENT current
;
343 DPRINT("CcRosLookupCacheSegment(Bcb %x, FileOffset %d)\n", Bcb
, FileOffset
);
345 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
346 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
347 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
349 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
350 BcbSegmentListEntry
);
351 if (current
->FileOffset
<= FileOffset
&&
352 (current
->FileOffset
+ Bcb
->CacheSegmentSize
) > FileOffset
)
354 current
->ReferenceCount
++;
355 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
358 current_entry
= current_entry
->Flink
;
360 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
365 CcRosMarkDirtyCacheSegment(PBCB Bcb
, ULONG FileOffset
)
367 PCACHE_SEGMENT CacheSeg
;
372 DPRINT("CcRosMarkDirtyCacheSegment(Bcb %x, FileOffset %d)\n", Bcb
, FileOffset
);
374 CacheSeg
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
375 if (CacheSeg
== NULL
)
379 ExAcquireFastMutex(&CacheSeg
->Lock
);
380 if (!CacheSeg
->Dirty
)
382 ExAcquireFastMutex(&ViewLock
);
383 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
384 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
385 ExReleaseFastMutex(&ViewLock
);
389 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
390 CacheSeg
->ReferenceCount
--;
391 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
395 CacheSeg
->Dirty
= TRUE
;
396 ExReleaseFastMutex(&CacheSeg
->Lock
);
398 return(STATUS_SUCCESS
);
402 CcRosUnmapCacheSegment(PBCB Bcb
, ULONG FileOffset
, BOOLEAN NowDirty
)
404 PCACHE_SEGMENT CacheSeg
;
410 DPRINT("CcRosUnmapCacheSegment(Bcb %x, FileOffset %d, NowDirty %d)\n",
411 Bcb
, FileOffset
, NowDirty
);
413 CacheSeg
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
414 if (CacheSeg
== NULL
)
416 return(STATUS_UNSUCCESSFUL
);
418 ExAcquireFastMutex(&CacheSeg
->Lock
);
420 WasDirty
= CacheSeg
->Dirty
;
421 CacheSeg
->Dirty
= CacheSeg
->Dirty
|| NowDirty
;
423 CacheSeg
->MappedCount
--;
425 if (!WasDirty
&& NowDirty
)
427 ExAcquireFastMutex(&ViewLock
);
428 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
429 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
430 ExReleaseFastMutex(&ViewLock
);
433 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
434 CacheSeg
->ReferenceCount
--;
435 if (!WasDirty
&& NowDirty
)
437 CacheSeg
->ReferenceCount
++;
439 if (CacheSeg
->MappedCount
== 0)
441 CacheSeg
->ReferenceCount
--;
443 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
445 ExReleaseFastMutex(&CacheSeg
->Lock
);
446 return(STATUS_SUCCESS
);
450 CcRosCreateCacheSegment(PBCB Bcb
,
452 PCACHE_SEGMENT
* CacheSeg
,
456 PCACHE_SEGMENT current
;
457 PLIST_ENTRY current_entry
;
461 ULONG StartingOffset
;
466 DPRINT("CcRosCreateCacheSegment()\n");
468 current
= ExAllocateFromNPagedLookasideList(&CacheSegLookasideList
);
469 current
->Valid
= FALSE
;
470 current
->Dirty
= FALSE
;
471 current
->FileOffset
= ROUND_DOWN(FileOffset
, Bcb
->CacheSegmentSize
);
473 current
->MappedCount
= 0;
474 current
->DirtySegmentListEntry
.Flink
= NULL
;
475 current
->DirtySegmentListEntry
.Blink
= NULL
;
476 current
->ReferenceCount
= 1;
477 ExInitializeFastMutex(¤t
->Lock
);
478 ExAcquireFastMutex(¤t
->Lock
);
479 ExAcquireFastMutex(&ViewLock
);
482 /* There is window between the call to CcRosLookupCacheSegment
483 * and CcRosCreateCacheSegment. We must check if a segment on
484 * the fileoffset exist. If there exist a segment, we release
485 * our new created segment and return the existing one.
487 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
488 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
489 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
491 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
492 BcbSegmentListEntry
);
493 if (current
->FileOffset
<= FileOffset
&&
494 (current
->FileOffset
+ Bcb
->CacheSegmentSize
) > FileOffset
)
496 current
->ReferenceCount
++;
497 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
498 ExReleaseFastMutex(&(*CacheSeg
)->Lock
);
499 ExReleaseFastMutex(&ViewLock
);
500 ExFreeToNPagedLookasideList(&CacheSegLookasideList
, *CacheSeg
);
504 ExAcquireFastMutex(¤t
->Lock
);
506 return STATUS_SUCCESS
;
508 current_entry
= current_entry
->Flink
;
510 /* There was no existing segment. */
512 InsertTailList(&Bcb
->BcbSegmentListHead
, ¤t
->BcbSegmentListEntry
);
513 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
514 InsertTailList(&CacheSegmentListHead
, ¤t
->CacheSegmentListEntry
);
515 InsertTailList(&CacheSegmentLRUListHead
, ¤t
->CacheSegmentLRUListEntry
);
516 ExReleaseFastMutex(&ViewLock
);
518 KeAcquireSpinLock(&CiCacheSegMappingRegionLock
, &oldIrql
);
520 StartingOffset
= RtlFindClearBitsAndSet(&CiCacheSegMappingRegionAllocMap
, Bcb
->CacheSegmentSize
/ PAGE_SIZE
, CiCacheSegMappingRegionHint
);
522 if (StartingOffset
== 0xffffffff)
524 DPRINT1("Out of CacheSeg mapping space\n");
528 current
->BaseAddress
= CiCacheSegMappingRegionBase
+ StartingOffset
* PAGE_SIZE
;
530 if (CiCacheSegMappingRegionHint
== StartingOffset
)
532 CiCacheSegMappingRegionHint
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
535 KeReleaseSpinLock(&CiCacheSegMappingRegionLock
, oldIrql
);
537 MmLockAddressSpace(MmGetKernelAddressSpace());
538 current
->BaseAddress
= NULL
;
539 Status
= MmCreateMemoryArea(NULL
,
540 MmGetKernelAddressSpace(),
541 MEMORY_AREA_CACHE_SEGMENT
,
542 ¤t
->BaseAddress
,
543 Bcb
->CacheSegmentSize
,
545 (PMEMORY_AREA
*)¤t
->MemoryArea
,
547 MmUnlockAddressSpace(MmGetKernelAddressSpace());
548 if (!NT_SUCCESS(Status
))
553 for (i
= 0; i
< (Bcb
->CacheSegmentSize
/ PAGE_SIZE
); i
++)
555 PHYSICAL_ADDRESS Page
;
557 Status
= MmRequestPageMemoryConsumer(MC_CACHE
, TRUE
, &Page
);
558 if (!NT_SUCCESS(Status
))
563 Status
= MmCreateVirtualMapping(NULL
,
564 current
->BaseAddress
+ (i
* PAGE_SIZE
),
568 if (!NT_SUCCESS(Status
))
575 ExReleaseFastMutex(¤t
->Lock
);
578 return(STATUS_SUCCESS
);
582 CcRosGetCacheSegmentChain(PBCB Bcb
,
585 PCACHE_SEGMENT
* CacheSeg
)
587 PCACHE_SEGMENT current
;
589 PCACHE_SEGMENT
* CacheSegList
;
590 PCACHE_SEGMENT Previous
= NULL
;
594 DPRINT("CcRosGetCacheSegmentChain()\n");
596 Length
= ROUND_UP(Length
, Bcb
->CacheSegmentSize
);
598 CacheSegList
= alloca(sizeof(PCACHE_SEGMENT
) *
599 (Length
/ Bcb
->CacheSegmentSize
));
602 * Look for a cache segment already mapping the same data.
604 for (i
= 0; i
< (Length
/ Bcb
->CacheSegmentSize
); i
++)
606 ULONG CurrentOffset
= FileOffset
+ (i
* Bcb
->CacheSegmentSize
);
607 current
= CcRosLookupCacheSegment(Bcb
, CurrentOffset
);
610 CacheSegList
[i
] = current
;
614 CcRosCreateCacheSegment(Bcb
, CurrentOffset
, ¤t
, FALSE
);
615 CacheSegList
[i
] = current
;
619 for (i
= 0; i
< (Length
/ Bcb
->CacheSegmentSize
); i
++)
621 ExAcquireFastMutex(&CacheSegList
[i
]->Lock
);
624 *CacheSeg
= CacheSegList
[i
];
625 Previous
= CacheSegList
[i
];
629 Previous
->NextInChain
= CacheSegList
[i
];
630 Previous
= CacheSegList
[i
];
633 Previous
->NextInChain
= NULL
;
635 return(STATUS_SUCCESS
);
639 CcRosGetCacheSegment(PBCB Bcb
,
644 PCACHE_SEGMENT
* CacheSeg
)
646 PCACHE_SEGMENT current
;
651 DPRINT("CcRosGetCacheSegment()\n");
654 * Look for a cache segment already mapping the same data.
656 current
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
659 ExAcquireFastMutex(¤t
->Lock
);
664 * Otherwise create a new segment.
666 Status
= CcRosCreateCacheSegment(Bcb
, FileOffset
, ¤t
, TRUE
);
669 * Return information about the segment to the caller.
671 *UptoDate
= current
->Valid
;
672 *BaseAddress
= current
->BaseAddress
;
673 DPRINT("*BaseAddress 0x%.8X\n", *BaseAddress
);
675 *BaseOffset
= current
->FileOffset
;
676 return(STATUS_SUCCESS
);
680 CcRosRequestCacheSegment(PBCB Bcb
,
684 PCACHE_SEGMENT
* CacheSeg
)
686 * FUNCTION: Request a page mapping for a BCB
693 if ((FileOffset
% Bcb
->CacheSegmentSize
) != 0)
695 CPRINT("Bad fileoffset %x should be multiple of %x",
696 FileOffset
, Bcb
->CacheSegmentSize
);
700 return(CcRosGetCacheSegment(Bcb
,
710 CcFreeCachePage(PVOID Context
, MEMORY_AREA
* MemoryArea
, PVOID Address
,
711 PHYSICAL_ADDRESS PhysAddr
, SWAPENTRY SwapEntry
, BOOLEAN Dirty
)
713 assert(SwapEntry
== 0);
714 if (PhysAddr
.QuadPart
!= 0)
716 MmReleasePageMemoryConsumer(MC_CACHE
, PhysAddr
);
721 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg
)
723 * FUNCTION: Releases a cache segment associated with a BCB
730 PHYSICAL_ADDRESS PhysicalAddr
;
733 DPRINT("Freeing cache segment %x\n", CacheSeg
);
735 RegionSize
= CacheSeg
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
737 /* Unmap all the pages. */
738 for (i
= 0; i
< RegionSize
; i
++)
740 MmDeleteVirtualMapping(NULL
,
741 CacheSeg
->BaseAddress
+ (i
* PAGE_SIZE
),
745 MmReleasePageMemoryConsumer(MC_CACHE
, PhysicalAddr
);
748 KeAcquireSpinLock(&CiCacheSegMappingRegionLock
, &oldIrql
);
749 /* Deallocate all the pages used. */
750 Base
= (ULONG
)(CacheSeg
->BaseAddress
- CiCacheSegMappingRegionBase
) / PAGE_SIZE
;
752 RtlClearBits(&CiCacheSegMappingRegionAllocMap
, Base
, RegionSize
);
754 CiCacheSegMappingRegionHint
= min (CiCacheSegMappingRegionHint
, Base
);
756 KeReleaseSpinLock(&CiCacheSegMappingRegionLock
, oldIrql
);
758 MmLockAddressSpace(MmGetKernelAddressSpace());
759 MmFreeMemoryArea(MmGetKernelAddressSpace(),
760 CacheSeg
->BaseAddress
,
761 CacheSeg
->Bcb
->CacheSegmentSize
,
764 MmUnlockAddressSpace(MmGetKernelAddressSpace());
766 ExFreeToNPagedLookasideList(&CacheSegLookasideList
, CacheSeg
);
767 return(STATUS_SUCCESS
);
771 CcRosFreeCacheSegment(PBCB Bcb
, PCACHE_SEGMENT CacheSeg
)
778 DPRINT("CcRosFreeCacheSegment(Bcb %x, CacheSeg %x)\n",
781 ExAcquireFastMutex(&ViewLock
);
782 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
783 RemoveEntryList(&CacheSeg
->BcbSegmentListEntry
);
784 RemoveEntryList(&CacheSeg
->CacheSegmentListEntry
);
785 RemoveEntryList(&CacheSeg
->CacheSegmentLRUListEntry
);
788 RemoveEntryList(&CacheSeg
->DirtySegmentListEntry
);
789 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
792 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
793 ExReleaseFastMutex(&ViewLock
);
795 Status
= CcRosInternalFreeCacheSegment(CacheSeg
);
800 CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointers
,
801 IN PLARGE_INTEGER FileOffset OPTIONAL
,
803 OUT PIO_STATUS_BLOCK IoStatus
)
806 LARGE_INTEGER Offset
;
807 PCACHE_SEGMENT current
;
811 DPRINT("CcFlushCache(SectionObjectPointers %x, FileOffset %x, Length %d, IoStatus %x)\n",
812 SectionObjectPointers
, FileOffset
, Length
, IoStatus
);
814 if (SectionObjectPointers
&& SectionObjectPointers
->SharedCacheMap
)
816 Bcb
= (PBCB
)SectionObjectPointers
->SharedCacheMap
;
820 Offset
= *FileOffset
;
824 Offset
.QuadPart
= 0LL;
825 Length
= Bcb
->FileSize
.u
.LowPart
;
830 IoStatus
->Status
= STATUS_SUCCESS
;
831 IoStatus
->Information
= 0;
836 current
= CcRosLookupCacheSegment (Bcb
, Offset
.u
.LowPart
);
839 ExAcquireFastMutex(¤t
->Lock
);
842 Status
= CcRosFlushCacheSegment(current
);
843 if (!NT_SUCCESS(Status
) && IoStatus
!= NULL
)
845 IoStatus
->Status
= Status
;
848 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
849 ExReleaseFastMutex(¤t
->Lock
);
850 current
->ReferenceCount
--;
851 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
854 Offset
.QuadPart
+= Bcb
->CacheSegmentSize
;
855 if (Length
> Bcb
->CacheSegmentSize
)
857 Length
-= Bcb
->CacheSegmentSize
;
869 IoStatus
->Status
= STATUS_INVALID_PARAMETER
;
875 CcRosDeleteFileCache(PFILE_OBJECT FileObject
, PBCB Bcb
)
877 * FUNCTION: Releases the BCB associated with a file object
880 PLIST_ENTRY current_entry
;
881 PCACHE_SEGMENT current
;
889 ExReleaseFastMutex(&ViewLock
);
891 CcFlushCache(FileObject
->SectionObjectPointers
, NULL
, 0, NULL
);
893 ExAcquireFastMutex(&ViewLock
);
895 if (Bcb
->RefCount
== 0)
897 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
899 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
900 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
903 FileObject
->SectionObjectPointers
->SharedCacheMap
= NULL
;
906 * Release all cache segments.
908 InitializeListHead(&FreeList
);
909 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
910 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
911 while (!IsListEmpty(&Bcb
->BcbSegmentListHead
))
913 current_entry
= RemoveTailList(&Bcb
->BcbSegmentListHead
);
914 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
915 RemoveEntryList(¤t
->CacheSegmentListEntry
);
916 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
919 RemoveEntryList(¤t
->DirtySegmentListEntry
);
920 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
921 DPRINT1("Freeing dirty segment\n");
923 InsertHeadList(&FreeList
, ¤t
->BcbSegmentListEntry
);
925 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
927 ExReleaseFastMutex(&ViewLock
);
928 ObDereferenceObject (Bcb
->FileObject
);
930 while (!IsListEmpty(&FreeList
))
932 current_entry
= RemoveTailList(&FreeList
);
933 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
934 Status
= CcRosInternalFreeCacheSegment(current
);
936 ExFreeToNPagedLookasideList(&BcbLookasideList
, Bcb
);
937 ExAcquireFastMutex(&ViewLock
);
939 return(STATUS_SUCCESS
);
942 VOID
CcRosReferenceCache(PFILE_OBJECT FileObject
)
945 ExAcquireFastMutex(&ViewLock
);
946 Bcb
= (PBCB
)FileObject
->SectionObjectPointers
->SharedCacheMap
;
949 ExReleaseFastMutex(&ViewLock
);
952 VOID
CcRosSetRemoveOnClose(PSECTION_OBJECT_POINTERS SectionObjectPointer
)
955 // DPRINT1("CcRosSetRemoveOnClose()\n");
956 ExAcquireFastMutex(&ViewLock
);
957 Bcb
= (PBCB
)SectionObjectPointer
->SharedCacheMap
;
960 Bcb
->RemoveOnClose
= TRUE
;
961 if (Bcb
->RefCount
== 0)
963 CcRosDeleteFileCache(Bcb
->FileObject
, Bcb
);
966 ExReleaseFastMutex(&ViewLock
);
970 VOID
CcRosDereferenceCache(PFILE_OBJECT FileObject
)
973 ExAcquireFastMutex(&ViewLock
);
974 Bcb
= (PBCB
)FileObject
->SectionObjectPointers
->SharedCacheMap
;
976 if (Bcb
->RefCount
> 0)
979 if (Bcb
->RefCount
== 0)
981 MmFreeSectionSegments(Bcb
->FileObject
);
982 if (Bcb
->RemoveOnClose
)
984 CcRosDeleteFileCache(FileObject
, Bcb
);
988 Bcb
->TimeStamp
= CcTimeStamp
;
989 InsertHeadList(&ClosedListHead
, &Bcb
->BcbRemoveListEntry
);
993 ExReleaseFastMutex(&ViewLock
);
997 CcRosReleaseFileCache(PFILE_OBJECT FileObject
)
999 * FUNCTION: Called by the file system when a handle to a file object
1005 ExAcquireFastMutex(&ViewLock
);
1007 if (FileObject
->SectionObjectPointers
->SharedCacheMap
!= NULL
)
1009 Bcb
= FileObject
->SectionObjectPointers
->SharedCacheMap
;
1010 if (FileObject
->PrivateCacheMap
!= NULL
)
1012 FileObject
->PrivateCacheMap
= NULL
;
1013 if (Bcb
->RefCount
> 0)
1016 if (Bcb
->RefCount
== 0)
1018 if (Bcb
->RemoveOnClose
)
1020 CcRosDeleteFileCache(FileObject
, Bcb
);
1024 Bcb
->TimeStamp
= CcTimeStamp
;
1025 InsertHeadList(&ClosedListHead
, &Bcb
->BcbRemoveListEntry
);
1031 ExReleaseFastMutex(&ViewLock
);
1032 return(STATUS_SUCCESS
);
1036 CcRosInitializeFileCache(PFILE_OBJECT FileObject
,
1037 ULONG CacheSegmentSize
)
1039 * FUNCTION: Initializes a BCB for a file object
1043 DPRINT("CcRosInitializeFileCache(FileObject %x, *Bcb %x, CacheSegmentSize %d)\n",
1044 FileObject
, Bcb
, CacheSegmentSize
);
1046 ExAcquireFastMutex(&ViewLock
);
1048 Bcb
= FileObject
->SectionObjectPointers
->SharedCacheMap
;
1051 Bcb
= ExAllocateFromNPagedLookasideList(&BcbLookasideList
);
1054 ExReleaseFastMutex(&ViewLock
);
1055 return(STATUS_UNSUCCESSFUL
);
1057 memset(Bcb
, 0, sizeof(BCB
));
1058 ObReferenceObjectByPointer(FileObject
,
1062 Bcb
->FileObject
= FileObject
;
1063 Bcb
->CacheSegmentSize
= CacheSegmentSize
;
1064 if (FileObject
->FsContext
)
1066 Bcb
->AllocationSize
=
1067 ((REACTOS_COMMON_FCB_HEADER
*)FileObject
->FsContext
)->AllocationSize
;
1069 ((REACTOS_COMMON_FCB_HEADER
*)FileObject
->FsContext
)->FileSize
;
1071 KeInitializeSpinLock(&Bcb
->BcbLock
);
1072 InitializeListHead(&Bcb
->BcbSegmentListHead
);
1073 FileObject
->SectionObjectPointers
->SharedCacheMap
= Bcb
;
1075 if (FileObject
->PrivateCacheMap
== NULL
)
1077 FileObject
->PrivateCacheMap
= Bcb
;
1080 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1082 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1083 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1085 ExReleaseFastMutex(&ViewLock
);
1087 return(STATUS_SUCCESS
);
1090 PFILE_OBJECT STDCALL
1091 CcGetFileObjectFromSectionPtrs(IN PSECTION_OBJECT_POINTERS SectionObjectPointers
)
1094 if (SectionObjectPointers
&& SectionObjectPointers
->SharedCacheMap
)
1096 Bcb
= (PBCB
)SectionObjectPointers
->SharedCacheMap
;
1098 return Bcb
->FileObject
;
1104 CmLazyCloseThreadMain(PVOID Ignored
)
1106 LARGE_INTEGER Timeout
;
1107 PLIST_ENTRY current_entry
;
1109 ULONG RemoveTimeStamp
;
1112 KeQuerySystemTime (&Timeout
);
1116 Timeout
.QuadPart
+= 100000000LL; // 10sec
1117 Status
= KeWaitForSingleObject(&LazyCloseThreadEvent
,
1123 DPRINT("LazyCloseThreadMain %d\n", CcTimeStamp
);
1125 if (!NT_SUCCESS(Status
))
1127 DbgPrint("LazyCloseThread: Wait failed\n");
1131 if (LazyCloseThreadShouldTerminate
)
1133 DbgPrint("LazyCloseThread: Terminating\n");
1137 ExAcquireFastMutex(&ViewLock
);
1139 if (CcTimeStamp
>= 30)
1141 RemoveTimeStamp
= CcTimeStamp
- 30; /* 5min = 10sec * 30 */
1142 while (!IsListEmpty(&ClosedListHead
))
1144 current_entry
= ClosedListHead
.Blink
;
1145 current
= CONTAINING_RECORD(current_entry
, BCB
, BcbRemoveListEntry
);
1146 if (current
->TimeStamp
>= RemoveTimeStamp
)
1150 CcRosDeleteFileCache(current
->FileObject
, current
);
1153 ExReleaseFastMutex(&ViewLock
);
1167 DPRINT("CcInitView()\n");
1169 CiCacheSegMappingRegionHint
= 0;
1170 CiCacheSegMappingRegionBase
= NULL
;
1172 MmLockAddressSpace(MmGetKernelAddressSpace());
1174 Status
= MmCreateMemoryArea(NULL
,
1175 MmGetKernelAddressSpace(),
1176 MEMORY_AREA_CACHE_SEGMENT
,
1177 &CiCacheSegMappingRegionBase
,
1178 CI_CACHESEG_MAPPING_REGION_SIZE
,
1182 MmUnlockAddressSpace(MmGetKernelAddressSpace());
1183 if (!NT_SUCCESS(Status
))
1188 Buffer
= ExAllocatePool(NonPagedPool
, CI_CACHESEG_MAPPING_REGION_SIZE
/ (PAGE_SIZE
* 8));
1190 RtlInitializeBitMap(&CiCacheSegMappingRegionAllocMap
, Buffer
, CI_CACHESEG_MAPPING_REGION_SIZE
/ PAGE_SIZE
);
1191 RtlClearAllBits(&CiCacheSegMappingRegionAllocMap
);
1193 KeInitializeSpinLock(&CiCacheSegMappingRegionLock
);
1195 InitializeListHead(&CacheSegmentListHead
);
1196 InitializeListHead(&DirtySegmentListHead
);
1197 InitializeListHead(&CacheSegmentLRUListHead
);
1198 InitializeListHead(&ClosedListHead
);
1199 ExInitializeFastMutex(&ViewLock
);
1200 ExInitializeNPagedLookasideList (&iBcbLookasideList
,
1204 sizeof(INTERNAL_BCB
),
1207 ExInitializeNPagedLookasideList (&BcbLookasideList
,
1214 ExInitializeNPagedLookasideList (&CacheSegLookasideList
,
1218 sizeof(CACHE_SEGMENT
),
1222 MmInitializeMemoryConsumer(MC_CACHE
, CcRosTrimCache
);
1224 CcInitCacheZeroPage();
1227 LazyCloseThreadShouldTerminate
= FALSE
;
1228 KeInitializeEvent (&LazyCloseThreadEvent
, SynchronizationEvent
, FALSE
);
1229 Status
= PsCreateSystemThread(&LazyCloseThreadHandle
,
1234 (PKSTART_ROUTINE
)CmLazyCloseThreadMain
,
1236 if (NT_SUCCESS(Status
))
1238 Priority
= LOW_REALTIME_PRIORITY
;
1239 NtSetInformationThread(LazyCloseThreadHandle
,