- Call MmFreeSectionSegments if the Bcb reference count is zero (in CcRosReleaseFileC...
[reactos.git] / reactos / ntoskrnl / cc / view.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /* $Id: view.c,v 1.78 2004/09/19 12:11:44 hbirr Exp $
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/cc/view.c
23 * PURPOSE: Cache manager
24 * PROGRAMMER: David Welch (welch@mcmail.com)
25 * PORTABILITY: Checked
26 * UPDATE HISTORY:
27 * Created 22/05/98
28 */
29
30 /* NOTES **********************************************************************
31 *
32 * This is not the NT implementation of a file cache nor anything much like
33 * it.
34 *
35 * The general procedure for a filesystem to implement a read or write
36 * dispatch routine is as follows
37 *
38 * (1) If caching for the FCB hasn't been initiated then so do by calling
39 * CcInitializeFileCache.
40 *
41 * (2) For each 4k region which is being read or written obtain a cache page
42 * by calling CcRequestCachePage.
43 *
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.
47 *
48 * (4) Copy the data into or out of the page as necessary.
49 *
50 * (5) Release the cache page
51 */
52 /* INCLUDES ******************************************************************/
53
54 #include <ntoskrnl.h>
55 #define NDEBUG
56 #include <internal/debug.h>
57
58 /* GLOBALS *******************************************************************/
59
60 /*
61 * If CACHE_BITMAP is defined, the cache manager uses one large memory region
62 * within the kernel address space and allocate/deallocate space from this block
63 * over a bitmap. If CACHE_BITMAP is used, the size of the mdl mapping region
64 * must be reduced (ntoskrnl\mm\mdl.c, MI_MDLMAPPING_REGION_SIZE).
65 */
66 //#define CACHE_BITMAP
67
68 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
69 #define ROUND_DOWN(N, S) (((N) % (S)) ? ROUND_UP(N, S) - S : N)
70
71 #define TAG_CSEG TAG('C', 'S', 'E', 'G')
72 #define TAG_BCB TAG('B', 'C', 'B', ' ')
73 #define TAG_IBCB TAG('i', 'B', 'C', 'B')
74
75 static LIST_ENTRY DirtySegmentListHead;
76 static LIST_ENTRY CacheSegmentListHead;
77 static LIST_ENTRY CacheSegmentLRUListHead;
78 static LIST_ENTRY ClosedListHead;
79 ULONG DirtyPageCount=0;
80
81 FAST_MUTEX ViewLock;
82
83 #ifdef CACHE_BITMAP
84 #define CI_CACHESEG_MAPPING_REGION_SIZE (128*1024*1024)
85
86 static PVOID CiCacheSegMappingRegionBase = NULL;
87 static RTL_BITMAP CiCacheSegMappingRegionAllocMap;
88 static ULONG CiCacheSegMappingRegionHint;
89 static KSPIN_LOCK CiCacheSegMappingRegionLock;
90 #endif
91
92 NPAGED_LOOKASIDE_LIST iBcbLookasideList;
93 static NPAGED_LOOKASIDE_LIST BcbLookasideList;
94 static NPAGED_LOOKASIDE_LIST CacheSegLookasideList;
95
96 static ULONG CcTimeStamp;
97 static KEVENT LazyCloseThreadEvent;
98 static HANDLE LazyCloseThreadHandle;
99 static CLIENT_ID LazyCloseThreadId;
100 static volatile BOOLEAN LazyCloseThreadShouldTerminate;
101
102 #if defined(__GNUC__)
103 void * alloca(size_t size);
104 #elif defined(_MSC_VER)
105 void* _alloca(size_t size);
106 #else
107 #error Unknown compiler for alloca intrinsic stack allocation "function"
108 #endif
109
110
111 NTSTATUS
112 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg);
113
114 /* FUNCTIONS *****************************************************************/
115
116 NTSTATUS
117 CcRosFlushCacheSegment(PCACHE_SEGMENT CacheSegment)
118 {
119 NTSTATUS Status;
120 KIRQL oldIrql;
121 Status = WriteCacheSegment(CacheSegment);
122 if (NT_SUCCESS(Status))
123 {
124 ExAcquireFastMutex(&ViewLock);
125 KeAcquireSpinLock(&CacheSegment->Bcb->BcbLock, &oldIrql);
126 CacheSegment->Dirty = FALSE;
127 RemoveEntryList(&CacheSegment->DirtySegmentListEntry);
128 DirtyPageCount -= CacheSegment->Bcb->CacheSegmentSize / PAGE_SIZE;
129 CacheSegment->ReferenceCount--;
130 KeReleaseSpinLock(&CacheSegment->Bcb->BcbLock, oldIrql);
131 ExReleaseFastMutex(&ViewLock);
132 }
133 return(Status);
134 }
135
136 NTSTATUS
137 CcRosFlushDirtyPages(ULONG Target, PULONG Count)
138 {
139 PLIST_ENTRY current_entry;
140 PCACHE_SEGMENT current;
141 ULONG PagesPerSegment;
142 BOOLEAN Locked;
143 NTSTATUS Status;
144 static ULONG WriteCount[4] = {0, 0, 0, 0};
145 ULONG NewTarget;
146
147 DPRINT("CcRosFlushDirtyPages(Target %d)\n", Target);
148
149 (*Count) = 0;
150
151 ExAcquireFastMutex(&ViewLock);
152
153 WriteCount[0] = WriteCount[1];
154 WriteCount[1] = WriteCount[2];
155 WriteCount[2] = WriteCount[3];
156 WriteCount[3] = 0;
157
158 NewTarget = WriteCount[0] + WriteCount[1] + WriteCount[2];
159
160 if (NewTarget < DirtyPageCount)
161 {
162 NewTarget = (DirtyPageCount - NewTarget + 3) / 4;
163 WriteCount[0] += NewTarget;
164 WriteCount[1] += NewTarget;
165 WriteCount[2] += NewTarget;
166 WriteCount[3] += NewTarget;
167 }
168
169 NewTarget = WriteCount[0];
170
171 Target = max(NewTarget, Target);
172
173 current_entry = DirtySegmentListHead.Flink;
174 if (current_entry == &DirtySegmentListHead)
175 {
176 DPRINT("No Dirty pages\n");
177 }
178 while (current_entry != &DirtySegmentListHead && Target > 0)
179 {
180 current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
181 DirtySegmentListEntry);
182 current_entry = current_entry->Flink;
183 Locked = ExTryToAcquireFastMutex(&current->Lock);
184 if (!Locked)
185 {
186 continue;
187 }
188 assert(current->Dirty);
189 if (current->ReferenceCount > 1)
190 {
191 ExReleaseFastMutex(&current->Lock);
192 continue;
193 }
194 ExReleaseFastMutex(&ViewLock);
195 PagesPerSegment = current->Bcb->CacheSegmentSize / PAGE_SIZE;
196 Status = CcRosFlushCacheSegment(current);
197 ExReleaseFastMutex(&current->Lock);
198 if (!NT_SUCCESS(Status) && (Status != STATUS_END_OF_FILE))
199 {
200 DPRINT1("CC: Failed to flush cache segment.\n");
201 }
202 else
203 {
204 (*Count) += PagesPerSegment;
205 Target -= PagesPerSegment;
206 }
207 ExAcquireFastMutex(&ViewLock);
208 current_entry = DirtySegmentListHead.Flink;
209 }
210 if (*Count < NewTarget)
211 {
212 WriteCount[1] += (NewTarget - *Count);
213 }
214 ExReleaseFastMutex(&ViewLock);
215 DPRINT("CcRosFlushDirtyPages() finished\n");
216
217 return(STATUS_SUCCESS);
218 }
219
220 NTSTATUS
221 CcRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
222 /*
223 * FUNCTION: Try to free some memory from the file cache.
224 * ARGUMENTS:
225 * Target - The number of pages to be freed.
226 * Priority - The priority of free (currently unused).
227 * NrFreed - Points to a variable where the number of pages
228 * actually freed is returned.
229 */
230 {
231 PLIST_ENTRY current_entry;
232 PCACHE_SEGMENT current, last = NULL;
233 ULONG PagesPerSegment;
234 ULONG PagesFreed;
235 KIRQL oldIrql;
236 LIST_ENTRY FreeList;
237
238 DPRINT("CcRosTrimCache(Target %d)\n", Target);
239
240 *NrFreed = 0;
241
242 InitializeListHead(&FreeList);
243
244 ExAcquireFastMutex(&ViewLock);
245 current_entry = CacheSegmentLRUListHead.Flink;
246 while (current_entry != &CacheSegmentLRUListHead && Target > 0)
247 {
248 current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
249 CacheSegmentLRUListEntry);
250 current_entry = current_entry->Flink;
251
252 KeAcquireSpinLock(&current->Bcb->BcbLock, &oldIrql);
253 if (current->ReferenceCount == 0)
254 {
255 RemoveEntryList(&current->BcbSegmentListEntry);
256 KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
257 RemoveEntryList(&current->CacheSegmentListEntry);
258 RemoveEntryList(&current->CacheSegmentLRUListEntry);
259 InsertHeadList(&FreeList, &current->BcbSegmentListEntry);
260 PagesPerSegment = current->Bcb->CacheSegmentSize / PAGE_SIZE;
261 PagesFreed = min(PagesPerSegment, Target);
262 Target -= PagesFreed;
263 (*NrFreed) += PagesFreed;
264 }
265 else
266 {
267 if (last != current && current->MappedCount > 0 && !current->Dirty && !current->PageOut)
268 {
269 ULONG i;
270 NTSTATUS Status;
271
272 current->ReferenceCount++;
273 last = current;
274 current->PageOut = TRUE;
275 KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
276 ExReleaseFastMutex(&ViewLock);
277 for (i = 0; i < current->Bcb->CacheSegmentSize / PAGE_SIZE; i++)
278 {
279 PFN_TYPE Page;
280 Page = MmGetPhysicalAddress((char*)current->BaseAddress + i * PAGE_SIZE).QuadPart >> PAGE_SHIFT;
281 Status = MmPageOutPhysicalAddress(Page);
282 if (!NT_SUCCESS(Status))
283 {
284 break;
285 }
286 }
287 ExAcquireFastMutex(&ViewLock);
288 KeAcquireSpinLock(&current->Bcb->BcbLock, &oldIrql);
289 current->ReferenceCount--;
290 current->PageOut = FALSE;
291 KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
292 current_entry = &current->CacheSegmentLRUListEntry;
293 continue;
294 }
295 KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
296 }
297 }
298 ExReleaseFastMutex(&ViewLock);
299
300 while (!IsListEmpty(&FreeList))
301 {
302 current_entry = RemoveHeadList(&FreeList);
303 current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
304 BcbSegmentListEntry);
305 CcRosInternalFreeCacheSegment(current);
306 }
307
308 DPRINT("CcRosTrimCache() finished\n");
309 return(STATUS_SUCCESS);
310 }
311
312 NTSTATUS
313 CcRosReleaseCacheSegment(PBCB Bcb,
314 PCACHE_SEGMENT CacheSeg,
315 BOOLEAN Valid,
316 BOOLEAN Dirty,
317 BOOLEAN Mapped)
318 {
319 BOOLEAN WasDirty = CacheSeg->Dirty;
320 KIRQL oldIrql;
321
322 assert(Bcb);
323
324 DPRINT("CcReleaseCacheSegment(Bcb %x, CacheSeg %x, Valid %d)\n",
325 Bcb, CacheSeg, Valid);
326
327 CacheSeg->Valid = Valid;
328 CacheSeg->Dirty = CacheSeg->Dirty || Dirty;
329
330 ExAcquireFastMutex(&ViewLock);
331 if (!WasDirty && CacheSeg->Dirty)
332 {
333 InsertTailList(&DirtySegmentListHead, &CacheSeg->DirtySegmentListEntry);
334 DirtyPageCount += Bcb->CacheSegmentSize / PAGE_SIZE;
335 }
336 RemoveEntryList(&CacheSeg->CacheSegmentLRUListEntry);
337 InsertTailList(&CacheSegmentLRUListHead, &CacheSeg->CacheSegmentLRUListEntry);
338
339 if (Mapped)
340 {
341 CacheSeg->MappedCount++;
342 }
343 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
344 CacheSeg->ReferenceCount--;
345 if (Mapped && CacheSeg->MappedCount == 1)
346 {
347 CacheSeg->ReferenceCount++;
348 }
349 if (!WasDirty && CacheSeg->Dirty)
350 {
351 CacheSeg->ReferenceCount++;
352 }
353 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
354 ExReleaseFastMutex(&ViewLock);
355 ExReleaseFastMutex(&CacheSeg->Lock);
356
357 return(STATUS_SUCCESS);
358 }
359
360 PCACHE_SEGMENT
361 CcRosLookupCacheSegment(PBCB Bcb, ULONG FileOffset)
362 {
363 PLIST_ENTRY current_entry;
364 PCACHE_SEGMENT current;
365 KIRQL oldIrql;
366
367 assert(Bcb);
368
369 DPRINT("CcRosLookupCacheSegment(Bcb %x, FileOffset %d)\n", Bcb, FileOffset);
370
371 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
372 current_entry = Bcb->BcbSegmentListHead.Flink;
373 while (current_entry != &Bcb->BcbSegmentListHead)
374 {
375 current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
376 BcbSegmentListEntry);
377 if (current->FileOffset <= FileOffset &&
378 (current->FileOffset + Bcb->CacheSegmentSize) > FileOffset)
379 {
380 current->ReferenceCount++;
381 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
382 ExAcquireFastMutex(&current->Lock);
383 return(current);
384 }
385 current_entry = current_entry->Flink;
386 }
387 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
388 return(NULL);
389 }
390
391 NTSTATUS
392 CcRosMarkDirtyCacheSegment(PBCB Bcb, ULONG FileOffset)
393 {
394 PCACHE_SEGMENT CacheSeg;
395 KIRQL oldIrql;
396
397 assert(Bcb);
398
399 DPRINT("CcRosMarkDirtyCacheSegment(Bcb %x, FileOffset %d)\n", Bcb, FileOffset);
400
401 CacheSeg = CcRosLookupCacheSegment(Bcb, FileOffset);
402 if (CacheSeg == NULL)
403 {
404 KEBUGCHECKCC;
405 }
406 if (!CacheSeg->Dirty)
407 {
408 ExAcquireFastMutex(&ViewLock);
409 InsertTailList(&DirtySegmentListHead, &CacheSeg->DirtySegmentListEntry);
410 DirtyPageCount += Bcb->CacheSegmentSize / PAGE_SIZE;
411 ExReleaseFastMutex(&ViewLock);
412 }
413 else
414 {
415 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
416 CacheSeg->ReferenceCount--;
417 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
418 }
419
420
421 CacheSeg->Dirty = TRUE;
422 ExReleaseFastMutex(&CacheSeg->Lock);
423
424 return(STATUS_SUCCESS);
425 }
426
427 NTSTATUS
428 CcRosUnmapCacheSegment(PBCB Bcb, ULONG FileOffset, BOOLEAN NowDirty)
429 {
430 PCACHE_SEGMENT CacheSeg;
431 BOOLEAN WasDirty;
432 KIRQL oldIrql;
433
434 assert(Bcb);
435
436 DPRINT("CcRosUnmapCacheSegment(Bcb %x, FileOffset %d, NowDirty %d)\n",
437 Bcb, FileOffset, NowDirty);
438
439 CacheSeg = CcRosLookupCacheSegment(Bcb, FileOffset);
440 if (CacheSeg == NULL)
441 {
442 return(STATUS_UNSUCCESSFUL);
443 }
444
445 WasDirty = CacheSeg->Dirty;
446 CacheSeg->Dirty = CacheSeg->Dirty || NowDirty;
447
448 CacheSeg->MappedCount--;
449
450 if (!WasDirty && NowDirty)
451 {
452 ExAcquireFastMutex(&ViewLock);
453 InsertTailList(&DirtySegmentListHead, &CacheSeg->DirtySegmentListEntry);
454 DirtyPageCount += Bcb->CacheSegmentSize / PAGE_SIZE;
455 ExReleaseFastMutex(&ViewLock);
456 }
457
458 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
459 CacheSeg->ReferenceCount--;
460 if (!WasDirty && NowDirty)
461 {
462 CacheSeg->ReferenceCount++;
463 }
464 if (CacheSeg->MappedCount == 0)
465 {
466 CacheSeg->ReferenceCount--;
467 }
468 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
469
470 ExReleaseFastMutex(&CacheSeg->Lock);
471 return(STATUS_SUCCESS);
472 }
473
474 NTSTATUS STATIC
475 CcRosCreateCacheSegment(PBCB Bcb,
476 ULONG FileOffset,
477 PCACHE_SEGMENT* CacheSeg)
478 {
479 ULONG i;
480 PCACHE_SEGMENT current;
481 PCACHE_SEGMENT previous;
482 PLIST_ENTRY current_entry;
483 NTSTATUS Status;
484 KIRQL oldIrql;
485 PPFN_TYPE Pfn;
486 #ifdef CACHE_BITMAP
487 ULONG StartingOffset;
488 #else
489 #endif
490 PHYSICAL_ADDRESS BoundaryAddressMultiple;
491
492 assert(Bcb);
493
494 DPRINT("CcRosCreateCacheSegment()\n");
495
496 BoundaryAddressMultiple.QuadPart = 0;
497 if (FileOffset >= Bcb->FileSize.u.LowPart)
498 {
499 CacheSeg = NULL;
500 return STATUS_INVALID_PARAMETER;
501 }
502
503 current = ExAllocateFromNPagedLookasideList(&CacheSegLookasideList);
504 current->Valid = FALSE;
505 current->Dirty = FALSE;
506 current->PageOut = FALSE;
507 current->FileOffset = ROUND_DOWN(FileOffset, Bcb->CacheSegmentSize);
508 current->Bcb = Bcb;
509 current->MappedCount = 0;
510 current->DirtySegmentListEntry.Flink = NULL;
511 current->DirtySegmentListEntry.Blink = NULL;
512 current->ReferenceCount = 1;
513 ExInitializeFastMutex(&current->Lock);
514 ExAcquireFastMutex(&current->Lock);
515 ExAcquireFastMutex(&ViewLock);
516
517 *CacheSeg = current;
518 /* There is window between the call to CcRosLookupCacheSegment
519 * and CcRosCreateCacheSegment. We must check if a segment on
520 * the fileoffset exist. If there exist a segment, we release
521 * our new created segment and return the existing one.
522 */
523 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
524 current_entry = Bcb->BcbSegmentListHead.Flink;
525 previous = NULL;
526 while (current_entry != &Bcb->BcbSegmentListHead)
527 {
528 current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
529 BcbSegmentListEntry);
530 if (current->FileOffset <= FileOffset &&
531 (current->FileOffset + Bcb->CacheSegmentSize) > FileOffset)
532 {
533 current->ReferenceCount++;
534 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
535 ExReleaseFastMutex(&(*CacheSeg)->Lock);
536 ExReleaseFastMutex(&ViewLock);
537 ExFreeToNPagedLookasideList(&CacheSegLookasideList, *CacheSeg);
538 *CacheSeg = current;
539 ExAcquireFastMutex(&current->Lock);
540 return STATUS_SUCCESS;
541 }
542 if (current->FileOffset < FileOffset)
543 {
544 if (previous == NULL)
545 {
546 previous = current;
547 }
548 else
549 {
550 if (previous->FileOffset < current->FileOffset)
551 {
552 previous = current;
553 }
554 }
555 }
556 current_entry = current_entry->Flink;
557 }
558 /* There was no existing segment. */
559 current = *CacheSeg;
560 if (previous)
561 {
562 InsertHeadList(&previous->BcbSegmentListEntry, &current->BcbSegmentListEntry);
563 }
564 else
565 {
566 InsertHeadList(&Bcb->BcbSegmentListHead, &current->BcbSegmentListEntry);
567 }
568 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
569 InsertTailList(&CacheSegmentListHead, &current->CacheSegmentListEntry);
570 InsertTailList(&CacheSegmentLRUListHead, &current->CacheSegmentLRUListEntry);
571 ExReleaseFastMutex(&ViewLock);
572 #ifdef CACHE_BITMAP
573 KeAcquireSpinLock(&CiCacheSegMappingRegionLock, &oldIrql);
574
575 StartingOffset = RtlFindClearBitsAndSet(&CiCacheSegMappingRegionAllocMap, Bcb->CacheSegmentSize / PAGE_SIZE, CiCacheSegMappingRegionHint);
576
577 if (StartingOffset == 0xffffffff)
578 {
579 DPRINT1("Out of CacheSeg mapping space\n");
580 KEBUGCHECKCC;
581 }
582
583 current->BaseAddress = CiCacheSegMappingRegionBase + StartingOffset * PAGE_SIZE;
584
585 if (CiCacheSegMappingRegionHint == StartingOffset)
586 {
587 CiCacheSegMappingRegionHint += Bcb->CacheSegmentSize / PAGE_SIZE;
588 }
589
590 KeReleaseSpinLock(&CiCacheSegMappingRegionLock, oldIrql);
591 #else
592 MmLockAddressSpace(MmGetKernelAddressSpace());
593 current->BaseAddress = NULL;
594 Status = MmCreateMemoryArea(NULL,
595 MmGetKernelAddressSpace(),
596 MEMORY_AREA_CACHE_SEGMENT,
597 &current->BaseAddress,
598 Bcb->CacheSegmentSize,
599 PAGE_READWRITE,
600 (PMEMORY_AREA*)&current->MemoryArea,
601 FALSE,
602 FALSE,
603 BoundaryAddressMultiple);
604 MmUnlockAddressSpace(MmGetKernelAddressSpace());
605 if (!NT_SUCCESS(Status))
606 {
607 KEBUGCHECKCC;
608 }
609 #endif
610 Pfn = alloca(sizeof(PFN_TYPE) * (Bcb->CacheSegmentSize / PAGE_SIZE));
611 for (i = 0; i < (Bcb->CacheSegmentSize / PAGE_SIZE); i++)
612 {
613 Status = MmRequestPageMemoryConsumer(MC_CACHE, TRUE, &Pfn[i]);
614 if (!NT_SUCCESS(Status))
615 {
616 KEBUGCHECKCC;
617 }
618 }
619 Status = MmCreateVirtualMapping(NULL,
620 current->BaseAddress,
621 PAGE_READWRITE,
622 Pfn,
623 Bcb->CacheSegmentSize / PAGE_SIZE);
624 if (!NT_SUCCESS(Status))
625 {
626 KEBUGCHECKCC;
627 }
628 return(STATUS_SUCCESS);
629 }
630
631 NTSTATUS
632 CcRosGetCacheSegmentChain(PBCB Bcb,
633 ULONG FileOffset,
634 ULONG Length,
635 PCACHE_SEGMENT* CacheSeg)
636 {
637 PCACHE_SEGMENT current;
638 ULONG i;
639 PCACHE_SEGMENT* CacheSegList;
640 PCACHE_SEGMENT Previous = NULL;
641
642 assert(Bcb);
643
644 DPRINT("CcRosGetCacheSegmentChain()\n");
645
646 Length = ROUND_UP(Length, Bcb->CacheSegmentSize);
647
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));
654 #else
655 #error Unknown compiler for alloca intrinsic stack allocation "function"
656 #endif
657
658 /*
659 * Look for a cache segment already mapping the same data.
660 */
661 for (i = 0; i < (Length / Bcb->CacheSegmentSize); i++)
662 {
663 ULONG CurrentOffset = FileOffset + (i * Bcb->CacheSegmentSize);
664 current = CcRosLookupCacheSegment(Bcb, CurrentOffset);
665 if (current != NULL)
666 {
667 CacheSegList[i] = current;
668 }
669 else
670 {
671 CcRosCreateCacheSegment(Bcb, CurrentOffset, &current);
672 CacheSegList[i] = current;
673 }
674 }
675
676 for (i = 0; i < (Length / Bcb->CacheSegmentSize); i++)
677 {
678 if (i == 0)
679 {
680 *CacheSeg = CacheSegList[i];
681 Previous = CacheSegList[i];
682 }
683 else
684 {
685 Previous->NextInChain = CacheSegList[i];
686 Previous = CacheSegList[i];
687 }
688 }
689 Previous->NextInChain = NULL;
690
691 return(STATUS_SUCCESS);
692 }
693
694 NTSTATUS
695 CcRosGetCacheSegment(PBCB Bcb,
696 ULONG FileOffset,
697 PULONG BaseOffset,
698 PVOID* BaseAddress,
699 PBOOLEAN UptoDate,
700 PCACHE_SEGMENT* CacheSeg)
701 {
702 PCACHE_SEGMENT current;
703 NTSTATUS Status;
704
705 assert(Bcb);
706
707 DPRINT("CcRosGetCacheSegment()\n");
708
709 /*
710 * Look for a cache segment already mapping the same data.
711 */
712 current = CcRosLookupCacheSegment(Bcb, FileOffset);
713 if (current == NULL)
714 {
715 /*
716 * Otherwise create a new segment.
717 */
718 Status = CcRosCreateCacheSegment(Bcb, FileOffset, &current);
719 if (!NT_SUCCESS(Status))
720 {
721 return Status;
722 }
723 }
724 /*
725 * Return information about the segment to the caller.
726 */
727 *UptoDate = current->Valid;
728 *BaseAddress = current->BaseAddress;
729 DPRINT("*BaseAddress 0x%.8X\n", *BaseAddress);
730 *CacheSeg = current;
731 *BaseOffset = current->FileOffset;
732 return(STATUS_SUCCESS);
733 }
734
735 NTSTATUS STDCALL
736 CcRosRequestCacheSegment(PBCB Bcb,
737 ULONG FileOffset,
738 PVOID* BaseAddress,
739 PBOOLEAN UptoDate,
740 PCACHE_SEGMENT* CacheSeg)
741 /*
742 * FUNCTION: Request a page mapping for a BCB
743 */
744 {
745 ULONG BaseOffset;
746
747 assert(Bcb);
748
749 if ((FileOffset % Bcb->CacheSegmentSize) != 0)
750 {
751 CPRINT("Bad fileoffset %x should be multiple of %x",
752 FileOffset, Bcb->CacheSegmentSize);
753 KEBUGCHECKCC;
754 }
755
756 return(CcRosGetCacheSegment(Bcb,
757 FileOffset,
758 &BaseOffset,
759 BaseAddress,
760 UptoDate,
761 CacheSeg));
762 }
763 #ifdef CACHE_BITMAP
764 #else
765 STATIC VOID
766 CcFreeCachePage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
767 PFN_TYPE Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
768 {
769 assert(SwapEntry == 0);
770 if (Page != 0)
771 {
772 MmReleasePageMemoryConsumer(MC_CACHE, Page);
773 }
774 }
775 #endif
776 NTSTATUS
777 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg)
778 /*
779 * FUNCTION: Releases a cache segment associated with a BCB
780 */
781 {
782 #ifdef CACHE_BITMAP
783 ULONG i;
784 ULONG RegionSize;
785 ULONG Base;
786 PFN_TYPE Page;
787 KIRQL oldIrql;
788 #endif
789 DPRINT("Freeing cache segment %x\n", CacheSeg);
790 #ifdef CACHE_BITMAP
791 RegionSize = CacheSeg->Bcb->CacheSegmentSize / PAGE_SIZE;
792
793 /* Unmap all the pages. */
794 for (i = 0; i < RegionSize; i++)
795 {
796 MmDeleteVirtualMapping(NULL,
797 CacheSeg->BaseAddress + (i * PAGE_SIZE),
798 FALSE,
799 NULL,
800 &Page);
801 MmReleasePageMemoryConsumer(MC_CACHE, Page);
802 }
803
804 KeAcquireSpinLock(&CiCacheSegMappingRegionLock, &oldIrql);
805 /* Deallocate all the pages used. */
806 Base = (ULONG)(CacheSeg->BaseAddress - CiCacheSegMappingRegionBase) / PAGE_SIZE;
807
808 RtlClearBits(&CiCacheSegMappingRegionAllocMap, Base, RegionSize);
809
810 CiCacheSegMappingRegionHint = min (CiCacheSegMappingRegionHint, Base);
811
812 KeReleaseSpinLock(&CiCacheSegMappingRegionLock, oldIrql);
813 #else
814 MmLockAddressSpace(MmGetKernelAddressSpace());
815 MmFreeMemoryArea(MmGetKernelAddressSpace(),
816 CacheSeg->BaseAddress,
817 CacheSeg->Bcb->CacheSegmentSize,
818 CcFreeCachePage,
819 NULL);
820 MmUnlockAddressSpace(MmGetKernelAddressSpace());
821 #endif
822 ExFreeToNPagedLookasideList(&CacheSegLookasideList, CacheSeg);
823 return(STATUS_SUCCESS);
824 }
825
826 NTSTATUS
827 CcRosFreeCacheSegment(PBCB Bcb, PCACHE_SEGMENT CacheSeg)
828 {
829 NTSTATUS Status;
830 KIRQL oldIrql;
831
832 assert(Bcb);
833
834 DPRINT("CcRosFreeCacheSegment(Bcb %x, CacheSeg %x)\n",
835 Bcb, CacheSeg);
836
837 ExAcquireFastMutex(&ViewLock);
838 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
839 RemoveEntryList(&CacheSeg->BcbSegmentListEntry);
840 RemoveEntryList(&CacheSeg->CacheSegmentListEntry);
841 RemoveEntryList(&CacheSeg->CacheSegmentLRUListEntry);
842 if (CacheSeg->Dirty)
843 {
844 RemoveEntryList(&CacheSeg->DirtySegmentListEntry);
845 DirtyPageCount -= Bcb->CacheSegmentSize / PAGE_SIZE;
846
847 }
848 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
849 ExReleaseFastMutex(&ViewLock);
850
851 Status = CcRosInternalFreeCacheSegment(CacheSeg);
852 return(Status);
853 }
854
855 /*
856 * @implemented
857 */
858 VOID STDCALL
859 CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointers,
860 IN PLARGE_INTEGER FileOffset OPTIONAL,
861 IN ULONG Length,
862 OUT PIO_STATUS_BLOCK IoStatus)
863 {
864 PBCB Bcb;
865 LARGE_INTEGER Offset;
866 PCACHE_SEGMENT current;
867 NTSTATUS Status;
868 KIRQL oldIrql;
869
870 DPRINT("CcFlushCache(SectionObjectPointers %x, FileOffset %x, Length %d, IoStatus %x)\n",
871 SectionObjectPointers, FileOffset, Length, IoStatus);
872
873 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
874 {
875 Bcb = (PBCB)SectionObjectPointers->SharedCacheMap;
876 assert(Bcb);
877 if (FileOffset)
878 {
879 Offset = *FileOffset;
880 }
881 else
882 {
883 #if defined(__GNUC__)
884 Offset.QuadPart = 0LL;
885 #else
886 Offset.QuadPart = 0;
887 #endif
888 Length = Bcb->FileSize.u.LowPart;
889 }
890
891 if (IoStatus)
892 {
893 IoStatus->Status = STATUS_SUCCESS;
894 IoStatus->Information = 0;
895 }
896
897 while (Length > 0)
898 {
899 current = CcRosLookupCacheSegment (Bcb, Offset.u.LowPart);
900 if (current != NULL)
901 {
902 if (current->Dirty)
903 {
904 Status = CcRosFlushCacheSegment(current);
905 if (!NT_SUCCESS(Status) && IoStatus != NULL)
906 {
907 IoStatus->Status = Status;
908 }
909 }
910 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
911 ExReleaseFastMutex(&current->Lock);
912 current->ReferenceCount--;
913 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
914 }
915
916 Offset.QuadPart += Bcb->CacheSegmentSize;
917 if (Length > Bcb->CacheSegmentSize)
918 {
919 Length -= Bcb->CacheSegmentSize;
920 }
921 else
922 {
923 Length = 0;
924 }
925 }
926 }
927 else
928 {
929 if (IoStatus)
930 {
931 IoStatus->Status = STATUS_INVALID_PARAMETER;
932 }
933 }
934 }
935
936 NTSTATUS
937 CcRosDeleteFileCache(PFILE_OBJECT FileObject, PBCB Bcb)
938 /*
939 * FUNCTION: Releases the BCB associated with a file object
940 */
941 {
942 PLIST_ENTRY current_entry;
943 PCACHE_SEGMENT current;
944 NTSTATUS Status;
945 LIST_ENTRY FreeList;
946 KIRQL oldIrql;
947
948 assert(Bcb);
949
950 Bcb->RefCount++;
951 ExReleaseFastMutex(&ViewLock);
952
953 CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, NULL);
954
955 ExAcquireFastMutex(&ViewLock);
956 Bcb->RefCount--;
957 if (Bcb->RefCount == 0)
958 {
959 if (Bcb->BcbRemoveListEntry.Flink != NULL)
960 {
961 RemoveEntryList(&Bcb->BcbRemoveListEntry);
962 Bcb->BcbRemoveListEntry.Flink = NULL;
963 }
964
965 FileObject->SectionObjectPointer->SharedCacheMap = NULL;
966
967 /*
968 * Release all cache segments.
969 */
970 InitializeListHead(&FreeList);
971 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
972 current_entry = Bcb->BcbSegmentListHead.Flink;
973 while (!IsListEmpty(&Bcb->BcbSegmentListHead))
974 {
975 current_entry = RemoveTailList(&Bcb->BcbSegmentListHead);
976 current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry);
977 RemoveEntryList(&current->CacheSegmentListEntry);
978 RemoveEntryList(&current->CacheSegmentLRUListEntry);
979 if (current->Dirty)
980 {
981 RemoveEntryList(&current->DirtySegmentListEntry);
982 DirtyPageCount -= Bcb->CacheSegmentSize / PAGE_SIZE;
983 DPRINT1("Freeing dirty segment\n");
984 }
985 InsertHeadList(&FreeList, &current->BcbSegmentListEntry);
986 }
987 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
988
989 ExReleaseFastMutex(&ViewLock);
990 ObDereferenceObject (Bcb->FileObject);
991
992 while (!IsListEmpty(&FreeList))
993 {
994 current_entry = RemoveTailList(&FreeList);
995 current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry);
996 Status = CcRosInternalFreeCacheSegment(current);
997 }
998 ExFreeToNPagedLookasideList(&BcbLookasideList, Bcb);
999 ExAcquireFastMutex(&ViewLock);
1000 }
1001 return(STATUS_SUCCESS);
1002 }
1003
1004 VOID CcRosReferenceCache(PFILE_OBJECT FileObject)
1005 {
1006 PBCB Bcb;
1007 ExAcquireFastMutex(&ViewLock);
1008 Bcb = (PBCB)FileObject->SectionObjectPointer->SharedCacheMap;
1009 assert(Bcb);
1010 if (Bcb->RefCount == 0)
1011 {
1012 assert(Bcb->BcbRemoveListEntry.Flink != NULL);
1013 RemoveEntryList(&Bcb->BcbRemoveListEntry);
1014 Bcb->BcbRemoveListEntry.Flink = NULL;
1015
1016 }
1017 else
1018 {
1019 assert(Bcb->BcbRemoveListEntry.Flink == NULL);
1020 }
1021 Bcb->RefCount++;
1022 ExReleaseFastMutex(&ViewLock);
1023 }
1024
1025 VOID CcRosSetRemoveOnClose(PSECTION_OBJECT_POINTERS SectionObjectPointer)
1026 {
1027 PBCB Bcb;
1028 DPRINT("CcRosSetRemoveOnClose()\n");
1029 ExAcquireFastMutex(&ViewLock);
1030 Bcb = (PBCB)SectionObjectPointer->SharedCacheMap;
1031 if (Bcb)
1032 {
1033 Bcb->RemoveOnClose = TRUE;
1034 if (Bcb->RefCount == 0)
1035 {
1036 CcRosDeleteFileCache(Bcb->FileObject, Bcb);
1037 }
1038 }
1039 ExReleaseFastMutex(&ViewLock);
1040 }
1041
1042
1043 VOID CcRosDereferenceCache(PFILE_OBJECT FileObject)
1044 {
1045 PBCB Bcb;
1046 ExAcquireFastMutex(&ViewLock);
1047 Bcb = (PBCB)FileObject->SectionObjectPointer->SharedCacheMap;
1048 assert(Bcb);
1049 if (Bcb->RefCount > 0)
1050 {
1051 Bcb->RefCount--;
1052 if (Bcb->RefCount == 0)
1053 {
1054 MmFreeSectionSegments(Bcb->FileObject);
1055 if (Bcb->RemoveOnClose)
1056 {
1057 CcRosDeleteFileCache(FileObject, Bcb);
1058 }
1059 else
1060 {
1061 Bcb->TimeStamp = CcTimeStamp;
1062 InsertHeadList(&ClosedListHead, &Bcb->BcbRemoveListEntry);
1063 }
1064 }
1065 }
1066 ExReleaseFastMutex(&ViewLock);
1067 }
1068
1069 NTSTATUS STDCALL
1070 CcRosReleaseFileCache(PFILE_OBJECT FileObject)
1071 /*
1072 * FUNCTION: Called by the file system when a handle to a file object
1073 * has been closed.
1074 */
1075 {
1076 PBCB Bcb;
1077
1078 ExAcquireFastMutex(&ViewLock);
1079
1080 if (FileObject->SectionObjectPointer->SharedCacheMap != NULL)
1081 {
1082 Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
1083 if (FileObject->PrivateCacheMap != NULL)
1084 {
1085 FileObject->PrivateCacheMap = NULL;
1086 if (Bcb->RefCount > 0)
1087 {
1088 Bcb->RefCount--;
1089 if (Bcb->RefCount == 0)
1090 {
1091 MmFreeSectionSegments(Bcb->FileObject);
1092 if (Bcb->RemoveOnClose)
1093 {
1094 CcRosDeleteFileCache(FileObject, Bcb);
1095 }
1096 else
1097 {
1098 Bcb->TimeStamp = CcTimeStamp;
1099 InsertHeadList(&ClosedListHead, &Bcb->BcbRemoveListEntry);
1100 }
1101 }
1102 }
1103 }
1104 }
1105 ExReleaseFastMutex(&ViewLock);
1106 return(STATUS_SUCCESS);
1107 }
1108
1109 NTSTATUS
1110 CcTryToInitializeFileCache(PFILE_OBJECT FileObject)
1111 {
1112 PBCB Bcb;
1113 NTSTATUS Status;
1114
1115 ExAcquireFastMutex(&ViewLock);
1116
1117 Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
1118 if (Bcb == NULL)
1119 {
1120 Status = STATUS_UNSUCCESSFUL;
1121 }
1122 else
1123 {
1124 if (FileObject->PrivateCacheMap == NULL)
1125 {
1126 FileObject->PrivateCacheMap = Bcb;
1127 Bcb->RefCount++;
1128 }
1129 if (Bcb->BcbRemoveListEntry.Flink != NULL)
1130 {
1131 RemoveEntryList(&Bcb->BcbRemoveListEntry);
1132 Bcb->BcbRemoveListEntry.Flink = NULL;
1133 }
1134 Status = STATUS_SUCCESS;
1135 }
1136 ExReleaseFastMutex(&ViewLock);
1137
1138 return Status;
1139 }
1140
1141
1142 NTSTATUS STDCALL
1143 CcRosInitializeFileCache(PFILE_OBJECT FileObject,
1144 ULONG CacheSegmentSize)
1145 /*
1146 * FUNCTION: Initializes a BCB for a file object
1147 */
1148 {
1149 PBCB Bcb;
1150
1151 Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
1152 DPRINT("CcRosInitializeFileCache(FileObject %x, *Bcb %x, CacheSegmentSize %d)\n",
1153 FileObject, Bcb, CacheSegmentSize);
1154
1155 ExAcquireFastMutex(&ViewLock);
1156 if (Bcb == NULL)
1157 {
1158 Bcb = ExAllocateFromNPagedLookasideList(&BcbLookasideList);
1159 if (Bcb == NULL)
1160 {
1161 ExReleaseFastMutex(&ViewLock);
1162 return(STATUS_UNSUCCESSFUL);
1163 }
1164 memset(Bcb, 0, sizeof(BCB));
1165 ObReferenceObjectByPointer(FileObject,
1166 FILE_ALL_ACCESS,
1167 NULL,
1168 KernelMode);
1169 Bcb->FileObject = FileObject;
1170 Bcb->CacheSegmentSize = CacheSegmentSize;
1171 if (FileObject->FsContext)
1172 {
1173 Bcb->AllocationSize =
1174 ((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->AllocationSize;
1175 Bcb->FileSize =
1176 ((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->FileSize;
1177 }
1178 KeInitializeSpinLock(&Bcb->BcbLock);
1179 InitializeListHead(&Bcb->BcbSegmentListHead);
1180 FileObject->SectionObjectPointer->SharedCacheMap = Bcb;
1181 }
1182 if (FileObject->PrivateCacheMap == NULL)
1183 {
1184 FileObject->PrivateCacheMap = Bcb;
1185 Bcb->RefCount++;
1186 }
1187 if (Bcb->BcbRemoveListEntry.Flink != NULL)
1188 {
1189 RemoveEntryList(&Bcb->BcbRemoveListEntry);
1190 Bcb->BcbRemoveListEntry.Flink = NULL;
1191 }
1192 ExReleaseFastMutex(&ViewLock);
1193
1194 return(STATUS_SUCCESS);
1195 }
1196
1197 /*
1198 * @implemented
1199 */
1200 PFILE_OBJECT STDCALL
1201 CcGetFileObjectFromSectionPtrs(IN PSECTION_OBJECT_POINTERS SectionObjectPointers)
1202 {
1203 PBCB Bcb;
1204 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
1205 {
1206 Bcb = (PBCB)SectionObjectPointers->SharedCacheMap;
1207 assert(Bcb);
1208 return Bcb->FileObject;
1209 }
1210 return NULL;
1211 }
1212
1213 VOID STDCALL
1214 CmLazyCloseThreadMain(PVOID Ignored)
1215 {
1216 LARGE_INTEGER Timeout;
1217 PLIST_ENTRY current_entry;
1218 PBCB current;
1219 ULONG RemoveTimeStamp;
1220 NTSTATUS Status;
1221
1222 KeQuerySystemTime (&Timeout);
1223
1224 while (1)
1225 {
1226 #if defined(__GNUC__)
1227 Timeout.QuadPart += 100000000LL; // 10sec
1228 #else
1229 Timeout.QuadPart += 100000000; // 10sec
1230 #endif
1231 Status = KeWaitForSingleObject(&LazyCloseThreadEvent,
1232 0,
1233 KernelMode,
1234 FALSE,
1235 &Timeout);
1236
1237 DPRINT("LazyCloseThreadMain %d\n", CcTimeStamp);
1238
1239 if (!NT_SUCCESS(Status))
1240 {
1241 DbgPrint("LazyCloseThread: Wait failed\n");
1242 KEBUGCHECKCC;
1243 break;
1244 }
1245 if (LazyCloseThreadShouldTerminate)
1246 {
1247 DbgPrint("LazyCloseThread: Terminating\n");
1248 break;
1249 }
1250
1251 ExAcquireFastMutex(&ViewLock);
1252 CcTimeStamp++;
1253 if (CcTimeStamp >= 30)
1254 {
1255 RemoveTimeStamp = CcTimeStamp - 30; /* 5min = 10sec * 30 */
1256 while (!IsListEmpty(&ClosedListHead))
1257 {
1258 current_entry = ClosedListHead.Blink;
1259 current = CONTAINING_RECORD(current_entry, BCB, BcbRemoveListEntry);
1260 if (current->TimeStamp >= RemoveTimeStamp)
1261 {
1262 break;
1263 }
1264 CcRosDeleteFileCache(current->FileObject, current);
1265 }
1266 }
1267 ExReleaseFastMutex(&ViewLock);
1268 }
1269 }
1270
1271 VOID INIT_FUNCTION
1272 CcInitView(VOID)
1273 {
1274 #ifdef CACHE_BITMAP
1275 PMEMORY_AREA marea;
1276 PVOID Buffer;
1277 PHYSICAL_ADDRESS BoundaryAddressMultiple;
1278 #endif
1279 NTSTATUS Status;
1280 KPRIORITY Priority;
1281
1282 DPRINT("CcInitView()\n");
1283 #ifdef CACHE_BITMAP
1284 BoundaryAddressMultiple.QuadPart = 0;
1285 CiCacheSegMappingRegionHint = 0;
1286 CiCacheSegMappingRegionBase = NULL;
1287
1288 MmLockAddressSpace(MmGetKernelAddressSpace());
1289
1290 Status = MmCreateMemoryArea(NULL,
1291 MmGetKernelAddressSpace(),
1292 MEMORY_AREA_CACHE_SEGMENT,
1293 &CiCacheSegMappingRegionBase,
1294 CI_CACHESEG_MAPPING_REGION_SIZE,
1295 0,
1296 &marea,
1297 FALSE,
1298 FALSE,
1299 BoundaryAddressMultiple);
1300 MmUnlockAddressSpace(MmGetKernelAddressSpace());
1301 if (!NT_SUCCESS(Status))
1302 {
1303 KEBUGCHECKCC;
1304 }
1305
1306 Buffer = ExAllocatePool(NonPagedPool, CI_CACHESEG_MAPPING_REGION_SIZE / (PAGE_SIZE * 8));
1307
1308 RtlInitializeBitMap(&CiCacheSegMappingRegionAllocMap, Buffer, CI_CACHESEG_MAPPING_REGION_SIZE / PAGE_SIZE);
1309 RtlClearAllBits(&CiCacheSegMappingRegionAllocMap);
1310
1311 KeInitializeSpinLock(&CiCacheSegMappingRegionLock);
1312 #endif
1313 InitializeListHead(&CacheSegmentListHead);
1314 InitializeListHead(&DirtySegmentListHead);
1315 InitializeListHead(&CacheSegmentLRUListHead);
1316 InitializeListHead(&ClosedListHead);
1317 ExInitializeFastMutex(&ViewLock);
1318 ExInitializeNPagedLookasideList (&iBcbLookasideList,
1319 NULL,
1320 NULL,
1321 0,
1322 sizeof(INTERNAL_BCB),
1323 TAG_IBCB,
1324 20);
1325 ExInitializeNPagedLookasideList (&BcbLookasideList,
1326 NULL,
1327 NULL,
1328 0,
1329 sizeof(BCB),
1330 TAG_BCB,
1331 20);
1332 ExInitializeNPagedLookasideList (&CacheSegLookasideList,
1333 NULL,
1334 NULL,
1335 0,
1336 sizeof(CACHE_SEGMENT),
1337 TAG_CSEG,
1338 20);
1339
1340 MmInitializeMemoryConsumer(MC_CACHE, CcRosTrimCache);
1341
1342 CcInitCacheZeroPage();
1343
1344 CcTimeStamp = 0;
1345 LazyCloseThreadShouldTerminate = FALSE;
1346 KeInitializeEvent (&LazyCloseThreadEvent, SynchronizationEvent, FALSE);
1347 Status = PsCreateSystemThread(&LazyCloseThreadHandle,
1348 THREAD_ALL_ACCESS,
1349 NULL,
1350 NULL,
1351 &LazyCloseThreadId,
1352 (PKSTART_ROUTINE)CmLazyCloseThreadMain,
1353 NULL);
1354 if (NT_SUCCESS(Status))
1355 {
1356 Priority = LOW_REALTIME_PRIORITY;
1357 NtSetInformationThread(LazyCloseThreadHandle,
1358 ThreadPriority,
1359 &Priority,
1360 sizeof(Priority));
1361 }
1362
1363 }
1364
1365 /* EOF */
1366
1367
1368
1369
1370
1371
1372