706623b0a5327922e1fa10effc3401f76aa46f9e
[reactos.git] / ntoskrnl / cc / view.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/cc/view.c
5 * PURPOSE: Cache manager
6 *
7 * PROGRAMMERS: David Welch (welch@mcmail.com)
8 * Pierre Schweitzer (pierre@reactos.org)
9 */
10
11 /* NOTES **********************************************************************
12 *
13 * This is not the NT implementation of a file cache nor anything much like
14 * it.
15 *
16 * The general procedure for a filesystem to implement a read or write
17 * dispatch routine is as follows
18 *
19 * (1) If caching for the FCB hasn't been initiated then so do by calling
20 * CcInitializeFileCache.
21 *
22 * (2) For each 4k region which is being read or written obtain a cache page
23 * by calling CcRequestCachePage.
24 *
25 * (3) If either the page is being read or not completely written, and it is
26 * not up to date then read its data from the underlying medium. If the read
27 * fails then call CcReleaseCachePage with VALID as FALSE and return a error.
28 *
29 * (4) Copy the data into or out of the page as necessary.
30 *
31 * (5) Release the cache page
32 */
33 /* INCLUDES ******************************************************************/
34
35 #include <ntoskrnl.h>
36 #define NDEBUG
37 #include <debug.h>
38
39 #if defined (ALLOC_PRAGMA)
40 #pragma alloc_text(INIT, CcInitView)
41 #endif
42
43 /* GLOBALS *******************************************************************/
44
45 LIST_ENTRY DirtyVacbListHead;
46 static LIST_ENTRY VacbLruListHead;
47
48 KGUARDED_MUTEX ViewLock;
49
50 NPAGED_LOOKASIDE_LIST iBcbLookasideList;
51 static NPAGED_LOOKASIDE_LIST SharedCacheMapLookasideList;
52 static NPAGED_LOOKASIDE_LIST VacbLookasideList;
53
54 /* Counters:
55 * - Amount of pages flushed by lazy writer
56 * - Number of times lazy writer ran
57 */
58 ULONG CcLazyWritePages = 0;
59 ULONG CcLazyWriteIos = 0;
60
61 /* Internal vars (MS):
62 * - Threshold above which lazy writer will start action
63 * - Amount of dirty pages
64 * - List for deferred writes
65 * - Spinlock when dealing with the deferred list
66 * - List for "clean" shared cache maps
67 */
68 ULONG CcDirtyPageThreshold = 0;
69 ULONG CcTotalDirtyPages = 0;
70 LIST_ENTRY CcDeferredWrites;
71 KSPIN_LOCK CcDeferredWriteSpinLock;
72 LIST_ENTRY CcCleanSharedCacheMapList;
73
74 /* Internal vars (ROS):
75 * - Event to notify lazy writer to shutdown
76 * - Event to inform watchers lazy writer is done for this loop
77 * - Lock for the CcCleanSharedCacheMapList list
78 */
79 KEVENT iLazyWriterShutdown;
80 KEVENT iLazyWriterNotify;
81 KSPIN_LOCK iSharedCacheMapLock;
82
83 #if DBG
84 static void CcRosVacbIncRefCount_(PROS_VACB vacb, const char* file, int line)
85 {
86 ++vacb->ReferenceCount;
87 if (vacb->SharedCacheMap->Trace)
88 {
89 DbgPrint("(%s:%i) VACB %p ++RefCount=%lu, Dirty %u, PageOut %lu\n",
90 file, line, vacb, vacb->ReferenceCount, vacb->Dirty, vacb->PageOut);
91 }
92 }
93 static void CcRosVacbDecRefCount_(PROS_VACB vacb, const char* file, int line)
94 {
95 --vacb->ReferenceCount;
96 if (vacb->SharedCacheMap->Trace)
97 {
98 DbgPrint("(%s:%i) VACB %p --RefCount=%lu, Dirty %u, PageOut %lu\n",
99 file, line, vacb, vacb->ReferenceCount, vacb->Dirty, vacb->PageOut);
100 }
101 }
102 #define CcRosVacbIncRefCount(vacb) CcRosVacbIncRefCount_(vacb,__FILE__,__LINE__)
103 #define CcRosVacbDecRefCount(vacb) CcRosVacbDecRefCount_(vacb,__FILE__,__LINE__)
104 #else
105 #define CcRosVacbIncRefCount(vacb) (++((vacb)->ReferenceCount))
106 #define CcRosVacbDecRefCount(vacb) (--((vacb)->ReferenceCount))
107 #endif
108
109 NTSTATUS
110 CcRosInternalFreeVacb(PROS_VACB Vacb);
111
112
113 /* FUNCTIONS *****************************************************************/
114
115 VOID
116 NTAPI
117 CcRosTraceCacheMap (
118 PROS_SHARED_CACHE_MAP SharedCacheMap,
119 BOOLEAN Trace )
120 {
121 #if DBG
122 KIRQL oldirql;
123 PLIST_ENTRY current_entry;
124 PROS_VACB current;
125
126 if (!SharedCacheMap)
127 return;
128
129 SharedCacheMap->Trace = Trace;
130
131 if (Trace)
132 {
133 DPRINT1("Enabling Tracing for CacheMap 0x%p:\n", SharedCacheMap);
134
135 KeAcquireGuardedMutex(&ViewLock);
136 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldirql);
137
138 current_entry = SharedCacheMap->CacheMapVacbListHead.Flink;
139 while (current_entry != &SharedCacheMap->CacheMapVacbListHead)
140 {
141 current = CONTAINING_RECORD(current_entry, ROS_VACB, CacheMapVacbListEntry);
142 current_entry = current_entry->Flink;
143
144 DPRINT1(" VACB 0x%p enabled, RefCount %lu, Dirty %u, PageOut %lu\n",
145 current, current->ReferenceCount, current->Dirty, current->PageOut );
146 }
147 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldirql);
148 KeReleaseGuardedMutex(&ViewLock);
149 }
150 else
151 {
152 DPRINT1("Disabling Tracing for CacheMap 0x%p:\n", SharedCacheMap);
153 }
154
155 #else
156 UNREFERENCED_PARAMETER(SharedCacheMap);
157 UNREFERENCED_PARAMETER(Trace);
158 #endif
159 }
160
161 NTSTATUS
162 NTAPI
163 CcRosFlushVacb (
164 PROS_VACB Vacb)
165 {
166 NTSTATUS Status;
167 KIRQL oldIrql;
168
169 Status = CcWriteVirtualAddress(Vacb);
170 if (NT_SUCCESS(Status))
171 {
172 KeAcquireGuardedMutex(&ViewLock);
173 KeAcquireSpinLock(&Vacb->SharedCacheMap->CacheMapLock, &oldIrql);
174
175 Vacb->Dirty = FALSE;
176 RemoveEntryList(&Vacb->DirtyVacbListEntry);
177 CcTotalDirtyPages -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
178 Vacb->SharedCacheMap->DirtyPages -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
179 CcRosVacbDecRefCount(Vacb);
180
181 KeReleaseSpinLock(&Vacb->SharedCacheMap->CacheMapLock, oldIrql);
182 KeReleaseGuardedMutex(&ViewLock);
183 }
184
185 return Status;
186 }
187
188 NTSTATUS
189 NTAPI
190 CcRosFlushDirtyPages (
191 ULONG Target,
192 PULONG Count,
193 BOOLEAN Wait,
194 BOOLEAN CalledFromLazy)
195 {
196 PLIST_ENTRY current_entry;
197 PROS_VACB current;
198 BOOLEAN Locked;
199 NTSTATUS Status;
200 LARGE_INTEGER ZeroTimeout;
201
202 DPRINT("CcRosFlushDirtyPages(Target %lu)\n", Target);
203
204 (*Count) = 0;
205 ZeroTimeout.QuadPart = 0;
206
207 KeEnterCriticalRegion();
208 KeAcquireGuardedMutex(&ViewLock);
209
210 current_entry = DirtyVacbListHead.Flink;
211 if (current_entry == &DirtyVacbListHead)
212 {
213 DPRINT("No Dirty pages\n");
214 }
215
216 while ((current_entry != &DirtyVacbListHead) && (Target > 0))
217 {
218 current = CONTAINING_RECORD(current_entry,
219 ROS_VACB,
220 DirtyVacbListEntry);
221 current_entry = current_entry->Flink;
222
223 CcRosVacbIncRefCount(current);
224
225 /* When performing lazy write, don't handle temporary files */
226 if (CalledFromLazy &&
227 BooleanFlagOn(current->SharedCacheMap->FileObject->Flags, FO_TEMPORARY_FILE))
228 {
229 CcRosVacbDecRefCount(current);
230 continue;
231 }
232
233 Locked = current->SharedCacheMap->Callbacks->AcquireForLazyWrite(
234 current->SharedCacheMap->LazyWriteContext, Wait);
235 if (!Locked)
236 {
237 CcRosVacbDecRefCount(current);
238 continue;
239 }
240
241 Status = CcRosAcquireVacbLock(current,
242 Wait ? NULL : &ZeroTimeout);
243 if (Status != STATUS_SUCCESS)
244 {
245 current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
246 current->SharedCacheMap->LazyWriteContext);
247 CcRosVacbDecRefCount(current);
248 continue;
249 }
250
251 ASSERT(current->Dirty);
252
253 /* One reference is added above */
254 if ((current->ReferenceCount > 2 && current->PinCount == 0) ||
255 (current->ReferenceCount > 3 && current->PinCount > 1))
256 {
257 CcRosReleaseVacbLock(current);
258 current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
259 current->SharedCacheMap->LazyWriteContext);
260 CcRosVacbDecRefCount(current);
261 continue;
262 }
263
264 KeReleaseGuardedMutex(&ViewLock);
265
266 Status = CcRosFlushVacb(current);
267
268 CcRosReleaseVacbLock(current);
269 current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
270 current->SharedCacheMap->LazyWriteContext);
271
272 KeAcquireGuardedMutex(&ViewLock);
273 CcRosVacbDecRefCount(current);
274
275 if (!NT_SUCCESS(Status) && (Status != STATUS_END_OF_FILE) &&
276 (Status != STATUS_MEDIA_WRITE_PROTECTED))
277 {
278 DPRINT1("CC: Failed to flush VACB.\n");
279 }
280 else
281 {
282 ULONG PagesFreed;
283
284 /* How many pages did we free? */
285 PagesFreed = VACB_MAPPING_GRANULARITY / PAGE_SIZE;
286 (*Count) += PagesFreed;
287
288 /* Make sure we don't overflow target! */
289 if (Target < PagesFreed)
290 {
291 /* If we would have, jump to zero directly */
292 Target = 0;
293 }
294 else
295 {
296 Target -= PagesFreed;
297 }
298 }
299
300 current_entry = DirtyVacbListHead.Flink;
301 }
302
303 KeReleaseGuardedMutex(&ViewLock);
304 KeLeaveCriticalRegion();
305
306 DPRINT("CcRosFlushDirtyPages() finished\n");
307 return STATUS_SUCCESS;
308 }
309
310 /* FIXME: Someday this could somewhat implement write-behind/read-ahead */
311 VOID
312 NTAPI
313 CciLazyWriter(PVOID Unused)
314 {
315 LARGE_INTEGER OneSecond;
316
317 OneSecond.QuadPart = (LONGLONG)-1*1000*1000*10;
318
319 while (TRUE)
320 {
321 NTSTATUS Status;
322 PLIST_ENTRY ListEntry;
323 ULONG Target, Count = 0;
324
325 /* One per second or until we have to stop */
326 Status = KeWaitForSingleObject(&iLazyWriterShutdown,
327 Executive,
328 KernelMode,
329 FALSE,
330 &OneSecond);
331
332 /* If we succeeed, we've to stop running! */
333 if (Status == STATUS_SUCCESS)
334 {
335 break;
336 }
337
338 /* We're not sleeping anymore */
339 KeClearEvent(&iLazyWriterNotify);
340
341 /* Our target is one-eighth of the dirty pages */
342 Target = CcTotalDirtyPages / 8;
343 if (Target != 0)
344 {
345 /* Flush! */
346 DPRINT("Lazy writer starting (%d)\n", Target);
347 CcRosFlushDirtyPages(Target, &Count, FALSE, TRUE);
348
349 /* And update stats */
350 CcLazyWritePages += Count;
351 ++CcLazyWriteIos;
352 DPRINT("Lazy writer done (%d)\n", Count);
353 }
354
355 /* Inform people waiting on us that we're done */
356 KeSetEvent(&iLazyWriterNotify, IO_DISK_INCREMENT, FALSE);
357
358 /* Likely not optimal, but let's handle one deferred write now! */
359 ListEntry = ExInterlockedRemoveHeadList(&CcDeferredWrites, &CcDeferredWriteSpinLock);
360 if (ListEntry != NULL)
361 {
362 PROS_DEFERRED_WRITE_CONTEXT Context;
363
364 /* Extract the context */
365 Context = CONTAINING_RECORD(ListEntry, ROS_DEFERRED_WRITE_CONTEXT, CcDeferredWritesEntry);
366
367 /* Can we write now? */
368 if (CcCanIWrite(Context->FileObject, Context->BytesToWrite, FALSE, Context->Retrying))
369 {
370 /* Yes! Do it, and destroy the associated context */
371 Context->PostRoutine(Context->Context1, Context->Context2);
372 ExFreePoolWithTag(Context, 'CcDw');
373 }
374 else
375 {
376 /* Otherwise, requeue it, but in tail, so that it doesn't block others
377 * This is clearly to improve, but given the poor algorithm used now
378 * It's better than nothing!
379 */
380 ExInterlockedInsertTailList(&CcDeferredWrites,
381 &Context->CcDeferredWritesEntry,
382 &CcDeferredWriteSpinLock);
383 }
384 }
385 }
386 }
387
388 NTSTATUS
389 CcRosTrimCache (
390 ULONG Target,
391 ULONG Priority,
392 PULONG NrFreed)
393 /*
394 * FUNCTION: Try to free some memory from the file cache.
395 * ARGUMENTS:
396 * Target - The number of pages to be freed.
397 * Priority - The priority of free (currently unused).
398 * NrFreed - Points to a variable where the number of pages
399 * actually freed is returned.
400 */
401 {
402 PLIST_ENTRY current_entry;
403 PROS_VACB current;
404 ULONG PagesFreed;
405 KIRQL oldIrql;
406 LIST_ENTRY FreeList;
407 PFN_NUMBER Page;
408 ULONG i;
409 BOOLEAN FlushedPages = FALSE;
410
411 DPRINT("CcRosTrimCache(Target %lu)\n", Target);
412
413 InitializeListHead(&FreeList);
414
415 *NrFreed = 0;
416
417 retry:
418 KeAcquireGuardedMutex(&ViewLock);
419
420 current_entry = VacbLruListHead.Flink;
421 while (current_entry != &VacbLruListHead)
422 {
423 current = CONTAINING_RECORD(current_entry,
424 ROS_VACB,
425 VacbLruListEntry);
426 current_entry = current_entry->Flink;
427
428 KeAcquireSpinLock(&current->SharedCacheMap->CacheMapLock, &oldIrql);
429
430 /* Reference the VACB */
431 CcRosVacbIncRefCount(current);
432
433 /* Check if it's mapped and not dirty */
434 if (current->MappedCount > 0 && !current->Dirty)
435 {
436 /* We have to break these locks because Cc sucks */
437 KeReleaseSpinLock(&current->SharedCacheMap->CacheMapLock, oldIrql);
438 KeReleaseGuardedMutex(&ViewLock);
439
440 /* Page out the VACB */
441 for (i = 0; i < VACB_MAPPING_GRANULARITY / PAGE_SIZE; i++)
442 {
443 Page = (PFN_NUMBER)(MmGetPhysicalAddress((PUCHAR)current->BaseAddress + (i * PAGE_SIZE)).QuadPart >> PAGE_SHIFT);
444
445 MmPageOutPhysicalAddress(Page);
446 }
447
448 /* Reacquire the locks */
449 KeAcquireGuardedMutex(&ViewLock);
450 KeAcquireSpinLock(&current->SharedCacheMap->CacheMapLock, &oldIrql);
451 }
452
453 /* Dereference the VACB */
454 CcRosVacbDecRefCount(current);
455
456 /* Check if we can free this entry now */
457 if (current->ReferenceCount == 0)
458 {
459 ASSERT(!current->Dirty);
460 ASSERT(!current->MappedCount);
461
462 RemoveEntryList(&current->CacheMapVacbListEntry);
463 RemoveEntryList(&current->VacbLruListEntry);
464 InsertHeadList(&FreeList, &current->CacheMapVacbListEntry);
465
466 /* Calculate how many pages we freed for Mm */
467 PagesFreed = min(VACB_MAPPING_GRANULARITY / PAGE_SIZE, Target);
468 Target -= PagesFreed;
469 (*NrFreed) += PagesFreed;
470 }
471
472 KeReleaseSpinLock(&current->SharedCacheMap->CacheMapLock, oldIrql);
473 }
474
475 KeReleaseGuardedMutex(&ViewLock);
476
477 /* Try flushing pages if we haven't met our target */
478 if ((Target > 0) && !FlushedPages)
479 {
480 /* Flush dirty pages to disk */
481 CcRosFlushDirtyPages(Target, &PagesFreed, FALSE, FALSE);
482 FlushedPages = TRUE;
483
484 /* We can only swap as many pages as we flushed */
485 if (PagesFreed < Target) Target = PagesFreed;
486
487 /* Check if we flushed anything */
488 if (PagesFreed != 0)
489 {
490 /* Try again after flushing dirty pages */
491 DPRINT("Flushed %lu dirty cache pages to disk\n", PagesFreed);
492 goto retry;
493 }
494 }
495
496 while (!IsListEmpty(&FreeList))
497 {
498 current_entry = RemoveHeadList(&FreeList);
499 current = CONTAINING_RECORD(current_entry,
500 ROS_VACB,
501 CacheMapVacbListEntry);
502 CcRosInternalFreeVacb(current);
503 }
504
505 DPRINT("Evicted %lu cache pages\n", (*NrFreed));
506
507 return STATUS_SUCCESS;
508 }
509
510 NTSTATUS
511 NTAPI
512 CcRosReleaseVacb (
513 PROS_SHARED_CACHE_MAP SharedCacheMap,
514 PROS_VACB Vacb,
515 BOOLEAN Valid,
516 BOOLEAN Dirty,
517 BOOLEAN Mapped)
518 {
519 BOOLEAN WasDirty;
520
521 ASSERT(SharedCacheMap);
522
523 DPRINT("CcRosReleaseVacb(SharedCacheMap 0x%p, Vacb 0x%p, Valid %u)\n",
524 SharedCacheMap, Vacb, Valid);
525
526 Vacb->Valid = Valid;
527
528 WasDirty = FALSE;
529 if (Dirty)
530 {
531 if (!Vacb->Dirty && Dirty)
532 {
533 CcRosMarkDirtyVacb(Vacb);
534 }
535 else
536 {
537 WasDirty = TRUE;
538 }
539 }
540
541 if (Mapped)
542 {
543 Vacb->MappedCount++;
544 }
545 CcRosVacbDecRefCount(Vacb);
546 if (Mapped && (Vacb->MappedCount == 1))
547 {
548 CcRosVacbIncRefCount(Vacb);
549 }
550 if (!WasDirty && Vacb->Dirty)
551 {
552 CcRosVacbIncRefCount(Vacb);
553 }
554
555 CcRosReleaseVacbLock(Vacb);
556
557 return STATUS_SUCCESS;
558 }
559
560 /* Returns with VACB Lock Held! */
561 PROS_VACB
562 NTAPI
563 CcRosLookupVacb (
564 PROS_SHARED_CACHE_MAP SharedCacheMap,
565 LONGLONG FileOffset)
566 {
567 PLIST_ENTRY current_entry;
568 PROS_VACB current;
569 KIRQL oldIrql;
570
571 ASSERT(SharedCacheMap);
572
573 DPRINT("CcRosLookupVacb(SharedCacheMap 0x%p, FileOffset %I64u)\n",
574 SharedCacheMap, FileOffset);
575
576 KeAcquireGuardedMutex(&ViewLock);
577 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
578
579 current_entry = SharedCacheMap->CacheMapVacbListHead.Flink;
580 while (current_entry != &SharedCacheMap->CacheMapVacbListHead)
581 {
582 current = CONTAINING_RECORD(current_entry,
583 ROS_VACB,
584 CacheMapVacbListEntry);
585 if (IsPointInRange(current->FileOffset.QuadPart,
586 VACB_MAPPING_GRANULARITY,
587 FileOffset))
588 {
589 CcRosVacbIncRefCount(current);
590 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
591 KeReleaseGuardedMutex(&ViewLock);
592 CcRosAcquireVacbLock(current, NULL);
593 return current;
594 }
595 if (current->FileOffset.QuadPart > FileOffset)
596 break;
597 current_entry = current_entry->Flink;
598 }
599
600 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
601 KeReleaseGuardedMutex(&ViewLock);
602
603 return NULL;
604 }
605
606 VOID
607 NTAPI
608 CcRosMarkDirtyVacb (
609 PROS_VACB Vacb)
610 {
611 KIRQL oldIrql;
612 PROS_SHARED_CACHE_MAP SharedCacheMap;
613
614 SharedCacheMap = Vacb->SharedCacheMap;
615
616 KeAcquireGuardedMutex(&ViewLock);
617 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
618
619 if (!Vacb->Dirty)
620 {
621 InsertTailList(&DirtyVacbListHead, &Vacb->DirtyVacbListEntry);
622 CcTotalDirtyPages += VACB_MAPPING_GRANULARITY / PAGE_SIZE;
623 Vacb->SharedCacheMap->DirtyPages += VACB_MAPPING_GRANULARITY / PAGE_SIZE;
624 }
625 else
626 {
627 CcRosVacbDecRefCount(Vacb);
628 }
629
630 /* Move to the tail of the LRU list */
631 RemoveEntryList(&Vacb->VacbLruListEntry);
632 InsertTailList(&VacbLruListHead, &Vacb->VacbLruListEntry);
633
634 Vacb->Dirty = TRUE;
635
636 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
637 KeReleaseGuardedMutex(&ViewLock);
638 }
639
640 NTSTATUS
641 NTAPI
642 CcRosMarkDirtyFile (
643 PROS_SHARED_CACHE_MAP SharedCacheMap,
644 LONGLONG FileOffset)
645 {
646 PROS_VACB Vacb;
647
648 ASSERT(SharedCacheMap);
649
650 DPRINT("CcRosMarkDirtyVacb(SharedCacheMap 0x%p, FileOffset %I64u)\n",
651 SharedCacheMap, FileOffset);
652
653 Vacb = CcRosLookupVacb(SharedCacheMap, FileOffset);
654 if (Vacb == NULL)
655 {
656 KeBugCheck(CACHE_MANAGER);
657 }
658
659 CcRosMarkDirtyVacb(Vacb);
660
661
662 CcRosReleaseVacbLock(Vacb);
663
664 return STATUS_SUCCESS;
665 }
666
667 NTSTATUS
668 NTAPI
669 CcRosUnmapVacb (
670 PROS_SHARED_CACHE_MAP SharedCacheMap,
671 LONGLONG FileOffset,
672 BOOLEAN NowDirty)
673 {
674 PROS_VACB Vacb;
675 BOOLEAN WasDirty;
676
677 ASSERT(SharedCacheMap);
678
679 DPRINT("CcRosUnmapVacb(SharedCacheMap 0x%p, FileOffset %I64u, NowDirty %u)\n",
680 SharedCacheMap, FileOffset, NowDirty);
681
682 Vacb = CcRosLookupVacb(SharedCacheMap, FileOffset);
683 if (Vacb == NULL)
684 {
685 return STATUS_UNSUCCESSFUL;
686 }
687
688 WasDirty = FALSE;
689 if (NowDirty)
690 {
691 if (!Vacb->Dirty && NowDirty)
692 {
693 CcRosMarkDirtyVacb(Vacb);
694 }
695 else
696 {
697 WasDirty = TRUE;
698 }
699 }
700
701 Vacb->MappedCount--;
702
703 CcRosVacbDecRefCount(Vacb);
704 if (!WasDirty && NowDirty)
705 {
706 CcRosVacbIncRefCount(Vacb);
707 }
708 if (Vacb->MappedCount == 0)
709 {
710 CcRosVacbDecRefCount(Vacb);
711 }
712
713 CcRosReleaseVacbLock(Vacb);
714
715 return STATUS_SUCCESS;
716 }
717
718 static
719 NTSTATUS
720 CcRosMapVacb(
721 PROS_VACB Vacb)
722 {
723 ULONG i;
724 NTSTATUS Status;
725 ULONG_PTR NumberOfPages;
726
727 /* Create a memory area. */
728 MmLockAddressSpace(MmGetKernelAddressSpace());
729 Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
730 0, // nothing checks for VACB mareas, so set to 0
731 &Vacb->BaseAddress,
732 VACB_MAPPING_GRANULARITY,
733 PAGE_READWRITE,
734 (PMEMORY_AREA*)&Vacb->MemoryArea,
735 0,
736 PAGE_SIZE);
737 MmUnlockAddressSpace(MmGetKernelAddressSpace());
738 if (!NT_SUCCESS(Status))
739 {
740 DPRINT1("MmCreateMemoryArea failed with %lx for VACB %p\n", Status, Vacb);
741 return Status;
742 }
743
744 ASSERT(((ULONG_PTR)Vacb->BaseAddress % PAGE_SIZE) == 0);
745 ASSERT((ULONG_PTR)Vacb->BaseAddress > (ULONG_PTR)MmSystemRangeStart);
746
747 /* Create a virtual mapping for this memory area */
748 NumberOfPages = BYTES_TO_PAGES(VACB_MAPPING_GRANULARITY);
749 for (i = 0; i < NumberOfPages; i++)
750 {
751 PFN_NUMBER PageFrameNumber;
752
753 MI_SET_USAGE(MI_USAGE_CACHE);
754 Status = MmRequestPageMemoryConsumer(MC_CACHE, TRUE, &PageFrameNumber);
755 if (PageFrameNumber == 0)
756 {
757 DPRINT1("Unable to allocate page\n");
758 KeBugCheck(MEMORY_MANAGEMENT);
759 }
760
761 Status = MmCreateVirtualMapping(NULL,
762 (PVOID)((ULONG_PTR)Vacb->BaseAddress + (i * PAGE_SIZE)),
763 PAGE_READWRITE,
764 &PageFrameNumber,
765 1);
766 if (!NT_SUCCESS(Status))
767 {
768 DPRINT1("Unable to create virtual mapping\n");
769 KeBugCheck(MEMORY_MANAGEMENT);
770 }
771 }
772
773 return STATUS_SUCCESS;
774 }
775
776 static
777 NTSTATUS
778 CcRosCreateVacb (
779 PROS_SHARED_CACHE_MAP SharedCacheMap,
780 LONGLONG FileOffset,
781 PROS_VACB *Vacb)
782 {
783 PROS_VACB current;
784 PROS_VACB previous;
785 PLIST_ENTRY current_entry;
786 NTSTATUS Status;
787 KIRQL oldIrql;
788
789 ASSERT(SharedCacheMap);
790
791 DPRINT("CcRosCreateVacb()\n");
792
793 if (FileOffset >= SharedCacheMap->SectionSize.QuadPart)
794 {
795 *Vacb = NULL;
796 return STATUS_INVALID_PARAMETER;
797 }
798
799 current = ExAllocateFromNPagedLookasideList(&VacbLookasideList);
800 current->BaseAddress = NULL;
801 current->Valid = FALSE;
802 current->Dirty = FALSE;
803 current->PageOut = FALSE;
804 current->FileOffset.QuadPart = ROUND_DOWN(FileOffset, VACB_MAPPING_GRANULARITY);
805 current->SharedCacheMap = SharedCacheMap;
806 #if DBG
807 if (SharedCacheMap->Trace)
808 {
809 DPRINT1("CacheMap 0x%p: new VACB: 0x%p\n", SharedCacheMap, current);
810 }
811 #endif
812 current->MappedCount = 0;
813 current->DirtyVacbListEntry.Flink = NULL;
814 current->DirtyVacbListEntry.Blink = NULL;
815 current->ReferenceCount = 1;
816 current->PinCount = 0;
817 KeInitializeMutex(&current->Mutex, 0);
818 CcRosAcquireVacbLock(current, NULL);
819 KeAcquireGuardedMutex(&ViewLock);
820
821 *Vacb = current;
822 /* There is window between the call to CcRosLookupVacb
823 * and CcRosCreateVacb. We must check if a VACB for the
824 * file offset exist. If there is a VACB, we release
825 * our newly created VACB and return the existing one.
826 */
827 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
828 current_entry = SharedCacheMap->CacheMapVacbListHead.Flink;
829 previous = NULL;
830 while (current_entry != &SharedCacheMap->CacheMapVacbListHead)
831 {
832 current = CONTAINING_RECORD(current_entry,
833 ROS_VACB,
834 CacheMapVacbListEntry);
835 if (IsPointInRange(current->FileOffset.QuadPart,
836 VACB_MAPPING_GRANULARITY,
837 FileOffset))
838 {
839 CcRosVacbIncRefCount(current);
840 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
841 #if DBG
842 if (SharedCacheMap->Trace)
843 {
844 DPRINT1("CacheMap 0x%p: deleting newly created VACB 0x%p ( found existing one 0x%p )\n",
845 SharedCacheMap,
846 (*Vacb),
847 current);
848 }
849 #endif
850 CcRosReleaseVacbLock(*Vacb);
851 KeReleaseGuardedMutex(&ViewLock);
852 ExFreeToNPagedLookasideList(&VacbLookasideList, *Vacb);
853 *Vacb = current;
854 CcRosAcquireVacbLock(current, NULL);
855 return STATUS_SUCCESS;
856 }
857 if (current->FileOffset.QuadPart < FileOffset)
858 {
859 ASSERT(previous == NULL ||
860 previous->FileOffset.QuadPart < current->FileOffset.QuadPart);
861 previous = current;
862 }
863 if (current->FileOffset.QuadPart > FileOffset)
864 break;
865 current_entry = current_entry->Flink;
866 }
867 /* There was no existing VACB. */
868 current = *Vacb;
869 if (previous)
870 {
871 InsertHeadList(&previous->CacheMapVacbListEntry, &current->CacheMapVacbListEntry);
872 }
873 else
874 {
875 InsertHeadList(&SharedCacheMap->CacheMapVacbListHead, &current->CacheMapVacbListEntry);
876 }
877 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
878 InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
879 KeReleaseGuardedMutex(&ViewLock);
880
881 MI_SET_USAGE(MI_USAGE_CACHE);
882 #if MI_TRACE_PFNS
883 if ((SharedCacheMap->FileObject) && (SharedCacheMap->FileObject->FileName.Buffer))
884 {
885 PWCHAR pos;
886 ULONG len = 0;
887 pos = wcsrchr(SharedCacheMap->FileObject->FileName.Buffer, '\\');
888 if (pos)
889 {
890 len = wcslen(pos) * sizeof(WCHAR);
891 snprintf(MI_PFN_CURRENT_PROCESS_NAME, min(16, len), "%S", pos);
892 }
893 else
894 {
895 snprintf(MI_PFN_CURRENT_PROCESS_NAME, min(16, len), "%wZ", &SharedCacheMap->FileObject->FileName);
896 }
897 }
898 #endif
899
900 Status = CcRosMapVacb(current);
901 if (!NT_SUCCESS(Status))
902 {
903 RemoveEntryList(&current->CacheMapVacbListEntry);
904 RemoveEntryList(&current->VacbLruListEntry);
905 CcRosReleaseVacbLock(current);
906 ExFreeToNPagedLookasideList(&VacbLookasideList, current);
907 }
908
909 return Status;
910 }
911
912 NTSTATUS
913 NTAPI
914 CcRosGetVacb (
915 PROS_SHARED_CACHE_MAP SharedCacheMap,
916 LONGLONG FileOffset,
917 PLONGLONG BaseOffset,
918 PVOID* BaseAddress,
919 PBOOLEAN UptoDate,
920 PROS_VACB *Vacb)
921 {
922 PROS_VACB current;
923 NTSTATUS Status;
924
925 ASSERT(SharedCacheMap);
926
927 DPRINT("CcRosGetVacb()\n");
928
929 /*
930 * Look for a VACB already mapping the same data.
931 */
932 current = CcRosLookupVacb(SharedCacheMap, FileOffset);
933 if (current == NULL)
934 {
935 /*
936 * Otherwise create a new VACB.
937 */
938 Status = CcRosCreateVacb(SharedCacheMap, FileOffset, &current);
939 if (!NT_SUCCESS(Status))
940 {
941 return Status;
942 }
943 }
944
945 KeAcquireGuardedMutex(&ViewLock);
946
947 /* Move to the tail of the LRU list */
948 RemoveEntryList(&current->VacbLruListEntry);
949 InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
950
951 KeReleaseGuardedMutex(&ViewLock);
952
953 /*
954 * Return information about the VACB to the caller.
955 */
956 *UptoDate = current->Valid;
957 *BaseAddress = current->BaseAddress;
958 DPRINT("*BaseAddress %p\n", *BaseAddress);
959 *Vacb = current;
960 *BaseOffset = current->FileOffset.QuadPart;
961 return STATUS_SUCCESS;
962 }
963
964 NTSTATUS
965 NTAPI
966 CcRosRequestVacb (
967 PROS_SHARED_CACHE_MAP SharedCacheMap,
968 LONGLONG FileOffset,
969 PVOID* BaseAddress,
970 PBOOLEAN UptoDate,
971 PROS_VACB *Vacb)
972 /*
973 * FUNCTION: Request a page mapping for a shared cache map
974 */
975 {
976 LONGLONG BaseOffset;
977
978 ASSERT(SharedCacheMap);
979
980 if (FileOffset % VACB_MAPPING_GRANULARITY != 0)
981 {
982 DPRINT1("Bad fileoffset %I64x should be multiple of %x",
983 FileOffset, VACB_MAPPING_GRANULARITY);
984 KeBugCheck(CACHE_MANAGER);
985 }
986
987 return CcRosGetVacb(SharedCacheMap,
988 FileOffset,
989 &BaseOffset,
990 BaseAddress,
991 UptoDate,
992 Vacb);
993 }
994
995 static
996 VOID
997 CcFreeCachePage (
998 PVOID Context,
999 MEMORY_AREA* MemoryArea,
1000 PVOID Address,
1001 PFN_NUMBER Page,
1002 SWAPENTRY SwapEntry,
1003 BOOLEAN Dirty)
1004 {
1005 ASSERT(SwapEntry == 0);
1006 if (Page != 0)
1007 {
1008 ASSERT(MmGetReferenceCountPage(Page) == 1);
1009 MmReleasePageMemoryConsumer(MC_CACHE, Page);
1010 }
1011 }
1012
1013 NTSTATUS
1014 CcRosInternalFreeVacb (
1015 PROS_VACB Vacb)
1016 /*
1017 * FUNCTION: Releases a VACB associated with a shared cache map
1018 */
1019 {
1020 DPRINT("Freeing VACB 0x%p\n", Vacb);
1021 #if DBG
1022 if (Vacb->SharedCacheMap->Trace)
1023 {
1024 DPRINT1("CacheMap 0x%p: deleting VACB: 0x%p\n", Vacb->SharedCacheMap, Vacb);
1025 }
1026 #endif
1027
1028 MmLockAddressSpace(MmGetKernelAddressSpace());
1029 MmFreeMemoryArea(MmGetKernelAddressSpace(),
1030 Vacb->MemoryArea,
1031 CcFreeCachePage,
1032 NULL);
1033 MmUnlockAddressSpace(MmGetKernelAddressSpace());
1034
1035 ExFreeToNPagedLookasideList(&VacbLookasideList, Vacb);
1036 return STATUS_SUCCESS;
1037 }
1038
1039 /*
1040 * @implemented
1041 */
1042 VOID
1043 NTAPI
1044 CcFlushCache (
1045 IN PSECTION_OBJECT_POINTERS SectionObjectPointers,
1046 IN PLARGE_INTEGER FileOffset OPTIONAL,
1047 IN ULONG Length,
1048 OUT PIO_STATUS_BLOCK IoStatus)
1049 {
1050 PROS_SHARED_CACHE_MAP SharedCacheMap;
1051 LARGE_INTEGER Offset;
1052 LONGLONG RemainingLength;
1053 PROS_VACB current;
1054 NTSTATUS Status;
1055 KIRQL oldIrql;
1056
1057 CCTRACE(CC_API_DEBUG, "SectionObjectPointers=%p FileOffset=%p Length=%lu\n",
1058 SectionObjectPointers, FileOffset, Length);
1059
1060 DPRINT("CcFlushCache(SectionObjectPointers 0x%p, FileOffset 0x%p, Length %lu, IoStatus 0x%p)\n",
1061 SectionObjectPointers, FileOffset, Length, IoStatus);
1062
1063 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
1064 {
1065 SharedCacheMap = SectionObjectPointers->SharedCacheMap;
1066 ASSERT(SharedCacheMap);
1067 if (FileOffset)
1068 {
1069 Offset = *FileOffset;
1070 RemainingLength = Length;
1071 }
1072 else
1073 {
1074 Offset.QuadPart = 0;
1075 RemainingLength = SharedCacheMap->FileSize.QuadPart;
1076 }
1077
1078 if (IoStatus)
1079 {
1080 IoStatus->Status = STATUS_SUCCESS;
1081 IoStatus->Information = 0;
1082 }
1083
1084 while (RemainingLength > 0)
1085 {
1086 current = CcRosLookupVacb(SharedCacheMap, Offset.QuadPart);
1087 if (current != NULL)
1088 {
1089 if (current->Dirty)
1090 {
1091 Status = CcRosFlushVacb(current);
1092 if (!NT_SUCCESS(Status) && IoStatus != NULL)
1093 {
1094 IoStatus->Status = Status;
1095 }
1096 }
1097
1098 CcRosReleaseVacbLock(current);
1099
1100 KeAcquireGuardedMutex(&ViewLock);
1101 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
1102 CcRosVacbDecRefCount(current);
1103 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
1104 KeReleaseGuardedMutex(&ViewLock);
1105 }
1106
1107 Offset.QuadPart += VACB_MAPPING_GRANULARITY;
1108 RemainingLength -= min(RemainingLength, VACB_MAPPING_GRANULARITY);
1109 }
1110 }
1111 else
1112 {
1113 if (IoStatus)
1114 {
1115 IoStatus->Status = STATUS_INVALID_PARAMETER;
1116 }
1117 }
1118 }
1119
1120 NTSTATUS
1121 NTAPI
1122 CcRosDeleteFileCache (
1123 PFILE_OBJECT FileObject,
1124 PROS_SHARED_CACHE_MAP SharedCacheMap)
1125 /*
1126 * FUNCTION: Releases the shared cache map associated with a file object
1127 */
1128 {
1129 PLIST_ENTRY current_entry;
1130 PROS_VACB current;
1131 LIST_ENTRY FreeList;
1132 KIRQL oldIrql;
1133
1134 ASSERT(SharedCacheMap);
1135
1136 SharedCacheMap->OpenCount++;
1137 KeReleaseGuardedMutex(&ViewLock);
1138
1139 CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, NULL);
1140
1141 KeAcquireGuardedMutex(&ViewLock);
1142 SharedCacheMap->OpenCount--;
1143 if (SharedCacheMap->OpenCount == 0)
1144 {
1145 KIRQL OldIrql;
1146
1147 FileObject->SectionObjectPointer->SharedCacheMap = NULL;
1148
1149 /*
1150 * Release all VACBs
1151 */
1152 InitializeListHead(&FreeList);
1153 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
1154 while (!IsListEmpty(&SharedCacheMap->CacheMapVacbListHead))
1155 {
1156 current_entry = RemoveTailList(&SharedCacheMap->CacheMapVacbListHead);
1157 current = CONTAINING_RECORD(current_entry, ROS_VACB, CacheMapVacbListEntry);
1158 RemoveEntryList(&current->VacbLruListEntry);
1159 if (current->Dirty)
1160 {
1161 RemoveEntryList(&current->DirtyVacbListEntry);
1162 CcTotalDirtyPages -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
1163 current->SharedCacheMap->DirtyPages -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
1164 DPRINT1("Freeing dirty VACB\n");
1165 }
1166 InsertHeadList(&FreeList, &current->CacheMapVacbListEntry);
1167 }
1168 #if DBG
1169 SharedCacheMap->Trace = FALSE;
1170 #endif
1171 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
1172
1173 KeReleaseGuardedMutex(&ViewLock);
1174 ObDereferenceObject(SharedCacheMap->FileObject);
1175
1176 while (!IsListEmpty(&FreeList))
1177 {
1178 current_entry = RemoveTailList(&FreeList);
1179 current = CONTAINING_RECORD(current_entry, ROS_VACB, CacheMapVacbListEntry);
1180 CcRosInternalFreeVacb(current);
1181 }
1182
1183 KeAcquireSpinLock(&iSharedCacheMapLock, &OldIrql);
1184 RemoveEntryList(&SharedCacheMap->SharedCacheMapLinks);
1185 KeReleaseSpinLock(&iSharedCacheMapLock, OldIrql);
1186
1187 ExFreeToNPagedLookasideList(&SharedCacheMapLookasideList, SharedCacheMap);
1188 KeAcquireGuardedMutex(&ViewLock);
1189 }
1190 return STATUS_SUCCESS;
1191 }
1192
1193 VOID
1194 NTAPI
1195 CcRosReferenceCache (
1196 PFILE_OBJECT FileObject)
1197 {
1198 PROS_SHARED_CACHE_MAP SharedCacheMap;
1199 KeAcquireGuardedMutex(&ViewLock);
1200 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1201 ASSERT(SharedCacheMap);
1202 ASSERT(SharedCacheMap->OpenCount != 0);
1203 SharedCacheMap->OpenCount++;
1204 KeReleaseGuardedMutex(&ViewLock);
1205 }
1206
1207 VOID
1208 NTAPI
1209 CcRosRemoveIfClosed (
1210 PSECTION_OBJECT_POINTERS SectionObjectPointer)
1211 {
1212 PROS_SHARED_CACHE_MAP SharedCacheMap;
1213 DPRINT("CcRosRemoveIfClosed()\n");
1214 KeAcquireGuardedMutex(&ViewLock);
1215 SharedCacheMap = SectionObjectPointer->SharedCacheMap;
1216 if (SharedCacheMap && SharedCacheMap->OpenCount == 0)
1217 {
1218 CcRosDeleteFileCache(SharedCacheMap->FileObject, SharedCacheMap);
1219 }
1220 KeReleaseGuardedMutex(&ViewLock);
1221 }
1222
1223
1224 VOID
1225 NTAPI
1226 CcRosDereferenceCache (
1227 PFILE_OBJECT FileObject)
1228 {
1229 PROS_SHARED_CACHE_MAP SharedCacheMap;
1230 KeAcquireGuardedMutex(&ViewLock);
1231 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1232 ASSERT(SharedCacheMap);
1233 if (SharedCacheMap->OpenCount > 0)
1234 {
1235 SharedCacheMap->OpenCount--;
1236 if (SharedCacheMap->OpenCount == 0)
1237 {
1238 MmFreeSectionSegments(SharedCacheMap->FileObject);
1239 CcRosDeleteFileCache(FileObject, SharedCacheMap);
1240 }
1241 }
1242 KeReleaseGuardedMutex(&ViewLock);
1243 }
1244
1245 NTSTATUS
1246 NTAPI
1247 CcRosReleaseFileCache (
1248 PFILE_OBJECT FileObject)
1249 /*
1250 * FUNCTION: Called by the file system when a handle to a file object
1251 * has been closed.
1252 */
1253 {
1254 PROS_SHARED_CACHE_MAP SharedCacheMap;
1255
1256 KeAcquireGuardedMutex(&ViewLock);
1257
1258 if (FileObject->SectionObjectPointer->SharedCacheMap != NULL)
1259 {
1260 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1261 if (FileObject->PrivateCacheMap != NULL)
1262 {
1263 FileObject->PrivateCacheMap = NULL;
1264 if (SharedCacheMap->OpenCount > 0)
1265 {
1266 SharedCacheMap->OpenCount--;
1267 if (SharedCacheMap->OpenCount == 0)
1268 {
1269 MmFreeSectionSegments(SharedCacheMap->FileObject);
1270 CcRosDeleteFileCache(FileObject, SharedCacheMap);
1271 }
1272 }
1273 }
1274 }
1275 KeReleaseGuardedMutex(&ViewLock);
1276 return STATUS_SUCCESS;
1277 }
1278
1279 NTSTATUS
1280 NTAPI
1281 CcTryToInitializeFileCache (
1282 PFILE_OBJECT FileObject)
1283 {
1284 PROS_SHARED_CACHE_MAP SharedCacheMap;
1285 NTSTATUS Status;
1286
1287 KeAcquireGuardedMutex(&ViewLock);
1288
1289 ASSERT(FileObject->SectionObjectPointer);
1290 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1291 if (SharedCacheMap == NULL)
1292 {
1293 Status = STATUS_UNSUCCESSFUL;
1294 }
1295 else
1296 {
1297 if (FileObject->PrivateCacheMap == NULL)
1298 {
1299 FileObject->PrivateCacheMap = SharedCacheMap;
1300 SharedCacheMap->OpenCount++;
1301 }
1302 Status = STATUS_SUCCESS;
1303 }
1304 KeReleaseGuardedMutex(&ViewLock);
1305
1306 return Status;
1307 }
1308
1309
1310 NTSTATUS
1311 NTAPI
1312 CcRosInitializeFileCache (
1313 PFILE_OBJECT FileObject,
1314 PCC_FILE_SIZES FileSizes,
1315 BOOLEAN PinAccess,
1316 PCACHE_MANAGER_CALLBACKS CallBacks,
1317 PVOID LazyWriterContext)
1318 /*
1319 * FUNCTION: Initializes a shared cache map for a file object
1320 */
1321 {
1322 PROS_SHARED_CACHE_MAP SharedCacheMap;
1323
1324 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1325 DPRINT("CcRosInitializeFileCache(FileObject 0x%p, SharedCacheMap 0x%p)\n",
1326 FileObject, SharedCacheMap);
1327
1328 KeAcquireGuardedMutex(&ViewLock);
1329 if (SharedCacheMap == NULL)
1330 {
1331 KIRQL OldIrql;
1332
1333 SharedCacheMap = ExAllocateFromNPagedLookasideList(&SharedCacheMapLookasideList);
1334 if (SharedCacheMap == NULL)
1335 {
1336 KeReleaseGuardedMutex(&ViewLock);
1337 return STATUS_INSUFFICIENT_RESOURCES;
1338 }
1339 RtlZeroMemory(SharedCacheMap, sizeof(*SharedCacheMap));
1340 ObReferenceObjectByPointer(FileObject,
1341 FILE_ALL_ACCESS,
1342 NULL,
1343 KernelMode);
1344 SharedCacheMap->FileObject = FileObject;
1345 SharedCacheMap->Callbacks = CallBacks;
1346 SharedCacheMap->LazyWriteContext = LazyWriterContext;
1347 SharedCacheMap->SectionSize = FileSizes->AllocationSize;
1348 SharedCacheMap->FileSize = FileSizes->FileSize;
1349 SharedCacheMap->PinAccess = PinAccess;
1350 SharedCacheMap->DirtyPageThreshold = 0;
1351 SharedCacheMap->DirtyPages = 0;
1352 KeInitializeSpinLock(&SharedCacheMap->CacheMapLock);
1353 InitializeListHead(&SharedCacheMap->CacheMapVacbListHead);
1354 FileObject->SectionObjectPointer->SharedCacheMap = SharedCacheMap;
1355
1356 KeAcquireSpinLock(&iSharedCacheMapLock, &OldIrql);
1357 InsertTailList(&CcCleanSharedCacheMapList, &SharedCacheMap->SharedCacheMapLinks);
1358 KeReleaseSpinLock(&iSharedCacheMapLock, OldIrql);
1359 }
1360 if (FileObject->PrivateCacheMap == NULL)
1361 {
1362 FileObject->PrivateCacheMap = SharedCacheMap;
1363 SharedCacheMap->OpenCount++;
1364 }
1365 KeReleaseGuardedMutex(&ViewLock);
1366
1367 return STATUS_SUCCESS;
1368 }
1369
1370 /*
1371 * @implemented
1372 */
1373 PFILE_OBJECT
1374 NTAPI
1375 CcGetFileObjectFromSectionPtrs (
1376 IN PSECTION_OBJECT_POINTERS SectionObjectPointers)
1377 {
1378 PROS_SHARED_CACHE_MAP SharedCacheMap;
1379
1380 CCTRACE(CC_API_DEBUG, "SectionObjectPointers=%p\n", SectionObjectPointers);
1381
1382 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
1383 {
1384 SharedCacheMap = SectionObjectPointers->SharedCacheMap;
1385 ASSERT(SharedCacheMap);
1386 return SharedCacheMap->FileObject;
1387 }
1388 return NULL;
1389 }
1390
1391 VOID
1392 NTAPI
1393 CcShutdownLazyWriter (
1394 VOID)
1395 {
1396 /* Simply set the event, lazy writer will stop when it's done */
1397 KeSetEvent(&iLazyWriterShutdown, IO_DISK_INCREMENT, FALSE);
1398 }
1399
1400 BOOLEAN
1401 INIT_FUNCTION
1402 NTAPI
1403 CcInitView (
1404 VOID)
1405 {
1406 HANDLE LazyWriter;
1407 NTSTATUS Status;
1408 KPRIORITY Priority;
1409 OBJECT_ATTRIBUTES ObjectAttributes;
1410
1411 DPRINT("CcInitView()\n");
1412
1413 InitializeListHead(&DirtyVacbListHead);
1414 InitializeListHead(&VacbLruListHead);
1415 InitializeListHead(&CcDeferredWrites);
1416 InitializeListHead(&CcCleanSharedCacheMapList);
1417 KeInitializeSpinLock(&CcDeferredWriteSpinLock);
1418 KeInitializeSpinLock(&iSharedCacheMapLock);
1419 KeInitializeGuardedMutex(&ViewLock);
1420 ExInitializeNPagedLookasideList(&iBcbLookasideList,
1421 NULL,
1422 NULL,
1423 0,
1424 sizeof(INTERNAL_BCB),
1425 TAG_BCB,
1426 20);
1427 ExInitializeNPagedLookasideList(&SharedCacheMapLookasideList,
1428 NULL,
1429 NULL,
1430 0,
1431 sizeof(ROS_SHARED_CACHE_MAP),
1432 TAG_SHARED_CACHE_MAP,
1433 20);
1434 ExInitializeNPagedLookasideList(&VacbLookasideList,
1435 NULL,
1436 NULL,
1437 0,
1438 sizeof(ROS_VACB),
1439 TAG_VACB,
1440 20);
1441
1442 MmInitializeMemoryConsumer(MC_CACHE, CcRosTrimCache);
1443
1444 /* Initialize lazy writer events */
1445 KeInitializeEvent(&iLazyWriterShutdown, SynchronizationEvent, FALSE);
1446 KeInitializeEvent(&iLazyWriterNotify, NotificationEvent, FALSE);
1447
1448 /* Define lazy writer threshold, depending on system type */
1449 switch (MmQuerySystemSize())
1450 {
1451 case MmSmallSystem:
1452 CcDirtyPageThreshold = MmNumberOfPhysicalPages / 8;
1453 break;
1454
1455 case MmMediumSystem:
1456 CcDirtyPageThreshold = MmNumberOfPhysicalPages / 4;
1457 break;
1458
1459 case MmLargeSystem:
1460 CcDirtyPageThreshold = MmNumberOfPhysicalPages / 8 + MmNumberOfPhysicalPages / 4;
1461 break;
1462 }
1463
1464 /* Start the lazy writer thread */
1465 InitializeObjectAttributes(&ObjectAttributes,
1466 NULL,
1467 OBJ_KERNEL_HANDLE,
1468 NULL,
1469 NULL);
1470 Status = PsCreateSystemThread(&LazyWriter,
1471 THREAD_ALL_ACCESS,
1472 &ObjectAttributes,
1473 NULL,
1474 NULL,
1475 CciLazyWriter,
1476 NULL);
1477 if (!NT_SUCCESS(Status))
1478 {
1479 return FALSE;
1480 }
1481
1482 Priority = 27;
1483 Status = NtSetInformationThread(LazyWriter,
1484 ThreadPriority,
1485 &Priority,
1486 sizeof(Priority));
1487 ASSERT(NT_SUCCESS(Status));
1488
1489 /* Handle is not needed */
1490 ObCloseHandle(LazyWriter, KernelMode);
1491
1492 CcInitCacheZeroPage();
1493
1494 return TRUE;
1495 }
1496
1497 #if DBG && defined(KDBG)
1498 BOOLEAN
1499 ExpKdbgExtFileCache(ULONG Argc, PCHAR Argv[])
1500 {
1501 PLIST_ENTRY ListEntry;
1502 UNICODE_STRING NoName = RTL_CONSTANT_STRING(L"No name for File");
1503
1504 KdbpPrint(" Usage Summary (in kb)\n");
1505 KdbpPrint("Shared\t\tValid\tDirty\tName\n");
1506 /* No need to lock the spin lock here, we're in DBG */
1507 for (ListEntry = CcCleanSharedCacheMapList.Flink;
1508 ListEntry != &CcCleanSharedCacheMapList;
1509 ListEntry = ListEntry->Flink)
1510 {
1511 PLIST_ENTRY Vacbs;
1512 ULONG Valid = 0, Dirty = 0;
1513 PROS_SHARED_CACHE_MAP SharedCacheMap;
1514 PUNICODE_STRING FileName;
1515
1516 SharedCacheMap = CONTAINING_RECORD(ListEntry, ROS_SHARED_CACHE_MAP, SharedCacheMapLinks);
1517
1518 /* Dirty size */
1519 Dirty = (SharedCacheMap->DirtyPages * PAGE_SIZE) / 1024;
1520
1521 /* First, count for all the associated VACB */
1522 for (Vacbs = SharedCacheMap->CacheMapVacbListHead.Flink;
1523 Vacbs != &SharedCacheMap->CacheMapVacbListHead;
1524 Vacbs = Vacbs->Flink)
1525 {
1526 PROS_VACB Vacb;
1527
1528 Vacb = CONTAINING_RECORD(Vacbs, ROS_VACB, CacheMapVacbListEntry);
1529 if (Vacb->Valid)
1530 {
1531 Valid += VACB_MAPPING_GRANULARITY / 1024;
1532 }
1533 }
1534
1535 /* Setup name */
1536 if (SharedCacheMap->FileObject != NULL &&
1537 SharedCacheMap->FileObject->FileName.Length != 0)
1538 {
1539 FileName = &SharedCacheMap->FileObject->FileName;
1540 }
1541 else
1542 {
1543 FileName = &NoName;
1544 }
1545
1546 /* And print */
1547 KdbpPrint("%p\t%d\t%d\t%wZ\n", SharedCacheMap, Valid, Dirty, FileName);
1548 }
1549
1550 return TRUE;
1551 }
1552 #endif
1553
1554 /* EOF */