[NTOS:CC]
[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 VacbListHead;
46 static LIST_ENTRY VacbLruListHead;
47 static LIST_ENTRY ClosedListHead;
48 ULONG DirtyPageCount = 0;
49
50 KGUARDED_MUTEX ViewLock;
51
52 NPAGED_LOOKASIDE_LIST iBcbLookasideList;
53 static NPAGED_LOOKASIDE_LIST BcbLookasideList;
54 static NPAGED_LOOKASIDE_LIST VacbLookasideList;
55
56 #if DBG
57 static void CcRosVacbIncRefCount_(PROS_VACB vacb, const char* file, int line)
58 {
59 ++vacb->ReferenceCount;
60 if (vacb->Bcb->Trace)
61 {
62 DbgPrint("(%s:%i) VACB %p ++RefCount=%lu, Dirty %u, PageOut %lu\n",
63 file, line, vacb, vacb->ReferenceCount, vacb->Dirty, vacb->PageOut);
64 }
65 }
66 static void CcRosVacbDecRefCount_(PROS_VACB vacb, const char* file, int line)
67 {
68 --vacb->ReferenceCount;
69 if (vacb->Bcb->Trace)
70 {
71 DbgPrint("(%s:%i) VACB %p --RefCount=%lu, Dirty %u, PageOut %lu\n",
72 file, line, vacb, vacb->ReferenceCount, vacb->Dirty, vacb->PageOut);
73 }
74 }
75 #define CcRosVacbIncRefCount(vacb) CcRosVacbIncRefCount_(vacb,__FILE__,__LINE__)
76 #define CcRosVacbDecRefCount(vacb) CcRosVacbDecRefCount_(vacb,__FILE__,__LINE__)
77 #else
78 #define CcRosVacbIncRefCount(vacb) (++((vacb)->ReferenceCount))
79 #define CcRosVacbDecRefCount(vacb) (--((vacb)->ReferenceCount))
80 #endif
81
82 NTSTATUS
83 CcRosInternalFreeVacb(PROS_VACB Vacb);
84
85
86 /* FUNCTIONS *****************************************************************/
87
88 VOID
89 NTAPI
90 CcRosTraceCacheMap (
91 PBCB Bcb,
92 BOOLEAN Trace )
93 {
94 #if DBG
95 KIRQL oldirql;
96 PLIST_ENTRY current_entry;
97 PROS_VACB current;
98
99 if ( !Bcb )
100 return;
101
102 Bcb->Trace = Trace;
103
104 if ( Trace )
105 {
106 DPRINT1("Enabling Tracing for CacheMap 0x%p:\n", Bcb );
107
108 KeAcquireGuardedMutex(&ViewLock);
109 KeAcquireSpinLock(&Bcb->BcbLock, &oldirql);
110
111 current_entry = Bcb->BcbVacbListHead.Flink;
112 while (current_entry != &Bcb->BcbVacbListHead)
113 {
114 current = CONTAINING_RECORD(current_entry, ROS_VACB, BcbVacbListEntry);
115 current_entry = current_entry->Flink;
116
117 DPRINT1(" VACB 0x%p enabled, RefCount %lu, Dirty %u, PageOut %lu\n",
118 current, current->ReferenceCount, current->Dirty, current->PageOut );
119 }
120 KeReleaseSpinLock(&Bcb->BcbLock, oldirql);
121 KeReleaseGuardedMutex(&ViewLock);
122 }
123 else
124 {
125 DPRINT1("Disabling Tracing for CacheMap 0x%p:\n", Bcb );
126 }
127
128 #else
129 Bcb = Bcb;
130 Trace = Trace;
131 #endif
132 }
133
134 NTSTATUS
135 NTAPI
136 CcRosFlushVacb (
137 PROS_VACB Vacb)
138 {
139 NTSTATUS Status;
140 KIRQL oldIrql;
141
142 Status = CcWriteVirtualAddress(Vacb);
143 if (NT_SUCCESS(Status))
144 {
145 KeAcquireGuardedMutex(&ViewLock);
146 KeAcquireSpinLock(&Vacb->Bcb->BcbLock, &oldIrql);
147
148 Vacb->Dirty = FALSE;
149 RemoveEntryList(&Vacb->DirtyVacbListEntry);
150 DirtyPageCount -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
151 CcRosVacbDecRefCount(Vacb);
152
153 KeReleaseSpinLock(&Vacb->Bcb->BcbLock, oldIrql);
154 KeReleaseGuardedMutex(&ViewLock);
155 }
156
157 return Status;
158 }
159
160 NTSTATUS
161 NTAPI
162 CcRosFlushDirtyPages (
163 ULONG Target,
164 PULONG Count,
165 BOOLEAN Wait)
166 {
167 PLIST_ENTRY current_entry;
168 PROS_VACB current;
169 BOOLEAN Locked;
170 NTSTATUS Status;
171 LARGE_INTEGER ZeroTimeout;
172
173 DPRINT("CcRosFlushDirtyPages(Target %lu)\n", Target);
174
175 (*Count) = 0;
176 ZeroTimeout.QuadPart = 0;
177
178 KeEnterCriticalRegion();
179 KeAcquireGuardedMutex(&ViewLock);
180
181 current_entry = DirtyVacbListHead.Flink;
182 if (current_entry == &DirtyVacbListHead)
183 {
184 DPRINT("No Dirty pages\n");
185 }
186
187 while ((current_entry != &DirtyVacbListHead) && (Target > 0))
188 {
189 current = CONTAINING_RECORD(current_entry,
190 ROS_VACB,
191 DirtyVacbListEntry);
192 current_entry = current_entry->Flink;
193
194 CcRosVacbIncRefCount(current);
195
196 Locked = current->Bcb->Callbacks->AcquireForLazyWrite(
197 current->Bcb->LazyWriteContext, Wait);
198 if (!Locked)
199 {
200 CcRosVacbDecRefCount(current);
201 continue;
202 }
203
204 Status = KeWaitForSingleObject(&current->Mutex,
205 Executive,
206 KernelMode,
207 FALSE,
208 Wait ? NULL : &ZeroTimeout);
209 if (Status != STATUS_SUCCESS)
210 {
211 current->Bcb->Callbacks->ReleaseFromLazyWrite(
212 current->Bcb->LazyWriteContext);
213 CcRosVacbDecRefCount(current);
214 continue;
215 }
216
217 ASSERT(current->Dirty);
218
219 /* One reference is added above */
220 if (current->ReferenceCount > 2)
221 {
222 KeReleaseMutex(&current->Mutex, FALSE);
223 current->Bcb->Callbacks->ReleaseFromLazyWrite(
224 current->Bcb->LazyWriteContext);
225 CcRosVacbDecRefCount(current);
226 continue;
227 }
228
229 KeReleaseGuardedMutex(&ViewLock);
230
231 Status = CcRosFlushVacb(current);
232
233 KeReleaseMutex(&current->Mutex, FALSE);
234 current->Bcb->Callbacks->ReleaseFromLazyWrite(
235 current->Bcb->LazyWriteContext);
236
237 KeAcquireGuardedMutex(&ViewLock);
238 CcRosVacbDecRefCount(current);
239
240 if (!NT_SUCCESS(Status) && (Status != STATUS_END_OF_FILE))
241 {
242 DPRINT1("CC: Failed to flush VACB.\n");
243 }
244 else
245 {
246 (*Count) += VACB_MAPPING_GRANULARITY / PAGE_SIZE;
247 Target -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
248 }
249
250 current_entry = DirtyVacbListHead.Flink;
251 }
252
253 KeReleaseGuardedMutex(&ViewLock);
254 KeLeaveCriticalRegion();
255
256 DPRINT("CcRosFlushDirtyPages() finished\n");
257 return STATUS_SUCCESS;
258 }
259
260 NTSTATUS
261 CcRosTrimCache (
262 ULONG Target,
263 ULONG Priority,
264 PULONG NrFreed)
265 /*
266 * FUNCTION: Try to free some memory from the file cache.
267 * ARGUMENTS:
268 * Target - The number of pages to be freed.
269 * Priority - The priority of free (currently unused).
270 * NrFreed - Points to a variable where the number of pages
271 * actually freed is returned.
272 */
273 {
274 PLIST_ENTRY current_entry;
275 PROS_VACB current;
276 ULONG PagesFreed;
277 KIRQL oldIrql;
278 LIST_ENTRY FreeList;
279 PFN_NUMBER Page;
280 ULONG i;
281 BOOLEAN FlushedPages = FALSE;
282
283 DPRINT("CcRosTrimCache(Target %lu)\n", Target);
284
285 InitializeListHead(&FreeList);
286
287 *NrFreed = 0;
288
289 retry:
290 KeAcquireGuardedMutex(&ViewLock);
291
292 current_entry = VacbLruListHead.Flink;
293 while (current_entry != &VacbLruListHead)
294 {
295 current = CONTAINING_RECORD(current_entry,
296 ROS_VACB,
297 VacbLruListEntry);
298 current_entry = current_entry->Flink;
299
300 KeAcquireSpinLock(&current->Bcb->BcbLock, &oldIrql);
301
302 /* Reference the VACB */
303 CcRosVacbIncRefCount(current);
304
305 /* Check if it's mapped and not dirty */
306 if (current->MappedCount > 0 && !current->Dirty)
307 {
308 /* We have to break these locks because Cc sucks */
309 KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
310 KeReleaseGuardedMutex(&ViewLock);
311
312 /* Page out the VACB */
313 for (i = 0; i < VACB_MAPPING_GRANULARITY / PAGE_SIZE; i++)
314 {
315 Page = (PFN_NUMBER)(MmGetPhysicalAddress((PUCHAR)current->BaseAddress + (i * PAGE_SIZE)).QuadPart >> PAGE_SHIFT);
316
317 MmPageOutPhysicalAddress(Page);
318 }
319
320 /* Reacquire the locks */
321 KeAcquireGuardedMutex(&ViewLock);
322 KeAcquireSpinLock(&current->Bcb->BcbLock, &oldIrql);
323 }
324
325 /* Dereference the VACB */
326 CcRosVacbDecRefCount(current);
327
328 /* Check if we can free this entry now */
329 if (current->ReferenceCount == 0)
330 {
331 ASSERT(!current->Dirty);
332 ASSERT(!current->MappedCount);
333
334 RemoveEntryList(&current->BcbVacbListEntry);
335 RemoveEntryList(&current->VacbListEntry);
336 RemoveEntryList(&current->VacbLruListEntry);
337 InsertHeadList(&FreeList, &current->BcbVacbListEntry);
338
339 /* Calculate how many pages we freed for Mm */
340 PagesFreed = min(VACB_MAPPING_GRANULARITY / PAGE_SIZE, Target);
341 Target -= PagesFreed;
342 (*NrFreed) += PagesFreed;
343 }
344
345 KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
346 }
347
348 KeReleaseGuardedMutex(&ViewLock);
349
350 /* Try flushing pages if we haven't met our target */
351 if ((Target > 0) && !FlushedPages)
352 {
353 /* Flush dirty pages to disk */
354 CcRosFlushDirtyPages(Target, &PagesFreed, FALSE);
355 FlushedPages = TRUE;
356
357 /* We can only swap as many pages as we flushed */
358 if (PagesFreed < Target) Target = PagesFreed;
359
360 /* Check if we flushed anything */
361 if (PagesFreed != 0)
362 {
363 /* Try again after flushing dirty pages */
364 DPRINT("Flushed %lu dirty cache pages to disk\n", PagesFreed);
365 goto retry;
366 }
367 }
368
369 while (!IsListEmpty(&FreeList))
370 {
371 current_entry = RemoveHeadList(&FreeList);
372 current = CONTAINING_RECORD(current_entry,
373 ROS_VACB,
374 BcbVacbListEntry);
375 CcRosInternalFreeVacb(current);
376 }
377
378 DPRINT("Evicted %lu cache pages\n", (*NrFreed));
379
380 return STATUS_SUCCESS;
381 }
382
383 NTSTATUS
384 NTAPI
385 CcRosReleaseVacb (
386 PBCB Bcb,
387 PROS_VACB Vacb,
388 BOOLEAN Valid,
389 BOOLEAN Dirty,
390 BOOLEAN Mapped)
391 {
392 BOOLEAN WasDirty;
393 KIRQL oldIrql;
394
395 ASSERT(Bcb);
396
397 DPRINT("CcRosReleaseVacb(Bcb 0x%p, Vacb 0x%p, Valid %u)\n",
398 Bcb, Vacb, Valid);
399
400 KeAcquireGuardedMutex(&ViewLock);
401 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
402
403 Vacb->Valid = Valid;
404
405 WasDirty = Vacb->Dirty;
406 Vacb->Dirty = Vacb->Dirty || Dirty;
407
408 if (!WasDirty && Vacb->Dirty)
409 {
410 InsertTailList(&DirtyVacbListHead, &Vacb->DirtyVacbListEntry);
411 DirtyPageCount += VACB_MAPPING_GRANULARITY / PAGE_SIZE;
412 }
413
414 if (Mapped)
415 {
416 Vacb->MappedCount++;
417 }
418 CcRosVacbDecRefCount(Vacb);
419 if (Mapped && (Vacb->MappedCount == 1))
420 {
421 CcRosVacbIncRefCount(Vacb);
422 }
423 if (!WasDirty && Vacb->Dirty)
424 {
425 CcRosVacbIncRefCount(Vacb);
426 }
427
428 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
429 KeReleaseGuardedMutex(&ViewLock);
430 KeReleaseMutex(&Vacb->Mutex, FALSE);
431
432 return STATUS_SUCCESS;
433 }
434
435 /* Returns with VACB Lock Held! */
436 PROS_VACB
437 NTAPI
438 CcRosLookupVacb (
439 PBCB Bcb,
440 ULONG FileOffset)
441 {
442 PLIST_ENTRY current_entry;
443 PROS_VACB current;
444 KIRQL oldIrql;
445
446 ASSERT(Bcb);
447
448 DPRINT("CcRosLookupVacb(Bcb -x%p, FileOffset %lu)\n", Bcb, FileOffset);
449
450 KeAcquireGuardedMutex(&ViewLock);
451 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
452
453 current_entry = Bcb->BcbVacbListHead.Flink;
454 while (current_entry != &Bcb->BcbVacbListHead)
455 {
456 current = CONTAINING_RECORD(current_entry,
457 ROS_VACB,
458 BcbVacbListEntry);
459 if (IsPointInRange(current->FileOffset, VACB_MAPPING_GRANULARITY,
460 FileOffset))
461 {
462 CcRosVacbIncRefCount(current);
463 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
464 KeReleaseGuardedMutex(&ViewLock);
465 KeWaitForSingleObject(&current->Mutex,
466 Executive,
467 KernelMode,
468 FALSE,
469 NULL);
470 return current;
471 }
472 if (current->FileOffset > FileOffset)
473 break;
474 current_entry = current_entry->Flink;
475 }
476
477 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
478 KeReleaseGuardedMutex(&ViewLock);
479
480 return NULL;
481 }
482
483 NTSTATUS
484 NTAPI
485 CcRosMarkDirtyVacb (
486 PBCB Bcb,
487 ULONG FileOffset)
488 {
489 PROS_VACB Vacb;
490 KIRQL oldIrql;
491
492 ASSERT(Bcb);
493
494 DPRINT("CcRosMarkDirtyVacb(Bcb 0x%p, FileOffset %lu)\n", Bcb, FileOffset);
495
496 Vacb = CcRosLookupVacb(Bcb, FileOffset);
497 if (Vacb == NULL)
498 {
499 KeBugCheck(CACHE_MANAGER);
500 }
501
502 KeAcquireGuardedMutex(&ViewLock);
503 KeAcquireSpinLock(&Bcb->BcbLock, &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(&Bcb->BcbLock, oldIrql);
522 KeReleaseGuardedMutex(&ViewLock);
523 KeReleaseMutex(&Vacb->Mutex, FALSE);
524
525 return STATUS_SUCCESS;
526 }
527
528 NTSTATUS
529 NTAPI
530 CcRosUnmapVacb (
531 PBCB Bcb,
532 ULONG FileOffset,
533 BOOLEAN NowDirty)
534 {
535 PROS_VACB Vacb;
536 BOOLEAN WasDirty;
537 KIRQL oldIrql;
538
539 ASSERT(Bcb);
540
541 DPRINT("CcRosUnmapVacb(Bcb 0x%p, FileOffset %lu, NowDirty %u)\n",
542 Bcb, FileOffset, NowDirty);
543
544 Vacb = CcRosLookupVacb(Bcb, FileOffset);
545 if (Vacb == NULL)
546 {
547 return STATUS_UNSUCCESSFUL;
548 }
549
550 KeAcquireGuardedMutex(&ViewLock);
551 KeAcquireSpinLock(&Bcb->BcbLock, &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(&Bcb->BcbLock, oldIrql);
575 KeReleaseGuardedMutex(&ViewLock);
576 KeReleaseMutex(&Vacb->Mutex, FALSE);
577
578 return STATUS_SUCCESS;
579 }
580
581 static
582 NTSTATUS
583 CcRosCreateVacb (
584 PBCB Bcb,
585 ULONG 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(Bcb);
595
596 DPRINT("CcRosCreateVacb()\n");
597
598 if (FileOffset >= Bcb->FileSize.u.LowPart)
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 = ROUND_DOWN(FileOffset, VACB_MAPPING_GRANULARITY);
609 current->Bcb = Bcb;
610 #if DBG
611 if ( Bcb->Trace )
612 {
613 DPRINT1("CacheMap 0x%p: new VACB: 0x%p\n", Bcb, 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(&Bcb->BcbLock, &oldIrql);
635 current_entry = Bcb->BcbVacbListHead.Flink;
636 previous = NULL;
637 while (current_entry != &Bcb->BcbVacbListHead)
638 {
639 current = CONTAINING_RECORD(current_entry,
640 ROS_VACB,
641 BcbVacbListEntry);
642 if (IsPointInRange(current->FileOffset, VACB_MAPPING_GRANULARITY,
643 FileOffset))
644 {
645 CcRosVacbIncRefCount(current);
646 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
647 #if DBG
648 if ( Bcb->Trace )
649 {
650 DPRINT1("CacheMap 0x%p: deleting newly created VACB 0x%p ( found existing one 0x%p )\n",
651 Bcb,
652 (*Vacb),
653 current );
654 }
655 #endif
656 KeReleaseMutex(&(*Vacb)->Mutex, FALSE);
657 KeReleaseGuardedMutex(&ViewLock);
658 ExFreeToNPagedLookasideList(&VacbLookasideList, *Vacb);
659 *Vacb = current;
660 KeWaitForSingleObject(&current->Mutex,
661 Executive,
662 KernelMode,
663 FALSE,
664 NULL);
665 return STATUS_SUCCESS;
666 }
667 if (current->FileOffset < FileOffset)
668 {
669 ASSERT(previous == NULL ||
670 previous->FileOffset < current->FileOffset);
671 previous = current;
672 }
673 if (current->FileOffset > FileOffset)
674 break;
675 current_entry = current_entry->Flink;
676 }
677 /* There was no existing VACB. */
678 current = *Vacb;
679 if (previous)
680 {
681 InsertHeadList(&previous->BcbVacbListEntry, &current->BcbVacbListEntry);
682 }
683 else
684 {
685 InsertHeadList(&Bcb->BcbVacbListHead, &current->BcbVacbListEntry);
686 }
687 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
688 InsertTailList(&VacbListHead, &current->VacbListEntry);
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 ((Bcb->FileObject) && (Bcb->FileObject->FileName.Buffer))
715 {
716 pos = wcsrchr(Bcb->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 CcRosGetVacbChain (
731 PBCB Bcb,
732 ULONG FileOffset,
733 ULONG Length,
734 PROS_VACB *Vacb)
735 {
736 PROS_VACB current;
737 ULONG i;
738 PROS_VACB *VacbList;
739 PROS_VACB Previous = NULL;
740
741 ASSERT(Bcb);
742
743 DPRINT("CcRosGetVacbChain()\n");
744
745 Length = ROUND_UP(Length, VACB_MAPPING_GRANULARITY);
746
747 VacbList = _alloca(sizeof(PROS_VACB) *
748 (Length / VACB_MAPPING_GRANULARITY));
749
750 /*
751 * Look for a VACB already mapping the same data.
752 */
753 for (i = 0; i < (Length / VACB_MAPPING_GRANULARITY); i++)
754 {
755 ULONG CurrentOffset = FileOffset + (i * VACB_MAPPING_GRANULARITY);
756 current = CcRosLookupVacb(Bcb, CurrentOffset);
757 if (current != NULL)
758 {
759 KeAcquireGuardedMutex(&ViewLock);
760
761 /* Move to tail of LRU list */
762 RemoveEntryList(&current->VacbLruListEntry);
763 InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
764
765 KeReleaseGuardedMutex(&ViewLock);
766
767 VacbList[i] = current;
768 }
769 else
770 {
771 CcRosCreateVacb(Bcb, CurrentOffset, &current);
772 VacbList[i] = current;
773 }
774 }
775
776 for (i = 0; i < Length / VACB_MAPPING_GRANULARITY; i++)
777 {
778 if (i == 0)
779 {
780 *Vacb = VacbList[i];
781 Previous = VacbList[i];
782 }
783 else
784 {
785 Previous->NextInChain = VacbList[i];
786 Previous = VacbList[i];
787 }
788 }
789 ASSERT(Previous);
790 Previous->NextInChain = NULL;
791
792 return STATUS_SUCCESS;
793 }
794
795 NTSTATUS
796 NTAPI
797 CcRosGetVacb (
798 PBCB Bcb,
799 ULONG FileOffset,
800 PULONG BaseOffset,
801 PVOID* BaseAddress,
802 PBOOLEAN UptoDate,
803 PROS_VACB *Vacb)
804 {
805 PROS_VACB current;
806 NTSTATUS Status;
807
808 ASSERT(Bcb);
809
810 DPRINT("CcRosGetVacb()\n");
811
812 /*
813 * Look for a VACB already mapping the same data.
814 */
815 current = CcRosLookupVacb(Bcb, FileOffset);
816 if (current == NULL)
817 {
818 /*
819 * Otherwise create a new VACB.
820 */
821 Status = CcRosCreateVacb(Bcb, FileOffset, &current);
822 if (!NT_SUCCESS(Status))
823 {
824 return Status;
825 }
826 }
827
828 KeAcquireGuardedMutex(&ViewLock);
829
830 /* Move to the tail of the LRU list */
831 RemoveEntryList(&current->VacbLruListEntry);
832 InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
833
834 KeReleaseGuardedMutex(&ViewLock);
835
836 /*
837 * Return information about the VACB to the caller.
838 */
839 *UptoDate = current->Valid;
840 *BaseAddress = current->BaseAddress;
841 DPRINT("*BaseAddress %p\n", *BaseAddress);
842 *Vacb = current;
843 *BaseOffset = current->FileOffset;
844 return STATUS_SUCCESS;
845 }
846
847 NTSTATUS
848 NTAPI
849 CcRosRequestVacb (
850 PBCB Bcb,
851 ULONG FileOffset,
852 PVOID* BaseAddress,
853 PBOOLEAN UptoDate,
854 PROS_VACB *Vacb)
855 /*
856 * FUNCTION: Request a page mapping for a BCB
857 */
858 {
859 ULONG BaseOffset;
860
861 ASSERT(Bcb);
862
863 if (FileOffset % VACB_MAPPING_GRANULARITY != 0)
864 {
865 DPRINT1("Bad fileoffset %x should be multiple of %x",
866 FileOffset, VACB_MAPPING_GRANULARITY);
867 KeBugCheck(CACHE_MANAGER);
868 }
869
870 return CcRosGetVacb(Bcb,
871 FileOffset,
872 &BaseOffset,
873 BaseAddress,
874 UptoDate,
875 Vacb);
876 }
877
878 static
879 VOID
880 CcFreeCachePage (
881 PVOID Context,
882 MEMORY_AREA* MemoryArea,
883 PVOID Address,
884 PFN_NUMBER Page,
885 SWAPENTRY SwapEntry,
886 BOOLEAN Dirty)
887 {
888 ASSERT(SwapEntry == 0);
889 if (Page != 0)
890 {
891 ASSERT(MmGetReferenceCountPage(Page) == 1);
892 MmReleasePageMemoryConsumer(MC_CACHE, Page);
893 }
894 }
895
896 NTSTATUS
897 CcRosInternalFreeVacb (
898 PROS_VACB Vacb)
899 /*
900 * FUNCTION: Releases a VACB associated with a BCB
901 */
902 {
903 DPRINT("Freeing VACB 0x%p\n", Vacb);
904 #if DBG
905 if (Vacb->Bcb->Trace)
906 {
907 DPRINT1("CacheMap 0x%p: deleting VACB: 0x%p\n", Vacb->Bcb, Vacb);
908 }
909 #endif
910
911 MmLockAddressSpace(MmGetKernelAddressSpace());
912 MmFreeMemoryArea(MmGetKernelAddressSpace(),
913 Vacb->MemoryArea,
914 CcFreeCachePage,
915 NULL);
916 MmUnlockAddressSpace(MmGetKernelAddressSpace());
917
918 ExFreeToNPagedLookasideList(&VacbLookasideList, Vacb);
919 return STATUS_SUCCESS;
920 }
921
922 /*
923 * @implemented
924 */
925 VOID
926 NTAPI
927 CcFlushCache (
928 IN PSECTION_OBJECT_POINTERS SectionObjectPointers,
929 IN PLARGE_INTEGER FileOffset OPTIONAL,
930 IN ULONG Length,
931 OUT PIO_STATUS_BLOCK IoStatus)
932 {
933 PBCB Bcb;
934 LARGE_INTEGER Offset;
935 PROS_VACB current;
936 NTSTATUS Status;
937 KIRQL oldIrql;
938
939 DPRINT("CcFlushCache(SectionObjectPointers 0x%p, FileOffset 0x%p, Length %lu, IoStatus 0x%p)\n",
940 SectionObjectPointers, FileOffset, Length, IoStatus);
941
942 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
943 {
944 Bcb = (PBCB)SectionObjectPointers->SharedCacheMap;
945 ASSERT(Bcb);
946 if (FileOffset)
947 {
948 Offset = *FileOffset;
949 }
950 else
951 {
952 Offset.QuadPart = (LONGLONG)0;
953 Length = Bcb->FileSize.u.LowPart;
954 }
955
956 if (IoStatus)
957 {
958 IoStatus->Status = STATUS_SUCCESS;
959 IoStatus->Information = 0;
960 }
961
962 while (Length > 0)
963 {
964 current = CcRosLookupVacb(Bcb, Offset.u.LowPart);
965 if (current != NULL)
966 {
967 if (current->Dirty)
968 {
969 Status = CcRosFlushVacb(current);
970 if (!NT_SUCCESS(Status) && IoStatus != NULL)
971 {
972 IoStatus->Status = Status;
973 }
974 }
975 KeReleaseMutex(&current->Mutex, FALSE);
976
977 KeAcquireGuardedMutex(&ViewLock);
978 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
979 CcRosVacbDecRefCount(current);
980 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
981 KeReleaseGuardedMutex(&ViewLock);
982 }
983
984 Offset.QuadPart += VACB_MAPPING_GRANULARITY;
985 if (Length > VACB_MAPPING_GRANULARITY)
986 {
987 Length -= VACB_MAPPING_GRANULARITY;
988 }
989 else
990 {
991 Length = 0;
992 }
993 }
994 }
995 else
996 {
997 if (IoStatus)
998 {
999 IoStatus->Status = STATUS_INVALID_PARAMETER;
1000 }
1001 }
1002 }
1003
1004 NTSTATUS
1005 NTAPI
1006 CcRosDeleteFileCache (
1007 PFILE_OBJECT FileObject,
1008 PBCB Bcb)
1009 /*
1010 * FUNCTION: Releases the BCB associated with a file object
1011 */
1012 {
1013 PLIST_ENTRY current_entry;
1014 PROS_VACB current;
1015 LIST_ENTRY FreeList;
1016 KIRQL oldIrql;
1017
1018 ASSERT(Bcb);
1019
1020 Bcb->RefCount++;
1021 KeReleaseGuardedMutex(&ViewLock);
1022
1023 CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, NULL);
1024
1025 KeAcquireGuardedMutex(&ViewLock);
1026 Bcb->RefCount--;
1027 if (Bcb->RefCount == 0)
1028 {
1029 if (Bcb->BcbRemoveListEntry.Flink != NULL)
1030 {
1031 RemoveEntryList(&Bcb->BcbRemoveListEntry);
1032 Bcb->BcbRemoveListEntry.Flink = NULL;
1033 }
1034
1035 FileObject->SectionObjectPointer->SharedCacheMap = NULL;
1036
1037 /*
1038 * Release all VACBs
1039 */
1040 InitializeListHead(&FreeList);
1041 KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
1042 while (!IsListEmpty(&Bcb->BcbVacbListHead))
1043 {
1044 current_entry = RemoveTailList(&Bcb->BcbVacbListHead);
1045 current = CONTAINING_RECORD(current_entry, ROS_VACB, BcbVacbListEntry);
1046 RemoveEntryList(&current->VacbListEntry);
1047 RemoveEntryList(&current->VacbLruListEntry);
1048 if (current->Dirty)
1049 {
1050 RemoveEntryList(&current->DirtyVacbListEntry);
1051 DirtyPageCount -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
1052 DPRINT1("Freeing dirty VACB\n");
1053 }
1054 InsertHeadList(&FreeList, &current->BcbVacbListEntry);
1055 }
1056 #if DBG
1057 Bcb->Trace = FALSE;
1058 #endif
1059 KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
1060
1061 KeReleaseGuardedMutex(&ViewLock);
1062 ObDereferenceObject (Bcb->FileObject);
1063
1064 while (!IsListEmpty(&FreeList))
1065 {
1066 current_entry = RemoveTailList(&FreeList);
1067 current = CONTAINING_RECORD(current_entry, ROS_VACB, BcbVacbListEntry);
1068 CcRosInternalFreeVacb(current);
1069 }
1070 ExFreeToNPagedLookasideList(&BcbLookasideList, Bcb);
1071 KeAcquireGuardedMutex(&ViewLock);
1072 }
1073 return STATUS_SUCCESS;
1074 }
1075
1076 VOID
1077 NTAPI
1078 CcRosReferenceCache (
1079 PFILE_OBJECT FileObject)
1080 {
1081 PBCB Bcb;
1082 KeAcquireGuardedMutex(&ViewLock);
1083 Bcb = (PBCB)FileObject->SectionObjectPointer->SharedCacheMap;
1084 ASSERT(Bcb);
1085 if (Bcb->RefCount == 0)
1086 {
1087 ASSERT(Bcb->BcbRemoveListEntry.Flink != NULL);
1088 RemoveEntryList(&Bcb->BcbRemoveListEntry);
1089 Bcb->BcbRemoveListEntry.Flink = NULL;
1090
1091 }
1092 else
1093 {
1094 ASSERT(Bcb->BcbRemoveListEntry.Flink == NULL);
1095 }
1096 Bcb->RefCount++;
1097 KeReleaseGuardedMutex(&ViewLock);
1098 }
1099
1100 VOID
1101 NTAPI
1102 CcRosSetRemoveOnClose (
1103 PSECTION_OBJECT_POINTERS SectionObjectPointer)
1104 {
1105 PBCB Bcb;
1106 DPRINT("CcRosSetRemoveOnClose()\n");
1107 KeAcquireGuardedMutex(&ViewLock);
1108 Bcb = (PBCB)SectionObjectPointer->SharedCacheMap;
1109 if (Bcb)
1110 {
1111 Bcb->RemoveOnClose = TRUE;
1112 if (Bcb->RefCount == 0)
1113 {
1114 CcRosDeleteFileCache(Bcb->FileObject, Bcb);
1115 }
1116 }
1117 KeReleaseGuardedMutex(&ViewLock);
1118 }
1119
1120
1121 VOID
1122 NTAPI
1123 CcRosDereferenceCache (
1124 PFILE_OBJECT FileObject)
1125 {
1126 PBCB Bcb;
1127 KeAcquireGuardedMutex(&ViewLock);
1128 Bcb = (PBCB)FileObject->SectionObjectPointer->SharedCacheMap;
1129 ASSERT(Bcb);
1130 if (Bcb->RefCount > 0)
1131 {
1132 Bcb->RefCount--;
1133 if (Bcb->RefCount == 0)
1134 {
1135 MmFreeSectionSegments(Bcb->FileObject);
1136 CcRosDeleteFileCache(FileObject, Bcb);
1137 }
1138 }
1139 KeReleaseGuardedMutex(&ViewLock);
1140 }
1141
1142 NTSTATUS
1143 NTAPI
1144 CcRosReleaseFileCache (
1145 PFILE_OBJECT FileObject)
1146 /*
1147 * FUNCTION: Called by the file system when a handle to a file object
1148 * has been closed.
1149 */
1150 {
1151 PBCB Bcb;
1152
1153 KeAcquireGuardedMutex(&ViewLock);
1154
1155 if (FileObject->SectionObjectPointer->SharedCacheMap != NULL)
1156 {
1157 Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
1158 if (FileObject->PrivateCacheMap != NULL)
1159 {
1160 FileObject->PrivateCacheMap = NULL;
1161 if (Bcb->RefCount > 0)
1162 {
1163 Bcb->RefCount--;
1164 if (Bcb->RefCount == 0)
1165 {
1166 MmFreeSectionSegments(Bcb->FileObject);
1167 CcRosDeleteFileCache(FileObject, Bcb);
1168 }
1169 }
1170 }
1171 }
1172 KeReleaseGuardedMutex(&ViewLock);
1173 return STATUS_SUCCESS;
1174 }
1175
1176 NTSTATUS
1177 NTAPI
1178 CcTryToInitializeFileCache (
1179 PFILE_OBJECT FileObject)
1180 {
1181 PBCB Bcb;
1182 NTSTATUS Status;
1183
1184 KeAcquireGuardedMutex(&ViewLock);
1185
1186 ASSERT(FileObject->SectionObjectPointer);
1187 Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
1188 if (Bcb == NULL)
1189 {
1190 Status = STATUS_UNSUCCESSFUL;
1191 }
1192 else
1193 {
1194 if (FileObject->PrivateCacheMap == NULL)
1195 {
1196 FileObject->PrivateCacheMap = Bcb;
1197 Bcb->RefCount++;
1198 }
1199 if (Bcb->BcbRemoveListEntry.Flink != NULL)
1200 {
1201 RemoveEntryList(&Bcb->BcbRemoveListEntry);
1202 Bcb->BcbRemoveListEntry.Flink = NULL;
1203 }
1204 Status = STATUS_SUCCESS;
1205 }
1206 KeReleaseGuardedMutex(&ViewLock);
1207
1208 return Status;
1209 }
1210
1211
1212 NTSTATUS
1213 NTAPI
1214 CcRosInitializeFileCache (
1215 PFILE_OBJECT FileObject,
1216 PCACHE_MANAGER_CALLBACKS CallBacks,
1217 PVOID LazyWriterContext)
1218 /*
1219 * FUNCTION: Initializes a BCB for a file object
1220 */
1221 {
1222 PBCB Bcb;
1223
1224 Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
1225 DPRINT("CcRosInitializeFileCache(FileObject 0x%p, Bcb 0x%p)\n",
1226 FileObject, Bcb);
1227
1228 KeAcquireGuardedMutex(&ViewLock);
1229 if (Bcb == NULL)
1230 {
1231 Bcb = ExAllocateFromNPagedLookasideList(&BcbLookasideList);
1232 if (Bcb == NULL)
1233 {
1234 KeReleaseGuardedMutex(&ViewLock);
1235 return STATUS_UNSUCCESSFUL;
1236 }
1237 RtlZeroMemory(Bcb, sizeof(*Bcb));
1238 ObReferenceObjectByPointer(FileObject,
1239 FILE_ALL_ACCESS,
1240 NULL,
1241 KernelMode);
1242 Bcb->FileObject = FileObject;
1243 Bcb->Callbacks = CallBacks;
1244 Bcb->LazyWriteContext = LazyWriterContext;
1245 if (FileObject->FsContext)
1246 {
1247 Bcb->AllocationSize =
1248 ((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->AllocationSize;
1249 Bcb->FileSize =
1250 ((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->FileSize;
1251 }
1252 KeInitializeSpinLock(&Bcb->BcbLock);
1253 InitializeListHead(&Bcb->BcbVacbListHead);
1254 FileObject->SectionObjectPointer->SharedCacheMap = Bcb;
1255 }
1256 if (FileObject->PrivateCacheMap == NULL)
1257 {
1258 FileObject->PrivateCacheMap = Bcb;
1259 Bcb->RefCount++;
1260 }
1261 if (Bcb->BcbRemoveListEntry.Flink != NULL)
1262 {
1263 RemoveEntryList(&Bcb->BcbRemoveListEntry);
1264 Bcb->BcbRemoveListEntry.Flink = NULL;
1265 }
1266 KeReleaseGuardedMutex(&ViewLock);
1267
1268 return STATUS_SUCCESS;
1269 }
1270
1271 /*
1272 * @implemented
1273 */
1274 PFILE_OBJECT
1275 NTAPI
1276 CcGetFileObjectFromSectionPtrs (
1277 IN PSECTION_OBJECT_POINTERS SectionObjectPointers)
1278 {
1279 PBCB Bcb;
1280 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
1281 {
1282 Bcb = (PBCB)SectionObjectPointers->SharedCacheMap;
1283 ASSERT(Bcb);
1284 return Bcb->FileObject;
1285 }
1286 return NULL;
1287 }
1288
1289 VOID
1290 INIT_FUNCTION
1291 NTAPI
1292 CcInitView (
1293 VOID)
1294 {
1295 DPRINT("CcInitView()\n");
1296
1297 InitializeListHead(&VacbListHead);
1298 InitializeListHead(&DirtyVacbListHead);
1299 InitializeListHead(&VacbLruListHead);
1300 InitializeListHead(&ClosedListHead);
1301 KeInitializeGuardedMutex(&ViewLock);
1302 ExInitializeNPagedLookasideList (&iBcbLookasideList,
1303 NULL,
1304 NULL,
1305 0,
1306 sizeof(INTERNAL_BCB),
1307 TAG_IBCB,
1308 20);
1309 ExInitializeNPagedLookasideList (&BcbLookasideList,
1310 NULL,
1311 NULL,
1312 0,
1313 sizeof(BCB),
1314 TAG_BCB,
1315 20);
1316 ExInitializeNPagedLookasideList (&VacbLookasideList,
1317 NULL,
1318 NULL,
1319 0,
1320 sizeof(ROS_VACB),
1321 TAG_CSEG,
1322 20);
1323
1324 MmInitializeMemoryConsumer(MC_CACHE, CcRosTrimCache);
1325
1326 CcInitCacheZeroPage();
1327
1328 }
1329
1330 /* EOF */