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.70 2003/12/30 18:52:03 fireball 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 #if defined(__GNUC__)
109 void * alloca(size_t size
);
110 #elif defined(_MSC_VER)
111 void* _alloca(size_t size
);
113 #error Unknown compiler for alloca intrinsic stack allocation "function"
118 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg
);
120 /* FUNCTIONS *****************************************************************/
123 CcRosFlushCacheSegment(PCACHE_SEGMENT CacheSegment
)
127 Status
= WriteCacheSegment(CacheSegment
);
128 if (NT_SUCCESS(Status
))
130 ExAcquireFastMutex(&ViewLock
);
131 KeAcquireSpinLock(&CacheSegment
->Bcb
->BcbLock
, &oldIrql
);
132 CacheSegment
->Dirty
= FALSE
;
133 RemoveEntryList(&CacheSegment
->DirtySegmentListEntry
);
134 DirtyPageCount
-= CacheSegment
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
135 CacheSegment
->ReferenceCount
--;
136 KeReleaseSpinLock(&CacheSegment
->Bcb
->BcbLock
, oldIrql
);
137 ExReleaseFastMutex(&ViewLock
);
143 CcRosFlushDirtyPages(ULONG Target
, PULONG Count
)
145 PLIST_ENTRY current_entry
;
146 PCACHE_SEGMENT current
;
147 ULONG PagesPerSegment
;
150 static ULONG WriteCount
[4] = {0, 0, 0, 0};
153 DPRINT("CcRosFlushDirtyPages(Target %d)\n", Target
);
157 ExAcquireFastMutex(&ViewLock
);
159 WriteCount
[0] = WriteCount
[1];
160 WriteCount
[1] = WriteCount
[2];
161 WriteCount
[2] = WriteCount
[3];
164 NewTarget
= WriteCount
[0] + WriteCount
[1] + WriteCount
[2];
166 if (NewTarget
< DirtyPageCount
)
168 NewTarget
= (DirtyPageCount
- NewTarget
+ 3) / 4;
169 WriteCount
[0] += NewTarget
;
170 WriteCount
[1] += NewTarget
;
171 WriteCount
[2] += NewTarget
;
172 WriteCount
[3] += NewTarget
;
175 NewTarget
= WriteCount
[0];
177 Target
= max(NewTarget
, Target
);
179 current_entry
= DirtySegmentListHead
.Flink
;
180 if (current_entry
== &DirtySegmentListHead
)
182 DPRINT("No Dirty pages\n");
184 while (current_entry
!= &DirtySegmentListHead
&& Target
> 0)
186 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
187 DirtySegmentListEntry
);
188 current_entry
= current_entry
->Flink
;
189 Locked
= ExTryToAcquireFastMutex(¤t
->Lock
);
194 assert(current
->Dirty
);
195 if (current
->ReferenceCount
> 1)
197 ExReleaseFastMutex(¤t
->Lock
);
200 ExReleaseFastMutex(&ViewLock
);
201 PagesPerSegment
= current
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
202 Status
= CcRosFlushCacheSegment(current
);
203 ExReleaseFastMutex(¤t
->Lock
);
204 if (!NT_SUCCESS(Status
) && (Status
!= STATUS_END_OF_FILE
))
206 DPRINT1("CC: Failed to flush cache segment.\n");
210 (*Count
) += PagesPerSegment
;
211 Target
-= PagesPerSegment
;
213 ExAcquireFastMutex(&ViewLock
);
214 current_entry
= DirtySegmentListHead
.Flink
;
216 if (*Count
< NewTarget
)
218 WriteCount
[1] += (NewTarget
- *Count
);
220 ExReleaseFastMutex(&ViewLock
);
221 DPRINT("CcRosFlushDirtyPages() finished\n");
223 return(STATUS_SUCCESS
);
227 CcRosTrimCache(ULONG Target
, ULONG Priority
, PULONG NrFreed
)
229 * FUNCTION: Try to free some memory from the file cache.
231 * Target - The number of pages to be freed.
232 * Priority - The priority of free (currently unused).
233 * NrFreed - Points to a variable where the number of pages
234 * actually freed is returned.
237 PLIST_ENTRY current_entry
;
238 PCACHE_SEGMENT current
, last
= NULL
;
239 ULONG PagesPerSegment
;
244 DPRINT("CcRosTrimCache(Target %d)\n", Target
);
248 InitializeListHead(&FreeList
);
250 ExAcquireFastMutex(&ViewLock
);
251 current_entry
= CacheSegmentLRUListHead
.Flink
;
252 while (current_entry
!= &CacheSegmentLRUListHead
&& Target
> 0)
254 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
255 CacheSegmentLRUListEntry
);
256 current_entry
= current_entry
->Flink
;
258 KeAcquireSpinLock(¤t
->Bcb
->BcbLock
, &oldIrql
);
259 if (current
->ReferenceCount
== 0)
261 RemoveEntryList(¤t
->BcbSegmentListEntry
);
262 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
263 RemoveEntryList(¤t
->CacheSegmentListEntry
);
264 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
265 InsertHeadList(&FreeList
, ¤t
->BcbSegmentListEntry
);
266 PagesPerSegment
= current
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
267 PagesFreed
= min(PagesPerSegment
, Target
);
268 Target
-= PagesFreed
;
269 (*NrFreed
) += PagesFreed
;
273 if (last
!= current
&& current
->MappedCount
> 0 && !current
->Dirty
)
278 current
->ReferenceCount
++;
280 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
281 ExReleaseFastMutex(&ViewLock
);
282 for (i
= 0; i
< current
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
; i
++)
284 PHYSICAL_ADDRESS Page
;
285 Page
= MmGetPhysicalAddress((char*)current
->BaseAddress
+ i
* PAGE_SIZE
);
286 Status
= MmPageOutPhysicalAddress(Page
);
287 if (!NT_SUCCESS(Status
))
292 ExAcquireFastMutex(&ViewLock
);
293 KeAcquireSpinLock(¤t
->Bcb
->BcbLock
, &oldIrql
);
294 current
->ReferenceCount
--;
295 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
296 current_entry
= ¤t
->CacheSegmentLRUListEntry
;
299 KeReleaseSpinLock(¤t
->Bcb
->BcbLock
, oldIrql
);
302 ExReleaseFastMutex(&ViewLock
);
304 while (!IsListEmpty(&FreeList
))
306 current_entry
= RemoveHeadList(&FreeList
);
307 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
308 BcbSegmentListEntry
);
309 CcRosInternalFreeCacheSegment(current
);
312 DPRINT("CcRosTrimCache() finished\n");
313 return(STATUS_SUCCESS
);
317 CcRosReleaseCacheSegment(PBCB Bcb
,
318 PCACHE_SEGMENT CacheSeg
,
323 BOOLEAN WasDirty
= CacheSeg
->Dirty
;
328 DPRINT("CcReleaseCacheSegment(Bcb %x, CacheSeg %x, Valid %d)\n",
329 Bcb
, CacheSeg
, Valid
);
331 CacheSeg
->Valid
= Valid
;
332 CacheSeg
->Dirty
= CacheSeg
->Dirty
|| Dirty
;
334 ExAcquireFastMutex(&ViewLock
);
335 if (!WasDirty
&& CacheSeg
->Dirty
)
337 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
338 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
340 RemoveEntryList(&CacheSeg
->CacheSegmentLRUListEntry
);
341 InsertTailList(&CacheSegmentLRUListHead
, &CacheSeg
->CacheSegmentLRUListEntry
);
345 CacheSeg
->MappedCount
++;
347 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
348 CacheSeg
->ReferenceCount
--;
349 if (Mapped
&& CacheSeg
->MappedCount
== 1)
351 CacheSeg
->ReferenceCount
++;
353 if (!WasDirty
&& CacheSeg
->Dirty
)
355 CacheSeg
->ReferenceCount
++;
357 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
358 ExReleaseFastMutex(&ViewLock
);
359 ExReleaseFastMutex(&CacheSeg
->Lock
);
361 return(STATUS_SUCCESS
);
365 CcRosLookupCacheSegment(PBCB Bcb
, ULONG FileOffset
)
367 PLIST_ENTRY current_entry
;
368 PCACHE_SEGMENT current
;
373 DPRINT("CcRosLookupCacheSegment(Bcb %x, FileOffset %d)\n", Bcb
, FileOffset
);
375 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
376 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
377 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
379 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
380 BcbSegmentListEntry
);
381 if (current
->FileOffset
<= FileOffset
&&
382 (current
->FileOffset
+ Bcb
->CacheSegmentSize
) > FileOffset
)
384 current
->ReferenceCount
++;
385 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
386 ExAcquireFastMutex(¤t
->Lock
);
389 current_entry
= current_entry
->Flink
;
391 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
396 CcRosMarkDirtyCacheSegment(PBCB Bcb
, ULONG FileOffset
)
398 PCACHE_SEGMENT CacheSeg
;
403 DPRINT("CcRosMarkDirtyCacheSegment(Bcb %x, FileOffset %d)\n", Bcb
, FileOffset
);
405 CacheSeg
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
406 if (CacheSeg
== NULL
)
410 if (!CacheSeg
->Dirty
)
412 ExAcquireFastMutex(&ViewLock
);
413 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
414 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
415 ExReleaseFastMutex(&ViewLock
);
419 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
420 CacheSeg
->ReferenceCount
--;
421 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
425 CacheSeg
->Dirty
= TRUE
;
426 ExReleaseFastMutex(&CacheSeg
->Lock
);
428 return(STATUS_SUCCESS
);
432 CcRosUnmapCacheSegment(PBCB Bcb
, ULONG FileOffset
, BOOLEAN NowDirty
)
434 PCACHE_SEGMENT CacheSeg
;
440 DPRINT("CcRosUnmapCacheSegment(Bcb %x, FileOffset %d, NowDirty %d)\n",
441 Bcb
, FileOffset
, NowDirty
);
443 CacheSeg
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
444 if (CacheSeg
== NULL
)
446 return(STATUS_UNSUCCESSFUL
);
449 WasDirty
= CacheSeg
->Dirty
;
450 CacheSeg
->Dirty
= CacheSeg
->Dirty
|| NowDirty
;
452 CacheSeg
->MappedCount
--;
454 if (!WasDirty
&& NowDirty
)
456 ExAcquireFastMutex(&ViewLock
);
457 InsertTailList(&DirtySegmentListHead
, &CacheSeg
->DirtySegmentListEntry
);
458 DirtyPageCount
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
459 ExReleaseFastMutex(&ViewLock
);
462 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
463 CacheSeg
->ReferenceCount
--;
464 if (!WasDirty
&& NowDirty
)
466 CacheSeg
->ReferenceCount
++;
468 if (CacheSeg
->MappedCount
== 0)
470 CacheSeg
->ReferenceCount
--;
472 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
474 ExReleaseFastMutex(&CacheSeg
->Lock
);
475 return(STATUS_SUCCESS
);
479 CcRosCreateCacheSegment(PBCB Bcb
,
481 PCACHE_SEGMENT
* CacheSeg
)
484 PCACHE_SEGMENT current
;
485 PCACHE_SEGMENT previous
;
486 PLIST_ENTRY current_entry
;
490 ULONG StartingOffset
;
495 DPRINT("CcRosCreateCacheSegment()\n");
497 if (FileOffset
>= Bcb
->FileSize
.u
.LowPart
)
500 return STATUS_INVALID_PARAMETER
;
503 current
= ExAllocateFromNPagedLookasideList(&CacheSegLookasideList
);
504 current
->Valid
= FALSE
;
505 current
->Dirty
= FALSE
;
506 current
->FileOffset
= ROUND_DOWN(FileOffset
, Bcb
->CacheSegmentSize
);
508 current
->MappedCount
= 0;
509 current
->DirtySegmentListEntry
.Flink
= NULL
;
510 current
->DirtySegmentListEntry
.Blink
= NULL
;
511 current
->ReferenceCount
= 1;
512 ExInitializeFastMutex(¤t
->Lock
);
513 ExAcquireFastMutex(¤t
->Lock
);
514 ExAcquireFastMutex(&ViewLock
);
517 /* There is window between the call to CcRosLookupCacheSegment
518 * and CcRosCreateCacheSegment. We must check if a segment on
519 * the fileoffset exist. If there exist a segment, we release
520 * our new created segment and return the existing one.
522 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
523 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
525 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
527 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
,
528 BcbSegmentListEntry
);
529 if (current
->FileOffset
<= FileOffset
&&
530 (current
->FileOffset
+ Bcb
->CacheSegmentSize
) > FileOffset
)
532 current
->ReferenceCount
++;
533 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
534 ExReleaseFastMutex(&(*CacheSeg
)->Lock
);
535 ExReleaseFastMutex(&ViewLock
);
536 ExFreeToNPagedLookasideList(&CacheSegLookasideList
, *CacheSeg
);
538 ExAcquireFastMutex(¤t
->Lock
);
539 return STATUS_SUCCESS
;
541 if (current
->FileOffset
< FileOffset
)
543 if (previous
== NULL
)
549 if (previous
->FileOffset
< current
->FileOffset
)
555 current_entry
= current_entry
->Flink
;
557 /* There was no existing segment. */
561 InsertHeadList(&previous
->BcbSegmentListEntry
, ¤t
->BcbSegmentListEntry
);
565 InsertHeadList(&Bcb
->BcbSegmentListHead
, ¤t
->BcbSegmentListEntry
);
567 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
568 InsertTailList(&CacheSegmentListHead
, ¤t
->CacheSegmentListEntry
);
569 InsertTailList(&CacheSegmentLRUListHead
, ¤t
->CacheSegmentLRUListEntry
);
570 ExReleaseFastMutex(&ViewLock
);
572 KeAcquireSpinLock(&CiCacheSegMappingRegionLock
, &oldIrql
);
574 StartingOffset
= RtlFindClearBitsAndSet(&CiCacheSegMappingRegionAllocMap
, Bcb
->CacheSegmentSize
/ PAGE_SIZE
, CiCacheSegMappingRegionHint
);
576 if (StartingOffset
== 0xffffffff)
578 DPRINT1("Out of CacheSeg mapping space\n");
582 current
->BaseAddress
= CiCacheSegMappingRegionBase
+ StartingOffset
* PAGE_SIZE
;
584 if (CiCacheSegMappingRegionHint
== StartingOffset
)
586 CiCacheSegMappingRegionHint
+= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
589 KeReleaseSpinLock(&CiCacheSegMappingRegionLock
, oldIrql
);
591 MmLockAddressSpace(MmGetKernelAddressSpace());
592 current
->BaseAddress
= NULL
;
593 Status
= MmCreateMemoryArea(NULL
,
594 MmGetKernelAddressSpace(),
595 MEMORY_AREA_CACHE_SEGMENT
,
596 ¤t
->BaseAddress
,
597 Bcb
->CacheSegmentSize
,
599 (PMEMORY_AREA
*)¤t
->MemoryArea
,
602 MmUnlockAddressSpace(MmGetKernelAddressSpace());
603 if (!NT_SUCCESS(Status
))
608 for (i
= 0; i
< (Bcb
->CacheSegmentSize
/ PAGE_SIZE
); i
++)
610 PHYSICAL_ADDRESS Page
;
612 Status
= MmRequestPageMemoryConsumer(MC_CACHE
, TRUE
, &Page
);
613 if (!NT_SUCCESS(Status
))
618 Status
= MmCreateVirtualMapping(NULL
,
619 (char*)current
->BaseAddress
+ (i
* PAGE_SIZE
),
623 if (!NT_SUCCESS(Status
))
628 return(STATUS_SUCCESS
);
632 CcRosGetCacheSegmentChain(PBCB Bcb
,
635 PCACHE_SEGMENT
* CacheSeg
)
637 PCACHE_SEGMENT current
;
639 PCACHE_SEGMENT
* CacheSegList
;
640 PCACHE_SEGMENT Previous
= NULL
;
644 DPRINT("CcRosGetCacheSegmentChain()\n");
646 Length
= ROUND_UP(Length
, Bcb
->CacheSegmentSize
);
648 #if defined(__GNUC__)
649 CacheSegList
= alloca(sizeof(PCACHE_SEGMENT
) *
650 (Length
/ Bcb
->CacheSegmentSize
));
651 #elif defined(_MSC_VER)
652 CacheSegList
= _alloca(sizeof(PCACHE_SEGMENT
) *
653 (Length
/ Bcb
->CacheSegmentSize
));
655 #error Unknown compiler for alloca intrinsic stack allocation "function"
659 * Look for a cache segment already mapping the same data.
661 for (i
= 0; i
< (Length
/ Bcb
->CacheSegmentSize
); i
++)
663 ULONG CurrentOffset
= FileOffset
+ (i
* Bcb
->CacheSegmentSize
);
664 current
= CcRosLookupCacheSegment(Bcb
, CurrentOffset
);
667 CacheSegList
[i
] = current
;
671 CcRosCreateCacheSegment(Bcb
, CurrentOffset
, ¤t
);
672 CacheSegList
[i
] = current
;
676 for (i
= 0; i
< (Length
/ Bcb
->CacheSegmentSize
); i
++)
680 *CacheSeg
= CacheSegList
[i
];
681 Previous
= CacheSegList
[i
];
685 Previous
->NextInChain
= CacheSegList
[i
];
686 Previous
= CacheSegList
[i
];
689 Previous
->NextInChain
= NULL
;
691 return(STATUS_SUCCESS
);
695 CcRosGetCacheSegment(PBCB Bcb
,
700 PCACHE_SEGMENT
* CacheSeg
)
702 PCACHE_SEGMENT current
;
707 DPRINT("CcRosGetCacheSegment()\n");
710 * Look for a cache segment already mapping the same data.
712 current
= CcRosLookupCacheSegment(Bcb
, FileOffset
);
716 * Otherwise create a new segment.
718 Status
= CcRosCreateCacheSegment(Bcb
, FileOffset
, ¤t
);
719 if (!NT_SUCCESS(Status
))
725 * Return information about the segment to the caller.
727 *UptoDate
= current
->Valid
;
728 *BaseAddress
= current
->BaseAddress
;
729 DPRINT("*BaseAddress 0x%.8X\n", *BaseAddress
);
731 *BaseOffset
= current
->FileOffset
;
732 return(STATUS_SUCCESS
);
736 CcRosRequestCacheSegment(PBCB Bcb
,
740 PCACHE_SEGMENT
* CacheSeg
)
742 * FUNCTION: Request a page mapping for a BCB
749 if ((FileOffset
% Bcb
->CacheSegmentSize
) != 0)
751 CPRINT("Bad fileoffset %x should be multiple of %x",
752 FileOffset
, Bcb
->CacheSegmentSize
);
756 return(CcRosGetCacheSegment(Bcb
,
766 CcFreeCachePage(PVOID Context
, MEMORY_AREA
* MemoryArea
, PVOID Address
,
767 PHYSICAL_ADDRESS PhysAddr
, SWAPENTRY SwapEntry
, BOOLEAN Dirty
)
769 assert(SwapEntry
== 0);
770 if (PhysAddr
.QuadPart
!= 0)
772 MmReleasePageMemoryConsumer(MC_CACHE
, PhysAddr
);
777 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg
)
779 * FUNCTION: Releases a cache segment associated with a BCB
786 PHYSICAL_ADDRESS PhysicalAddr
;
789 DPRINT("Freeing cache segment %x\n", CacheSeg
);
791 RegionSize
= CacheSeg
->Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
793 /* Unmap all the pages. */
794 for (i
= 0; i
< RegionSize
; i
++)
796 MmDeleteVirtualMapping(NULL
,
797 CacheSeg
->BaseAddress
+ (i
* PAGE_SIZE
),
801 MmReleasePageMemoryConsumer(MC_CACHE
, PhysicalAddr
);
804 KeAcquireSpinLock(&CiCacheSegMappingRegionLock
, &oldIrql
);
805 /* Deallocate all the pages used. */
806 Base
= (ULONG
)(CacheSeg
->BaseAddress
- CiCacheSegMappingRegionBase
) / PAGE_SIZE
;
808 RtlClearBits(&CiCacheSegMappingRegionAllocMap
, Base
, RegionSize
);
810 CiCacheSegMappingRegionHint
= min (CiCacheSegMappingRegionHint
, Base
);
812 KeReleaseSpinLock(&CiCacheSegMappingRegionLock
, oldIrql
);
814 MmLockAddressSpace(MmGetKernelAddressSpace());
815 MmFreeMemoryArea(MmGetKernelAddressSpace(),
816 CacheSeg
->BaseAddress
,
817 CacheSeg
->Bcb
->CacheSegmentSize
,
820 MmUnlockAddressSpace(MmGetKernelAddressSpace());
822 ExFreeToNPagedLookasideList(&CacheSegLookasideList
, CacheSeg
);
823 return(STATUS_SUCCESS
);
827 CcRosFreeCacheSegment(PBCB Bcb
, PCACHE_SEGMENT CacheSeg
)
834 DPRINT("CcRosFreeCacheSegment(Bcb %x, CacheSeg %x)\n",
837 ExAcquireFastMutex(&ViewLock
);
838 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
839 RemoveEntryList(&CacheSeg
->BcbSegmentListEntry
);
840 RemoveEntryList(&CacheSeg
->CacheSegmentListEntry
);
841 RemoveEntryList(&CacheSeg
->CacheSegmentLRUListEntry
);
844 RemoveEntryList(&CacheSeg
->DirtySegmentListEntry
);
845 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
848 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
849 ExReleaseFastMutex(&ViewLock
);
851 Status
= CcRosInternalFreeCacheSegment(CacheSeg
);
859 CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointers
,
860 IN PLARGE_INTEGER FileOffset OPTIONAL
,
862 OUT PIO_STATUS_BLOCK IoStatus
)
865 LARGE_INTEGER Offset
;
866 PCACHE_SEGMENT current
;
870 DPRINT("CcFlushCache(SectionObjectPointers %x, FileOffset %x, Length %d, IoStatus %x)\n",
871 SectionObjectPointers
, FileOffset
, Length
, IoStatus
);
873 if (SectionObjectPointers
&& SectionObjectPointers
->SharedCacheMap
)
875 Bcb
= (PBCB
)SectionObjectPointers
->SharedCacheMap
;
879 Offset
= *FileOffset
;
883 #if defined(__GNUC__)
884 Offset
.QuadPart
= 0LL;
888 Length
= Bcb
->FileSize
.u
.LowPart
;
893 IoStatus
->Status
= STATUS_SUCCESS
;
894 IoStatus
->Information
= 0;
899 current
= CcRosLookupCacheSegment (Bcb
, Offset
.u
.LowPart
);
904 Status
= CcRosFlushCacheSegment(current
);
905 if (!NT_SUCCESS(Status
) && IoStatus
!= NULL
)
907 IoStatus
->Status
= Status
;
910 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
911 ExReleaseFastMutex(¤t
->Lock
);
912 current
->ReferenceCount
--;
913 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
916 Offset
.QuadPart
+= Bcb
->CacheSegmentSize
;
917 if (Length
> Bcb
->CacheSegmentSize
)
919 Length
-= Bcb
->CacheSegmentSize
;
931 IoStatus
->Status
= STATUS_INVALID_PARAMETER
;
937 CcRosDeleteFileCache(PFILE_OBJECT FileObject
, PBCB Bcb
)
939 * FUNCTION: Releases the BCB associated with a file object
942 PLIST_ENTRY current_entry
;
943 PCACHE_SEGMENT current
;
951 ExReleaseFastMutex(&ViewLock
);
953 CcFlushCache(FileObject
->SectionObjectPointer
, NULL
, 0, NULL
);
955 ExAcquireFastMutex(&ViewLock
);
957 if (Bcb
->RefCount
== 0)
959 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
961 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
962 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
965 FileObject
->SectionObjectPointer
->SharedCacheMap
= NULL
;
968 * Release all cache segments.
970 InitializeListHead(&FreeList
);
971 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldIrql
);
972 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
973 while (!IsListEmpty(&Bcb
->BcbSegmentListHead
))
975 current_entry
= RemoveTailList(&Bcb
->BcbSegmentListHead
);
976 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
977 RemoveEntryList(¤t
->CacheSegmentListEntry
);
978 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
981 RemoveEntryList(¤t
->DirtySegmentListEntry
);
982 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
983 DPRINT1("Freeing dirty segment\n");
985 InsertHeadList(&FreeList
, ¤t
->BcbSegmentListEntry
);
987 KeReleaseSpinLock(&Bcb
->BcbLock
, oldIrql
);
989 ExReleaseFastMutex(&ViewLock
);
990 ObDereferenceObject (Bcb
->FileObject
);
992 while (!IsListEmpty(&FreeList
))
994 current_entry
= RemoveTailList(&FreeList
);
995 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
996 Status
= CcRosInternalFreeCacheSegment(current
);
998 ExFreeToNPagedLookasideList(&BcbLookasideList
, Bcb
);
999 ExAcquireFastMutex(&ViewLock
);
1001 return(STATUS_SUCCESS
);
1004 VOID
CcRosReferenceCache(PFILE_OBJECT FileObject
)
1007 ExAcquireFastMutex(&ViewLock
);
1008 Bcb
= (PBCB
)FileObject
->SectionObjectPointer
->SharedCacheMap
;
1010 if (Bcb
->RefCount
== 0)
1012 assert(Bcb
->BcbRemoveListEntry
.Flink
!= NULL
);
1013 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1014 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1019 assert(Bcb
->BcbRemoveListEntry
.Flink
== NULL
);
1022 ExReleaseFastMutex(&ViewLock
);
1025 VOID
CcRosSetRemoveOnClose(PSECTION_OBJECT_POINTERS SectionObjectPointer
)
1028 DPRINT("CcRosSetRemoveOnClose()\n");
1029 ExAcquireFastMutex(&ViewLock
);
1030 Bcb
= (PBCB
)SectionObjectPointer
->SharedCacheMap
;
1033 Bcb
->RemoveOnClose
= TRUE
;
1034 if (Bcb
->RefCount
== 0)
1036 CcRosDeleteFileCache(Bcb
->FileObject
, Bcb
);
1039 ExReleaseFastMutex(&ViewLock
);
1043 VOID
CcRosDereferenceCache(PFILE_OBJECT FileObject
)
1046 ExAcquireFastMutex(&ViewLock
);
1047 Bcb
= (PBCB
)FileObject
->SectionObjectPointer
->SharedCacheMap
;
1049 if (Bcb
->RefCount
> 0)
1052 if (Bcb
->RefCount
== 0)
1054 MmFreeSectionSegments(Bcb
->FileObject
);
1055 if (Bcb
->RemoveOnClose
)
1057 CcRosDeleteFileCache(FileObject
, Bcb
);
1061 Bcb
->TimeStamp
= CcTimeStamp
;
1062 InsertHeadList(&ClosedListHead
, &Bcb
->BcbRemoveListEntry
);
1066 ExReleaseFastMutex(&ViewLock
);
1070 CcRosReleaseFileCache(PFILE_OBJECT FileObject
)
1072 * FUNCTION: Called by the file system when a handle to a file object
1078 ExAcquireFastMutex(&ViewLock
);
1080 if (FileObject
->SectionObjectPointer
->SharedCacheMap
!= NULL
)
1082 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1083 if (FileObject
->PrivateCacheMap
!= NULL
)
1085 FileObject
->PrivateCacheMap
= NULL
;
1086 if (Bcb
->RefCount
> 0)
1089 if (Bcb
->RefCount
== 0)
1091 if (Bcb
->RemoveOnClose
)
1093 CcRosDeleteFileCache(FileObject
, Bcb
);
1097 Bcb
->TimeStamp
= CcTimeStamp
;
1098 InsertHeadList(&ClosedListHead
, &Bcb
->BcbRemoveListEntry
);
1104 ExReleaseFastMutex(&ViewLock
);
1105 return(STATUS_SUCCESS
);
1109 CcTryToInitializeFileCache(PFILE_OBJECT FileObject
)
1114 ExAcquireFastMutex(&ViewLock
);
1116 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1119 Status
= STATUS_UNSUCCESSFUL
;
1123 if (FileObject
->PrivateCacheMap
== NULL
)
1125 FileObject
->PrivateCacheMap
= Bcb
;
1128 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1130 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1131 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1133 Status
= STATUS_SUCCESS
;
1135 ExReleaseFastMutex(&ViewLock
);
1142 CcRosInitializeFileCache(PFILE_OBJECT FileObject
,
1143 ULONG CacheSegmentSize
)
1145 * FUNCTION: Initializes a BCB for a file object
1149 DPRINT("CcRosInitializeFileCache(FileObject %x, *Bcb %x, CacheSegmentSize %d)\n",
1150 FileObject
, Bcb
, CacheSegmentSize
);
1152 ExAcquireFastMutex(&ViewLock
);
1154 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
1157 Bcb
= ExAllocateFromNPagedLookasideList(&BcbLookasideList
);
1160 ExReleaseFastMutex(&ViewLock
);
1161 return(STATUS_UNSUCCESSFUL
);
1163 memset(Bcb
, 0, sizeof(BCB
));
1164 ObReferenceObjectByPointer(FileObject
,
1168 Bcb
->FileObject
= FileObject
;
1169 Bcb
->CacheSegmentSize
= CacheSegmentSize
;
1170 if (FileObject
->FsContext
)
1172 Bcb
->AllocationSize
=
1173 ((PFSRTL_COMMON_FCB_HEADER
)FileObject
->FsContext
)->AllocationSize
;
1175 ((PFSRTL_COMMON_FCB_HEADER
)FileObject
->FsContext
)->FileSize
;
1177 KeInitializeSpinLock(&Bcb
->BcbLock
);
1178 InitializeListHead(&Bcb
->BcbSegmentListHead
);
1179 FileObject
->SectionObjectPointer
->SharedCacheMap
= Bcb
;
1181 if (FileObject
->PrivateCacheMap
== NULL
)
1183 FileObject
->PrivateCacheMap
= Bcb
;
1186 if (Bcb
->BcbRemoveListEntry
.Flink
!= NULL
)
1188 RemoveEntryList(&Bcb
->BcbRemoveListEntry
);
1189 Bcb
->BcbRemoveListEntry
.Flink
= NULL
;
1191 ExReleaseFastMutex(&ViewLock
);
1193 return(STATUS_SUCCESS
);
1199 PFILE_OBJECT STDCALL
1200 CcGetFileObjectFromSectionPtrs(IN PSECTION_OBJECT_POINTERS SectionObjectPointers
)
1203 if (SectionObjectPointers
&& SectionObjectPointers
->SharedCacheMap
)
1205 Bcb
= (PBCB
)SectionObjectPointers
->SharedCacheMap
;
1207 return Bcb
->FileObject
;
1213 CmLazyCloseThreadMain(PVOID Ignored
)
1215 LARGE_INTEGER Timeout
;
1216 PLIST_ENTRY current_entry
;
1218 ULONG RemoveTimeStamp
;
1221 KeQuerySystemTime (&Timeout
);
1225 #if defined(__GNUC__)
1226 Timeout
.QuadPart
+= 100000000LL; // 10sec
1228 Timeout
.QuadPart
+= 100000000; // 10sec
1230 Status
= KeWaitForSingleObject(&LazyCloseThreadEvent
,
1236 DPRINT("LazyCloseThreadMain %d\n", CcTimeStamp
);
1238 if (!NT_SUCCESS(Status
))
1240 DbgPrint("LazyCloseThread: Wait failed\n");
1244 if (LazyCloseThreadShouldTerminate
)
1246 DbgPrint("LazyCloseThread: Terminating\n");
1250 ExAcquireFastMutex(&ViewLock
);
1252 if (CcTimeStamp
>= 30)
1254 RemoveTimeStamp
= CcTimeStamp
- 30; /* 5min = 10sec * 30 */
1255 while (!IsListEmpty(&ClosedListHead
))
1257 current_entry
= ClosedListHead
.Blink
;
1258 current
= CONTAINING_RECORD(current_entry
, BCB
, BcbRemoveListEntry
);
1259 if (current
->TimeStamp
>= RemoveTimeStamp
)
1263 CcRosDeleteFileCache(current
->FileObject
, current
);
1266 ExReleaseFastMutex(&ViewLock
);
1280 DPRINT("CcInitView()\n");
1282 CiCacheSegMappingRegionHint
= 0;
1283 CiCacheSegMappingRegionBase
= NULL
;
1285 MmLockAddressSpace(MmGetKernelAddressSpace());
1287 Status
= MmCreateMemoryArea(NULL
,
1288 MmGetKernelAddressSpace(),
1289 MEMORY_AREA_CACHE_SEGMENT
,
1290 &CiCacheSegMappingRegionBase
,
1291 CI_CACHESEG_MAPPING_REGION_SIZE
,
1296 MmUnlockAddressSpace(MmGetKernelAddressSpace());
1297 if (!NT_SUCCESS(Status
))
1302 Buffer
= ExAllocatePool(NonPagedPool
, CI_CACHESEG_MAPPING_REGION_SIZE
/ (PAGE_SIZE
* 8));
1304 RtlInitializeBitMap(&CiCacheSegMappingRegionAllocMap
, Buffer
, CI_CACHESEG_MAPPING_REGION_SIZE
/ PAGE_SIZE
);
1305 RtlClearAllBits(&CiCacheSegMappingRegionAllocMap
);
1307 KeInitializeSpinLock(&CiCacheSegMappingRegionLock
);
1309 InitializeListHead(&CacheSegmentListHead
);
1310 InitializeListHead(&DirtySegmentListHead
);
1311 InitializeListHead(&CacheSegmentLRUListHead
);
1312 InitializeListHead(&ClosedListHead
);
1313 ExInitializeFastMutex(&ViewLock
);
1314 ExInitializeNPagedLookasideList (&iBcbLookasideList
,
1318 sizeof(INTERNAL_BCB
),
1321 ExInitializeNPagedLookasideList (&BcbLookasideList
,
1328 ExInitializeNPagedLookasideList (&CacheSegLookasideList
,
1332 sizeof(CACHE_SEGMENT
),
1336 MmInitializeMemoryConsumer(MC_CACHE
, CcRosTrimCache
);
1338 CcInitCacheZeroPage();
1341 LazyCloseThreadShouldTerminate
= FALSE
;
1342 KeInitializeEvent (&LazyCloseThreadEvent
, SynchronizationEvent
, FALSE
);
1343 Status
= PsCreateSystemThread(&LazyCloseThreadHandle
,
1348 (PKSTART_ROUTINE
)CmLazyCloseThreadMain
,
1350 if (NT_SUCCESS(Status
))
1352 Priority
= LOW_REALTIME_PRIORITY
;
1353 NtSetInformationThread(LazyCloseThreadHandle
,