c2e2f89dd678a71cfbb2800b4b5561a96197b4e7
[reactos.git] / reactos / 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 */
9
10 /* NOTES **********************************************************************
11 *
12 * This is not the NT implementation of a file cache nor anything much like
13 * it.
14 *
15 * The general procedure for a filesystem to implement a read or write
16 * dispatch routine is as follows
17 *
18 * (1) If caching for the FCB hasn't been initiated then so do by calling
19 * CcInitializeFileCache.
20 *
21 * (2) For each 4k region which is being read or written obtain a cache page
22 * by calling CcRequestCachePage.
23 *
24 * (3) If either the page is being read or not completely written, and it is
25 * not up to date then read its data from the underlying medium. If the read
26 * fails then call CcReleaseCachePage with VALID as FALSE and return a error.
27 *
28 * (4) Copy the data into or out of the page as necessary.
29 *
30 * (5) Release the cache page
31 */
32 /* INCLUDES ******************************************************************/
33
34 #include <ntoskrnl.h>
35 #define NDEBUG
36 #include <debug.h>
37
38 #if defined (ALLOC_PRAGMA)
39 #pragma alloc_text(INIT, CcInitView)
40 #endif
41
42 /* GLOBALS *******************************************************************/
43
44 static LIST_ENTRY DirtyVacbListHead;
45 static LIST_ENTRY VacbLruListHead;
46 ULONG DirtyPageCount = 0;
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 #if DBG
55 static void CcRosVacbIncRefCount_(PROS_VACB vacb, const char* file, int line)
56 {
57 ++vacb->ReferenceCount;
58 if (vacb->SharedCacheMap->Trace)
59 {
60 DbgPrint("(%s:%i) VACB %p ++RefCount=%lu, Dirty %u, PageOut %lu\n",
61 file, line, vacb, vacb->ReferenceCount, vacb->Dirty, vacb->PageOut);
62 }
63 }
64 static void CcRosVacbDecRefCount_(PROS_VACB vacb, const char* file, int line)
65 {
66 --vacb->ReferenceCount;
67 if (vacb->SharedCacheMap->Trace)
68 {
69 DbgPrint("(%s:%i) VACB %p --RefCount=%lu, Dirty %u, PageOut %lu\n",
70 file, line, vacb, vacb->ReferenceCount, vacb->Dirty, vacb->PageOut);
71 }
72 }
73 #define CcRosVacbIncRefCount(vacb) CcRosVacbIncRefCount_(vacb,__FILE__,__LINE__)
74 #define CcRosVacbDecRefCount(vacb) CcRosVacbDecRefCount_(vacb,__FILE__,__LINE__)
75 #else
76 #define CcRosVacbIncRefCount(vacb) (++((vacb)->ReferenceCount))
77 #define CcRosVacbDecRefCount(vacb) (--((vacb)->ReferenceCount))
78 #endif
79
80 NTSTATUS
81 CcRosInternalFreeVacb(PROS_VACB Vacb);
82
83
84 /* FUNCTIONS *****************************************************************/
85
86 VOID
87 NTAPI
88 CcRosTraceCacheMap (
89 PROS_SHARED_CACHE_MAP SharedCacheMap,
90 BOOLEAN Trace )
91 {
92 #if DBG
93 KIRQL oldirql;
94 PLIST_ENTRY current_entry;
95 PROS_VACB current;
96
97 if (!SharedCacheMap)
98 return;
99
100 SharedCacheMap->Trace = Trace;
101
102 if (Trace)
103 {
104 DPRINT1("Enabling Tracing for CacheMap 0x%p:\n", SharedCacheMap);
105
106 KeAcquireGuardedMutex(&ViewLock);
107 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldirql);
108
109 current_entry = SharedCacheMap->CacheMapVacbListHead.Flink;
110 while (current_entry != &SharedCacheMap->CacheMapVacbListHead)
111 {
112 current = CONTAINING_RECORD(current_entry, ROS_VACB, CacheMapVacbListEntry);
113 current_entry = current_entry->Flink;
114
115 DPRINT1(" VACB 0x%p enabled, RefCount %lu, Dirty %u, PageOut %lu\n",
116 current, current->ReferenceCount, current->Dirty, current->PageOut );
117 }
118 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldirql);
119 KeReleaseGuardedMutex(&ViewLock);
120 }
121 else
122 {
123 DPRINT1("Disabling Tracing for CacheMap 0x%p:\n", SharedCacheMap);
124 }
125
126 #else
127 UNREFERENCED_PARAMETER(SharedCacheMap);
128 UNREFERENCED_PARAMETER(Trace);
129 #endif
130 }
131
132 NTSTATUS
133 NTAPI
134 CcRosFlushVacb (
135 PROS_VACB Vacb)
136 {
137 NTSTATUS Status;
138 KIRQL oldIrql;
139
140 Status = CcWriteVirtualAddress(Vacb);
141 if (NT_SUCCESS(Status))
142 {
143 KeAcquireGuardedMutex(&ViewLock);
144 KeAcquireSpinLock(&Vacb->SharedCacheMap->CacheMapLock, &oldIrql);
145
146 Vacb->Dirty = FALSE;
147 RemoveEntryList(&Vacb->DirtyVacbListEntry);
148 DirtyPageCount -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
149 CcRosVacbDecRefCount(Vacb);
150
151 KeReleaseSpinLock(&Vacb->SharedCacheMap->CacheMapLock, oldIrql);
152 KeReleaseGuardedMutex(&ViewLock);
153 }
154
155 return Status;
156 }
157
158 NTSTATUS
159 NTAPI
160 CcRosFlushDirtyPages (
161 ULONG Target,
162 PULONG Count,
163 BOOLEAN Wait)
164 {
165 PLIST_ENTRY current_entry;
166 PROS_VACB current;
167 BOOLEAN Locked;
168 NTSTATUS Status;
169 LARGE_INTEGER ZeroTimeout;
170
171 DPRINT("CcRosFlushDirtyPages(Target %lu)\n", Target);
172
173 (*Count) = 0;
174 ZeroTimeout.QuadPart = 0;
175
176 KeEnterCriticalRegion();
177 KeAcquireGuardedMutex(&ViewLock);
178
179 current_entry = DirtyVacbListHead.Flink;
180 if (current_entry == &DirtyVacbListHead)
181 {
182 DPRINT("No Dirty pages\n");
183 }
184
185 while ((current_entry != &DirtyVacbListHead) && (Target > 0))
186 {
187 current = CONTAINING_RECORD(current_entry,
188 ROS_VACB,
189 DirtyVacbListEntry);
190 current_entry = current_entry->Flink;
191
192 CcRosVacbIncRefCount(current);
193
194 Locked = current->SharedCacheMap->Callbacks->AcquireForLazyWrite(
195 current->SharedCacheMap->LazyWriteContext, Wait);
196 if (!Locked)
197 {
198 CcRosVacbDecRefCount(current);
199 continue;
200 }
201
202 Status = KeWaitForSingleObject(&current->Mutex,
203 Executive,
204 KernelMode,
205 FALSE,
206 Wait ? NULL : &ZeroTimeout);
207 if (Status != STATUS_SUCCESS)
208 {
209 current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
210 current->SharedCacheMap->LazyWriteContext);
211 CcRosVacbDecRefCount(current);
212 continue;
213 }
214
215 ASSERT(current->Dirty);
216
217 /* One reference is added above */
218 if (current->ReferenceCount > 2)
219 {
220 KeReleaseMutex(&current->Mutex, FALSE);
221 current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
222 current->SharedCacheMap->LazyWriteContext);
223 CcRosVacbDecRefCount(current);
224 continue;
225 }
226
227 KeReleaseGuardedMutex(&ViewLock);
228
229 Status = CcRosFlushVacb(current);
230
231 KeReleaseMutex(&current->Mutex, FALSE);
232 current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
233 current->SharedCacheMap->LazyWriteContext);
234
235 KeAcquireGuardedMutex(&ViewLock);
236 CcRosVacbDecRefCount(current);
237
238 if (!NT_SUCCESS(Status) && (Status != STATUS_END_OF_FILE))
239 {
240 DPRINT1("CC: Failed to flush VACB.\n");
241 }
242 else
243 {
244 (*Count) += VACB_MAPPING_GRANULARITY / PAGE_SIZE;
245 Target -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
246 }
247
248 current_entry = DirtyVacbListHead.Flink;
249 }
250
251 KeReleaseGuardedMutex(&ViewLock);
252 KeLeaveCriticalRegion();
253
254 DPRINT("CcRosFlushDirtyPages() finished\n");
255 return STATUS_SUCCESS;
256 }
257
258 NTSTATUS
259 CcRosTrimCache (
260 ULONG Target,
261 ULONG Priority,
262 PULONG NrFreed)
263 /*
264 * FUNCTION: Try to free some memory from the file cache.
265 * ARGUMENTS:
266 * Target - The number of pages to be freed.
267 * Priority - The priority of free (currently unused).
268 * NrFreed - Points to a variable where the number of pages
269 * actually freed is returned.
270 */
271 {
272 PLIST_ENTRY current_entry;
273 PROS_VACB current;
274 ULONG PagesFreed;
275 KIRQL oldIrql;
276 LIST_ENTRY FreeList;
277 PFN_NUMBER Page;
278 ULONG i;
279 BOOLEAN FlushedPages = FALSE;
280
281 DPRINT("CcRosTrimCache(Target %lu)\n", Target);
282
283 InitializeListHead(&FreeList);
284
285 *NrFreed = 0;
286
287 retry:
288 KeAcquireGuardedMutex(&ViewLock);
289
290 current_entry = VacbLruListHead.Flink;
291 while (current_entry != &VacbLruListHead)
292 {
293 current = CONTAINING_RECORD(current_entry,
294 ROS_VACB,
295 VacbLruListEntry);
296 current_entry = current_entry->Flink;
297
298 KeAcquireSpinLock(&current->SharedCacheMap->CacheMapLock, &oldIrql);
299
300 /* Reference the VACB */
301 CcRosVacbIncRefCount(current);
302
303 /* Check if it's mapped and not dirty */
304 if (current->MappedCount > 0 && !current->Dirty)
305 {
306 /* We have to break these locks because Cc sucks */
307 KeReleaseSpinLock(&current->SharedCacheMap->CacheMapLock, oldIrql);
308 KeReleaseGuardedMutex(&ViewLock);
309
310 /* Page out the VACB */
311 for (i = 0; i < VACB_MAPPING_GRANULARITY / PAGE_SIZE; i++)
312 {
313 Page = (PFN_NUMBER)(MmGetPhysicalAddress((PUCHAR)current->BaseAddress + (i * PAGE_SIZE)).QuadPart >> PAGE_SHIFT);
314
315 MmPageOutPhysicalAddress(Page);
316 }
317
318 /* Reacquire the locks */
319 KeAcquireGuardedMutex(&ViewLock);
320 KeAcquireSpinLock(&current->SharedCacheMap->CacheMapLock, &oldIrql);
321 }
322
323 /* Dereference the VACB */
324 CcRosVacbDecRefCount(current);
325
326 /* Check if we can free this entry now */
327 if (current->ReferenceCount == 0)
328 {
329 ASSERT(!current->Dirty);
330 ASSERT(!current->MappedCount);
331
332 RemoveEntryList(&current->CacheMapVacbListEntry);
333 RemoveEntryList(&current->VacbLruListEntry);
334 InsertHeadList(&FreeList, &current->CacheMapVacbListEntry);
335
336 /* Calculate how many pages we freed for Mm */
337 PagesFreed = min(VACB_MAPPING_GRANULARITY / PAGE_SIZE, Target);
338 Target -= PagesFreed;
339 (*NrFreed) += PagesFreed;
340 }
341
342 KeReleaseSpinLock(&current->SharedCacheMap->CacheMapLock, oldIrql);
343 }
344
345 KeReleaseGuardedMutex(&ViewLock);
346
347 /* Try flushing pages if we haven't met our target */
348 if ((Target > 0) && !FlushedPages)
349 {
350 /* Flush dirty pages to disk */
351 CcRosFlushDirtyPages(Target, &PagesFreed, FALSE);
352 FlushedPages = TRUE;
353
354 /* We can only swap as many pages as we flushed */
355 if (PagesFreed < Target) Target = PagesFreed;
356
357 /* Check if we flushed anything */
358 if (PagesFreed != 0)
359 {
360 /* Try again after flushing dirty pages */
361 DPRINT("Flushed %lu dirty cache pages to disk\n", PagesFreed);
362 goto retry;
363 }
364 }
365
366 while (!IsListEmpty(&FreeList))
367 {
368 current_entry = RemoveHeadList(&FreeList);
369 current = CONTAINING_RECORD(current_entry,
370 ROS_VACB,
371 CacheMapVacbListEntry);
372 CcRosInternalFreeVacb(current);
373 }
374
375 DPRINT("Evicted %lu cache pages\n", (*NrFreed));
376
377 return STATUS_SUCCESS;
378 }
379
380 NTSTATUS
381 NTAPI
382 CcRosReleaseVacb (
383 PROS_SHARED_CACHE_MAP SharedCacheMap,
384 PROS_VACB Vacb,
385 BOOLEAN Valid,
386 BOOLEAN Dirty,
387 BOOLEAN Mapped)
388 {
389 BOOLEAN WasDirty;
390 KIRQL oldIrql;
391
392 ASSERT(SharedCacheMap);
393
394 DPRINT("CcRosReleaseVacb(SharedCacheMap 0x%p, Vacb 0x%p, Valid %u)\n",
395 SharedCacheMap, Vacb, Valid);
396
397 KeAcquireGuardedMutex(&ViewLock);
398 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
399
400 Vacb->Valid = Valid;
401
402 WasDirty = Vacb->Dirty;
403 Vacb->Dirty = Vacb->Dirty || Dirty;
404
405 if (!WasDirty && Vacb->Dirty)
406 {
407 InsertTailList(&DirtyVacbListHead, &Vacb->DirtyVacbListEntry);
408 DirtyPageCount += VACB_MAPPING_GRANULARITY / PAGE_SIZE;
409 }
410
411 if (Mapped)
412 {
413 Vacb->MappedCount++;
414 }
415 CcRosVacbDecRefCount(Vacb);
416 if (Mapped && (Vacb->MappedCount == 1))
417 {
418 CcRosVacbIncRefCount(Vacb);
419 }
420 if (!WasDirty && Vacb->Dirty)
421 {
422 CcRosVacbIncRefCount(Vacb);
423 }
424
425 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
426 KeReleaseGuardedMutex(&ViewLock);
427 KeReleaseMutex(&Vacb->Mutex, FALSE);
428
429 return STATUS_SUCCESS;
430 }
431
432 /* Returns with VACB Lock Held! */
433 PROS_VACB
434 NTAPI
435 CcRosLookupVacb (
436 PROS_SHARED_CACHE_MAP SharedCacheMap,
437 LONGLONG FileOffset)
438 {
439 PLIST_ENTRY current_entry;
440 PROS_VACB current;
441 KIRQL oldIrql;
442
443 ASSERT(SharedCacheMap);
444
445 DPRINT("CcRosLookupVacb(SharedCacheMap 0x%p, FileOffset %I64u)\n",
446 SharedCacheMap, FileOffset);
447
448 KeAcquireGuardedMutex(&ViewLock);
449 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
450
451 current_entry = SharedCacheMap->CacheMapVacbListHead.Flink;
452 while (current_entry != &SharedCacheMap->CacheMapVacbListHead)
453 {
454 current = CONTAINING_RECORD(current_entry,
455 ROS_VACB,
456 CacheMapVacbListEntry);
457 if (IsPointInRange(current->FileOffset.QuadPart,
458 VACB_MAPPING_GRANULARITY,
459 FileOffset))
460 {
461 CcRosVacbIncRefCount(current);
462 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
463 KeReleaseGuardedMutex(&ViewLock);
464 KeWaitForSingleObject(&current->Mutex,
465 Executive,
466 KernelMode,
467 FALSE,
468 NULL);
469 return current;
470 }
471 if (current->FileOffset.QuadPart > FileOffset)
472 break;
473 current_entry = current_entry->Flink;
474 }
475
476 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
477 KeReleaseGuardedMutex(&ViewLock);
478
479 return NULL;
480 }
481
482 NTSTATUS
483 NTAPI
484 CcRosMarkDirtyVacb (
485 PROS_SHARED_CACHE_MAP SharedCacheMap,
486 LONGLONG FileOffset)
487 {
488 PROS_VACB Vacb;
489 KIRQL oldIrql;
490
491 ASSERT(SharedCacheMap);
492
493 DPRINT("CcRosMarkDirtyVacb(SharedCacheMap 0x%p, FileOffset %I64u)\n",
494 SharedCacheMap, FileOffset);
495
496 Vacb = CcRosLookupVacb(SharedCacheMap, FileOffset);
497 if (Vacb == NULL)
498 {
499 KeBugCheck(CACHE_MANAGER);
500 }
501
502 KeAcquireGuardedMutex(&ViewLock);
503 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
504
505 if (!Vacb->Dirty)
506 {
507 InsertTailList(&DirtyVacbListHead, &Vacb->DirtyVacbListEntry);
508 DirtyPageCount += VACB_MAPPING_GRANULARITY / PAGE_SIZE;
509 }
510 else
511 {
512 CcRosVacbDecRefCount(Vacb);
513 }
514
515 /* Move to the tail of the LRU list */
516 RemoveEntryList(&Vacb->VacbLruListEntry);
517 InsertTailList(&VacbLruListHead, &Vacb->VacbLruListEntry);
518
519 Vacb->Dirty = TRUE;
520
521 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
522 KeReleaseGuardedMutex(&ViewLock);
523 KeReleaseMutex(&Vacb->Mutex, FALSE);
524
525 return STATUS_SUCCESS;
526 }
527
528 NTSTATUS
529 NTAPI
530 CcRosUnmapVacb (
531 PROS_SHARED_CACHE_MAP SharedCacheMap,
532 LONGLONG FileOffset,
533 BOOLEAN NowDirty)
534 {
535 PROS_VACB Vacb;
536 BOOLEAN WasDirty;
537 KIRQL oldIrql;
538
539 ASSERT(SharedCacheMap);
540
541 DPRINT("CcRosUnmapVacb(SharedCacheMap 0x%p, FileOffset %I64u, NowDirty %u)\n",
542 SharedCacheMap, FileOffset, NowDirty);
543
544 Vacb = CcRosLookupVacb(SharedCacheMap, FileOffset);
545 if (Vacb == NULL)
546 {
547 return STATUS_UNSUCCESSFUL;
548 }
549
550 KeAcquireGuardedMutex(&ViewLock);
551 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
552
553 WasDirty = Vacb->Dirty;
554 Vacb->Dirty = Vacb->Dirty || NowDirty;
555
556 Vacb->MappedCount--;
557
558 if (!WasDirty && NowDirty)
559 {
560 InsertTailList(&DirtyVacbListHead, &Vacb->DirtyVacbListEntry);
561 DirtyPageCount += VACB_MAPPING_GRANULARITY / PAGE_SIZE;
562 }
563
564 CcRosVacbDecRefCount(Vacb);
565 if (!WasDirty && NowDirty)
566 {
567 CcRosVacbIncRefCount(Vacb);
568 }
569 if (Vacb->MappedCount == 0)
570 {
571 CcRosVacbDecRefCount(Vacb);
572 }
573
574 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
575 KeReleaseGuardedMutex(&ViewLock);
576 KeReleaseMutex(&Vacb->Mutex, FALSE);
577
578 return STATUS_SUCCESS;
579 }
580
581 static
582 NTSTATUS
583 CcRosCreateVacb (
584 PROS_SHARED_CACHE_MAP SharedCacheMap,
585 LONGLONG FileOffset,
586 PROS_VACB *Vacb)
587 {
588 PROS_VACB current;
589 PROS_VACB previous;
590 PLIST_ENTRY current_entry;
591 NTSTATUS Status;
592 KIRQL oldIrql;
593
594 ASSERT(SharedCacheMap);
595
596 DPRINT("CcRosCreateVacb()\n");
597
598 if (FileOffset >= SharedCacheMap->FileSize.QuadPart)
599 {
600 *Vacb = NULL;
601 return STATUS_INVALID_PARAMETER;
602 }
603
604 current = ExAllocateFromNPagedLookasideList(&VacbLookasideList);
605 current->Valid = FALSE;
606 current->Dirty = FALSE;
607 current->PageOut = FALSE;
608 current->FileOffset.QuadPart = ROUND_DOWN(FileOffset, VACB_MAPPING_GRANULARITY);
609 current->SharedCacheMap = SharedCacheMap;
610 #if DBG
611 if (SharedCacheMap->Trace)
612 {
613 DPRINT1("CacheMap 0x%p: new VACB: 0x%p\n", SharedCacheMap, current);
614 }
615 #endif
616 current->MappedCount = 0;
617 current->DirtyVacbListEntry.Flink = NULL;
618 current->DirtyVacbListEntry.Blink = NULL;
619 current->ReferenceCount = 1;
620 KeInitializeMutex(&current->Mutex, 0);
621 KeWaitForSingleObject(&current->Mutex,
622 Executive,
623 KernelMode,
624 FALSE,
625 NULL);
626 KeAcquireGuardedMutex(&ViewLock);
627
628 *Vacb = current;
629 /* There is window between the call to CcRosLookupVacb
630 * and CcRosCreateVacb. We must check if a VACB for the
631 * file offset exist. If there is a VACB, we release
632 * our newly created VACB and return the existing one.
633 */
634 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
635 current_entry = SharedCacheMap->CacheMapVacbListHead.Flink;
636 previous = NULL;
637 while (current_entry != &SharedCacheMap->CacheMapVacbListHead)
638 {
639 current = CONTAINING_RECORD(current_entry,
640 ROS_VACB,
641 CacheMapVacbListEntry);
642 if (IsPointInRange(current->FileOffset.QuadPart,
643 VACB_MAPPING_GRANULARITY,
644 FileOffset))
645 {
646 CcRosVacbIncRefCount(current);
647 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
648 #if DBG
649 if (SharedCacheMap->Trace)
650 {
651 DPRINT1("CacheMap 0x%p: deleting newly created VACB 0x%p ( found existing one 0x%p )\n",
652 SharedCacheMap,
653 (*Vacb),
654 current);
655 }
656 #endif
657 KeReleaseMutex(&(*Vacb)->Mutex, FALSE);
658 KeReleaseGuardedMutex(&ViewLock);
659 ExFreeToNPagedLookasideList(&VacbLookasideList, *Vacb);
660 *Vacb = current;
661 KeWaitForSingleObject(&current->Mutex,
662 Executive,
663 KernelMode,
664 FALSE,
665 NULL);
666 return STATUS_SUCCESS;
667 }
668 if (current->FileOffset.QuadPart < FileOffset)
669 {
670 ASSERT(previous == NULL ||
671 previous->FileOffset.QuadPart < current->FileOffset.QuadPart);
672 previous = current;
673 }
674 if (current->FileOffset.QuadPart > FileOffset)
675 break;
676 current_entry = current_entry->Flink;
677 }
678 /* There was no existing VACB. */
679 current = *Vacb;
680 if (previous)
681 {
682 InsertHeadList(&previous->CacheMapVacbListEntry, &current->CacheMapVacbListEntry);
683 }
684 else
685 {
686 InsertHeadList(&SharedCacheMap->CacheMapVacbListHead, &current->CacheMapVacbListEntry);
687 }
688 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
689 InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
690 KeReleaseGuardedMutex(&ViewLock);
691
692 MmLockAddressSpace(MmGetKernelAddressSpace());
693 current->BaseAddress = NULL;
694 Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
695 0, // nothing checks for VACB mareas, so set to 0
696 &current->BaseAddress,
697 VACB_MAPPING_GRANULARITY,
698 PAGE_READWRITE,
699 (PMEMORY_AREA*)&current->MemoryArea,
700 FALSE,
701 0,
702 PAGE_SIZE);
703 MmUnlockAddressSpace(MmGetKernelAddressSpace());
704 if (!NT_SUCCESS(Status))
705 {
706 KeBugCheck(CACHE_MANAGER);
707 }
708
709 /* Create a virtual mapping for this memory area */
710 MI_SET_USAGE(MI_USAGE_CACHE);
711 #if MI_TRACE_PFNS
712 PWCHAR pos = NULL;
713 ULONG len = 0;
714 if ((SharedCacheMap->FileObject) && (SharedCacheMap->FileObject->FileName.Buffer))
715 {
716 pos = wcsrchr(SharedCacheMap->FileObject->FileName.Buffer, '\\');
717 len = wcslen(pos) * sizeof(WCHAR);
718 if (pos) snprintf(MI_PFN_CURRENT_PROCESS_NAME, min(16, len), "%S", pos);
719 }
720 #endif
721
722 MmMapMemoryArea(current->BaseAddress, VACB_MAPPING_GRANULARITY,
723 MC_CACHE, PAGE_READWRITE);
724
725 return STATUS_SUCCESS;
726 }
727
728 NTSTATUS
729 NTAPI
730 CcRosGetVacb (
731 PROS_SHARED_CACHE_MAP SharedCacheMap,
732 LONGLONG FileOffset,
733 PLONGLONG BaseOffset,
734 PVOID* BaseAddress,
735 PBOOLEAN UptoDate,
736 PROS_VACB *Vacb)
737 {
738 PROS_VACB current;
739 NTSTATUS Status;
740
741 ASSERT(SharedCacheMap);
742
743 DPRINT("CcRosGetVacb()\n");
744
745 /*
746 * Look for a VACB already mapping the same data.
747 */
748 current = CcRosLookupVacb(SharedCacheMap, FileOffset);
749 if (current == NULL)
750 {
751 /*
752 * Otherwise create a new VACB.
753 */
754 Status = CcRosCreateVacb(SharedCacheMap, FileOffset, &current);
755 if (!NT_SUCCESS(Status))
756 {
757 return Status;
758 }
759 }
760
761 KeAcquireGuardedMutex(&ViewLock);
762
763 /* Move to the tail of the LRU list */
764 RemoveEntryList(&current->VacbLruListEntry);
765 InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
766
767 KeReleaseGuardedMutex(&ViewLock);
768
769 /*
770 * Return information about the VACB to the caller.
771 */
772 *UptoDate = current->Valid;
773 *BaseAddress = current->BaseAddress;
774 DPRINT("*BaseAddress %p\n", *BaseAddress);
775 *Vacb = current;
776 *BaseOffset = current->FileOffset.QuadPart;
777 return STATUS_SUCCESS;
778 }
779
780 NTSTATUS
781 NTAPI
782 CcRosRequestVacb (
783 PROS_SHARED_CACHE_MAP SharedCacheMap,
784 LONGLONG FileOffset,
785 PVOID* BaseAddress,
786 PBOOLEAN UptoDate,
787 PROS_VACB *Vacb)
788 /*
789 * FUNCTION: Request a page mapping for a shared cache map
790 */
791 {
792 LONGLONG BaseOffset;
793
794 ASSERT(SharedCacheMap);
795
796 if (FileOffset % VACB_MAPPING_GRANULARITY != 0)
797 {
798 DPRINT1("Bad fileoffset %I64x should be multiple of %x",
799 FileOffset, VACB_MAPPING_GRANULARITY);
800 KeBugCheck(CACHE_MANAGER);
801 }
802
803 return CcRosGetVacb(SharedCacheMap,
804 FileOffset,
805 &BaseOffset,
806 BaseAddress,
807 UptoDate,
808 Vacb);
809 }
810
811 static
812 VOID
813 CcFreeCachePage (
814 PVOID Context,
815 MEMORY_AREA* MemoryArea,
816 PVOID Address,
817 PFN_NUMBER Page,
818 SWAPENTRY SwapEntry,
819 BOOLEAN Dirty)
820 {
821 ASSERT(SwapEntry == 0);
822 if (Page != 0)
823 {
824 ASSERT(MmGetReferenceCountPage(Page) == 1);
825 MmReleasePageMemoryConsumer(MC_CACHE, Page);
826 }
827 }
828
829 NTSTATUS
830 CcRosInternalFreeVacb (
831 PROS_VACB Vacb)
832 /*
833 * FUNCTION: Releases a VACB associated with a shared cache map
834 */
835 {
836 DPRINT("Freeing VACB 0x%p\n", Vacb);
837 #if DBG
838 if (Vacb->SharedCacheMap->Trace)
839 {
840 DPRINT1("CacheMap 0x%p: deleting VACB: 0x%p\n", Vacb->SharedCacheMap, Vacb);
841 }
842 #endif
843
844 MmLockAddressSpace(MmGetKernelAddressSpace());
845 MmFreeMemoryArea(MmGetKernelAddressSpace(),
846 Vacb->MemoryArea,
847 CcFreeCachePage,
848 NULL);
849 MmUnlockAddressSpace(MmGetKernelAddressSpace());
850
851 ExFreeToNPagedLookasideList(&VacbLookasideList, Vacb);
852 return STATUS_SUCCESS;
853 }
854
855 /*
856 * @implemented
857 */
858 VOID
859 NTAPI
860 CcFlushCache (
861 IN PSECTION_OBJECT_POINTERS SectionObjectPointers,
862 IN PLARGE_INTEGER FileOffset OPTIONAL,
863 IN ULONG Length,
864 OUT PIO_STATUS_BLOCK IoStatus)
865 {
866 PROS_SHARED_CACHE_MAP SharedCacheMap;
867 LARGE_INTEGER Offset;
868 LONGLONG RemainingLength;
869 PROS_VACB current;
870 NTSTATUS Status;
871 KIRQL oldIrql;
872
873 DPRINT("CcFlushCache(SectionObjectPointers 0x%p, FileOffset 0x%p, Length %lu, IoStatus 0x%p)\n",
874 SectionObjectPointers, FileOffset, Length, IoStatus);
875
876 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
877 {
878 SharedCacheMap = SectionObjectPointers->SharedCacheMap;
879 ASSERT(SharedCacheMap);
880 if (FileOffset)
881 {
882 Offset = *FileOffset;
883 RemainingLength = Length;
884 }
885 else
886 {
887 Offset.QuadPart = 0;
888 RemainingLength = SharedCacheMap->FileSize.QuadPart;
889 }
890
891 if (IoStatus)
892 {
893 IoStatus->Status = STATUS_SUCCESS;
894 IoStatus->Information = 0;
895 }
896
897 while (RemainingLength > 0)
898 {
899 current = CcRosLookupVacb(SharedCacheMap, Offset.QuadPart);
900 if (current != NULL)
901 {
902 if (current->Dirty)
903 {
904 Status = CcRosFlushVacb(current);
905 if (!NT_SUCCESS(Status) && IoStatus != NULL)
906 {
907 IoStatus->Status = Status;
908 }
909 }
910 KeReleaseMutex(&current->Mutex, FALSE);
911
912 KeAcquireGuardedMutex(&ViewLock);
913 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
914 CcRosVacbDecRefCount(current);
915 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
916 KeReleaseGuardedMutex(&ViewLock);
917 }
918
919 Offset.QuadPart += VACB_MAPPING_GRANULARITY;
920 RemainingLength -= min(RemainingLength, VACB_MAPPING_GRANULARITY);
921 }
922 }
923 else
924 {
925 if (IoStatus)
926 {
927 IoStatus->Status = STATUS_INVALID_PARAMETER;
928 }
929 }
930 }
931
932 NTSTATUS
933 NTAPI
934 CcRosDeleteFileCache (
935 PFILE_OBJECT FileObject,
936 PROS_SHARED_CACHE_MAP SharedCacheMap)
937 /*
938 * FUNCTION: Releases the shared cache map associated with a file object
939 */
940 {
941 PLIST_ENTRY current_entry;
942 PROS_VACB current;
943 LIST_ENTRY FreeList;
944 KIRQL oldIrql;
945
946 ASSERT(SharedCacheMap);
947
948 SharedCacheMap->RefCount++;
949 KeReleaseGuardedMutex(&ViewLock);
950
951 CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, NULL);
952
953 KeAcquireGuardedMutex(&ViewLock);
954 SharedCacheMap->RefCount--;
955 if (SharedCacheMap->RefCount == 0)
956 {
957 FileObject->SectionObjectPointer->SharedCacheMap = NULL;
958
959 /*
960 * Release all VACBs
961 */
962 InitializeListHead(&FreeList);
963 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
964 while (!IsListEmpty(&SharedCacheMap->CacheMapVacbListHead))
965 {
966 current_entry = RemoveTailList(&SharedCacheMap->CacheMapVacbListHead);
967 current = CONTAINING_RECORD(current_entry, ROS_VACB, CacheMapVacbListEntry);
968 RemoveEntryList(&current->VacbLruListEntry);
969 if (current->Dirty)
970 {
971 RemoveEntryList(&current->DirtyVacbListEntry);
972 DirtyPageCount -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
973 DPRINT1("Freeing dirty VACB\n");
974 }
975 InsertHeadList(&FreeList, &current->CacheMapVacbListEntry);
976 }
977 #if DBG
978 SharedCacheMap->Trace = FALSE;
979 #endif
980 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
981
982 KeReleaseGuardedMutex(&ViewLock);
983 ObDereferenceObject(SharedCacheMap->FileObject);
984
985 while (!IsListEmpty(&FreeList))
986 {
987 current_entry = RemoveTailList(&FreeList);
988 current = CONTAINING_RECORD(current_entry, ROS_VACB, CacheMapVacbListEntry);
989 CcRosInternalFreeVacb(current);
990 }
991 ExFreeToNPagedLookasideList(&SharedCacheMapLookasideList, SharedCacheMap);
992 KeAcquireGuardedMutex(&ViewLock);
993 }
994 return STATUS_SUCCESS;
995 }
996
997 VOID
998 NTAPI
999 CcRosReferenceCache (
1000 PFILE_OBJECT FileObject)
1001 {
1002 PROS_SHARED_CACHE_MAP SharedCacheMap;
1003 KeAcquireGuardedMutex(&ViewLock);
1004 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1005 ASSERT(SharedCacheMap);
1006 ASSERT(SharedCacheMap->RefCount != 0);
1007 SharedCacheMap->RefCount++;
1008 KeReleaseGuardedMutex(&ViewLock);
1009 }
1010
1011 VOID
1012 NTAPI
1013 CcRosRemoveIfClosed (
1014 PSECTION_OBJECT_POINTERS SectionObjectPointer)
1015 {
1016 PROS_SHARED_CACHE_MAP SharedCacheMap;
1017 DPRINT("CcRosRemoveIfClosed()\n");
1018 KeAcquireGuardedMutex(&ViewLock);
1019 SharedCacheMap = SectionObjectPointer->SharedCacheMap;
1020 if (SharedCacheMap && SharedCacheMap->RefCount == 0)
1021 {
1022 CcRosDeleteFileCache(SharedCacheMap->FileObject, SharedCacheMap);
1023 }
1024 KeReleaseGuardedMutex(&ViewLock);
1025 }
1026
1027
1028 VOID
1029 NTAPI
1030 CcRosDereferenceCache (
1031 PFILE_OBJECT FileObject)
1032 {
1033 PROS_SHARED_CACHE_MAP SharedCacheMap;
1034 KeAcquireGuardedMutex(&ViewLock);
1035 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1036 ASSERT(SharedCacheMap);
1037 if (SharedCacheMap->RefCount > 0)
1038 {
1039 SharedCacheMap->RefCount--;
1040 if (SharedCacheMap->RefCount == 0)
1041 {
1042 MmFreeSectionSegments(SharedCacheMap->FileObject);
1043 CcRosDeleteFileCache(FileObject, SharedCacheMap);
1044 }
1045 }
1046 KeReleaseGuardedMutex(&ViewLock);
1047 }
1048
1049 NTSTATUS
1050 NTAPI
1051 CcRosReleaseFileCache (
1052 PFILE_OBJECT FileObject)
1053 /*
1054 * FUNCTION: Called by the file system when a handle to a file object
1055 * has been closed.
1056 */
1057 {
1058 PROS_SHARED_CACHE_MAP SharedCacheMap;
1059
1060 KeAcquireGuardedMutex(&ViewLock);
1061
1062 if (FileObject->SectionObjectPointer->SharedCacheMap != NULL)
1063 {
1064 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1065 if (FileObject->PrivateCacheMap != NULL)
1066 {
1067 FileObject->PrivateCacheMap = NULL;
1068 if (SharedCacheMap->RefCount > 0)
1069 {
1070 SharedCacheMap->RefCount--;
1071 if (SharedCacheMap->RefCount == 0)
1072 {
1073 MmFreeSectionSegments(SharedCacheMap->FileObject);
1074 CcRosDeleteFileCache(FileObject, SharedCacheMap);
1075 }
1076 }
1077 }
1078 }
1079 KeReleaseGuardedMutex(&ViewLock);
1080 return STATUS_SUCCESS;
1081 }
1082
1083 NTSTATUS
1084 NTAPI
1085 CcTryToInitializeFileCache (
1086 PFILE_OBJECT FileObject)
1087 {
1088 PROS_SHARED_CACHE_MAP SharedCacheMap;
1089 NTSTATUS Status;
1090
1091 KeAcquireGuardedMutex(&ViewLock);
1092
1093 ASSERT(FileObject->SectionObjectPointer);
1094 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1095 if (SharedCacheMap == NULL)
1096 {
1097 Status = STATUS_UNSUCCESSFUL;
1098 }
1099 else
1100 {
1101 if (FileObject->PrivateCacheMap == NULL)
1102 {
1103 FileObject->PrivateCacheMap = SharedCacheMap;
1104 SharedCacheMap->RefCount++;
1105 }
1106 Status = STATUS_SUCCESS;
1107 }
1108 KeReleaseGuardedMutex(&ViewLock);
1109
1110 return Status;
1111 }
1112
1113
1114 NTSTATUS
1115 NTAPI
1116 CcRosInitializeFileCache (
1117 PFILE_OBJECT FileObject,
1118 PCC_FILE_SIZES FileSizes,
1119 PCACHE_MANAGER_CALLBACKS CallBacks,
1120 PVOID LazyWriterContext)
1121 /*
1122 * FUNCTION: Initializes a shared cache map for a file object
1123 */
1124 {
1125 PROS_SHARED_CACHE_MAP SharedCacheMap;
1126
1127 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1128 DPRINT("CcRosInitializeFileCache(FileObject 0x%p, SharedCacheMap 0x%p)\n",
1129 FileObject, SharedCacheMap);
1130
1131 KeAcquireGuardedMutex(&ViewLock);
1132 if (SharedCacheMap == NULL)
1133 {
1134 SharedCacheMap = ExAllocateFromNPagedLookasideList(&SharedCacheMapLookasideList);
1135 if (SharedCacheMap == NULL)
1136 {
1137 KeReleaseGuardedMutex(&ViewLock);
1138 return STATUS_UNSUCCESSFUL;
1139 }
1140 RtlZeroMemory(SharedCacheMap, sizeof(*SharedCacheMap));
1141 ObReferenceObjectByPointer(FileObject,
1142 FILE_ALL_ACCESS,
1143 NULL,
1144 KernelMode);
1145 SharedCacheMap->FileObject = FileObject;
1146 SharedCacheMap->Callbacks = CallBacks;
1147 SharedCacheMap->LazyWriteContext = LazyWriterContext;
1148 SharedCacheMap->SectionSize = FileSizes->AllocationSize;
1149 SharedCacheMap->FileSize = FileSizes->FileSize;
1150 KeInitializeSpinLock(&SharedCacheMap->CacheMapLock);
1151 InitializeListHead(&SharedCacheMap->CacheMapVacbListHead);
1152 FileObject->SectionObjectPointer->SharedCacheMap = SharedCacheMap;
1153 }
1154 if (FileObject->PrivateCacheMap == NULL)
1155 {
1156 FileObject->PrivateCacheMap = SharedCacheMap;
1157 SharedCacheMap->RefCount++;
1158 }
1159 KeReleaseGuardedMutex(&ViewLock);
1160
1161 return STATUS_SUCCESS;
1162 }
1163
1164 /*
1165 * @implemented
1166 */
1167 PFILE_OBJECT
1168 NTAPI
1169 CcGetFileObjectFromSectionPtrs (
1170 IN PSECTION_OBJECT_POINTERS SectionObjectPointers)
1171 {
1172 PROS_SHARED_CACHE_MAP SharedCacheMap;
1173 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
1174 {
1175 SharedCacheMap = SectionObjectPointers->SharedCacheMap;
1176 ASSERT(SharedCacheMap);
1177 return SharedCacheMap->FileObject;
1178 }
1179 return NULL;
1180 }
1181
1182 VOID
1183 INIT_FUNCTION
1184 NTAPI
1185 CcInitView (
1186 VOID)
1187 {
1188 DPRINT("CcInitView()\n");
1189
1190 InitializeListHead(&DirtyVacbListHead);
1191 InitializeListHead(&VacbLruListHead);
1192 KeInitializeGuardedMutex(&ViewLock);
1193 ExInitializeNPagedLookasideList(&iBcbLookasideList,
1194 NULL,
1195 NULL,
1196 0,
1197 sizeof(INTERNAL_BCB),
1198 TAG_BCB,
1199 20);
1200 ExInitializeNPagedLookasideList(&SharedCacheMapLookasideList,
1201 NULL,
1202 NULL,
1203 0,
1204 sizeof(ROS_SHARED_CACHE_MAP),
1205 TAG_SHARED_CACHE_MAP,
1206 20);
1207 ExInitializeNPagedLookasideList(&VacbLookasideList,
1208 NULL,
1209 NULL,
1210 0,
1211 sizeof(ROS_VACB),
1212 TAG_VACB,
1213 20);
1214
1215 MmInitializeMemoryConsumer(MC_CACHE, CcRosTrimCache);
1216
1217 CcInitCacheZeroPage();
1218 }
1219
1220 /* EOF */