* Sync up to trunk head (r65074).
[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 */
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 CcRosMapVacb(
584 PROS_VACB Vacb)
585 {
586 ULONG i;
587 NTSTATUS Status;
588 ULONG_PTR NumberOfPages;
589
590 /* Create a memory area. */
591 MmLockAddressSpace(MmGetKernelAddressSpace());
592 Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
593 0, // nothing checks for VACB mareas, so set to 0
594 &Vacb->BaseAddress,
595 VACB_MAPPING_GRANULARITY,
596 PAGE_READWRITE,
597 (PMEMORY_AREA*)&Vacb->MemoryArea,
598 FALSE,
599 0,
600 PAGE_SIZE);
601 MmUnlockAddressSpace(MmGetKernelAddressSpace());
602 if (!NT_SUCCESS(Status))
603 {
604 KeBugCheck(CACHE_MANAGER);
605 }
606
607 ASSERT(((ULONG_PTR)Vacb->BaseAddress % PAGE_SIZE) == 0);
608 ASSERT((ULONG_PTR)Vacb->BaseAddress > (ULONG_PTR)MmSystemRangeStart);
609
610 /* Create a virtual mapping for this memory area */
611 NumberOfPages = BYTES_TO_PAGES(VACB_MAPPING_GRANULARITY);
612 for (i = 0; i < NumberOfPages; i++)
613 {
614 PFN_NUMBER PageFrameNumber;
615
616 Status = MmRequestPageMemoryConsumer(MC_CACHE, TRUE, &PageFrameNumber);
617 if (PageFrameNumber == 0)
618 {
619 DPRINT1("Unable to allocate page\n");
620 KeBugCheck(MEMORY_MANAGEMENT);
621 }
622
623 Status = MmCreateVirtualMapping(NULL,
624 (PVOID)((ULONG_PTR)Vacb->BaseAddress + (i * PAGE_SIZE)),
625 PAGE_READWRITE,
626 &PageFrameNumber,
627 1);
628 if (!NT_SUCCESS(Status))
629 {
630 DPRINT1("Unable to create virtual mapping\n");
631 KeBugCheck(MEMORY_MANAGEMENT);
632 }
633 }
634
635 return STATUS_SUCCESS;
636 }
637
638 static
639 NTSTATUS
640 CcRosCreateVacb (
641 PROS_SHARED_CACHE_MAP SharedCacheMap,
642 LONGLONG FileOffset,
643 PROS_VACB *Vacb)
644 {
645 PROS_VACB current;
646 PROS_VACB previous;
647 PLIST_ENTRY current_entry;
648 NTSTATUS Status;
649 KIRQL oldIrql;
650
651 ASSERT(SharedCacheMap);
652
653 DPRINT("CcRosCreateVacb()\n");
654
655 if (FileOffset >= SharedCacheMap->FileSize.QuadPart)
656 {
657 *Vacb = NULL;
658 return STATUS_INVALID_PARAMETER;
659 }
660
661 current = ExAllocateFromNPagedLookasideList(&VacbLookasideList);
662 current->BaseAddress = NULL;
663 current->Valid = FALSE;
664 current->Dirty = FALSE;
665 current->PageOut = FALSE;
666 current->FileOffset.QuadPart = ROUND_DOWN(FileOffset, VACB_MAPPING_GRANULARITY);
667 current->SharedCacheMap = SharedCacheMap;
668 #if DBG
669 if (SharedCacheMap->Trace)
670 {
671 DPRINT1("CacheMap 0x%p: new VACB: 0x%p\n", SharedCacheMap, current);
672 }
673 #endif
674 current->MappedCount = 0;
675 current->DirtyVacbListEntry.Flink = NULL;
676 current->DirtyVacbListEntry.Blink = NULL;
677 current->ReferenceCount = 1;
678 KeInitializeMutex(&current->Mutex, 0);
679 KeWaitForSingleObject(&current->Mutex,
680 Executive,
681 KernelMode,
682 FALSE,
683 NULL);
684 KeAcquireGuardedMutex(&ViewLock);
685
686 *Vacb = current;
687 /* There is window between the call to CcRosLookupVacb
688 * and CcRosCreateVacb. We must check if a VACB for the
689 * file offset exist. If there is a VACB, we release
690 * our newly created VACB and return the existing one.
691 */
692 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
693 current_entry = SharedCacheMap->CacheMapVacbListHead.Flink;
694 previous = NULL;
695 while (current_entry != &SharedCacheMap->CacheMapVacbListHead)
696 {
697 current = CONTAINING_RECORD(current_entry,
698 ROS_VACB,
699 CacheMapVacbListEntry);
700 if (IsPointInRange(current->FileOffset.QuadPart,
701 VACB_MAPPING_GRANULARITY,
702 FileOffset))
703 {
704 CcRosVacbIncRefCount(current);
705 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
706 #if DBG
707 if (SharedCacheMap->Trace)
708 {
709 DPRINT1("CacheMap 0x%p: deleting newly created VACB 0x%p ( found existing one 0x%p )\n",
710 SharedCacheMap,
711 (*Vacb),
712 current);
713 }
714 #endif
715 KeReleaseMutex(&(*Vacb)->Mutex, FALSE);
716 KeReleaseGuardedMutex(&ViewLock);
717 ExFreeToNPagedLookasideList(&VacbLookasideList, *Vacb);
718 *Vacb = current;
719 KeWaitForSingleObject(&current->Mutex,
720 Executive,
721 KernelMode,
722 FALSE,
723 NULL);
724 return STATUS_SUCCESS;
725 }
726 if (current->FileOffset.QuadPart < FileOffset)
727 {
728 ASSERT(previous == NULL ||
729 previous->FileOffset.QuadPart < current->FileOffset.QuadPart);
730 previous = current;
731 }
732 if (current->FileOffset.QuadPart > FileOffset)
733 break;
734 current_entry = current_entry->Flink;
735 }
736 /* There was no existing VACB. */
737 current = *Vacb;
738 if (previous)
739 {
740 InsertHeadList(&previous->CacheMapVacbListEntry, &current->CacheMapVacbListEntry);
741 }
742 else
743 {
744 InsertHeadList(&SharedCacheMap->CacheMapVacbListHead, &current->CacheMapVacbListEntry);
745 }
746 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
747 InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
748 KeReleaseGuardedMutex(&ViewLock);
749
750 MI_SET_USAGE(MI_USAGE_CACHE);
751 #if MI_TRACE_PFNS
752 if ((SharedCacheMap->FileObject) && (SharedCacheMap->FileObject->FileName.Buffer))
753 {
754 PWCHAR pos = NULL;
755 ULONG len = 0;
756 pos = wcsrchr(SharedCacheMap->FileObject->FileName.Buffer, '\\');
757 len = wcslen(pos) * sizeof(WCHAR);
758 if (pos) snprintf(MI_PFN_CURRENT_PROCESS_NAME, min(16, len), "%S", pos);
759 }
760 #endif
761
762 Status = CcRosMapVacb(current);
763
764 return Status;
765 }
766
767 NTSTATUS
768 NTAPI
769 CcRosGetVacb (
770 PROS_SHARED_CACHE_MAP SharedCacheMap,
771 LONGLONG FileOffset,
772 PLONGLONG BaseOffset,
773 PVOID* BaseAddress,
774 PBOOLEAN UptoDate,
775 PROS_VACB *Vacb)
776 {
777 PROS_VACB current;
778 NTSTATUS Status;
779
780 ASSERT(SharedCacheMap);
781
782 DPRINT("CcRosGetVacb()\n");
783
784 /*
785 * Look for a VACB already mapping the same data.
786 */
787 current = CcRosLookupVacb(SharedCacheMap, FileOffset);
788 if (current == NULL)
789 {
790 /*
791 * Otherwise create a new VACB.
792 */
793 Status = CcRosCreateVacb(SharedCacheMap, FileOffset, &current);
794 if (!NT_SUCCESS(Status))
795 {
796 return Status;
797 }
798 }
799
800 KeAcquireGuardedMutex(&ViewLock);
801
802 /* Move to the tail of the LRU list */
803 RemoveEntryList(&current->VacbLruListEntry);
804 InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
805
806 KeReleaseGuardedMutex(&ViewLock);
807
808 /*
809 * Return information about the VACB to the caller.
810 */
811 *UptoDate = current->Valid;
812 *BaseAddress = current->BaseAddress;
813 DPRINT("*BaseAddress %p\n", *BaseAddress);
814 *Vacb = current;
815 *BaseOffset = current->FileOffset.QuadPart;
816 return STATUS_SUCCESS;
817 }
818
819 NTSTATUS
820 NTAPI
821 CcRosRequestVacb (
822 PROS_SHARED_CACHE_MAP SharedCacheMap,
823 LONGLONG FileOffset,
824 PVOID* BaseAddress,
825 PBOOLEAN UptoDate,
826 PROS_VACB *Vacb)
827 /*
828 * FUNCTION: Request a page mapping for a shared cache map
829 */
830 {
831 LONGLONG BaseOffset;
832
833 ASSERT(SharedCacheMap);
834
835 if (FileOffset % VACB_MAPPING_GRANULARITY != 0)
836 {
837 DPRINT1("Bad fileoffset %I64x should be multiple of %x",
838 FileOffset, VACB_MAPPING_GRANULARITY);
839 KeBugCheck(CACHE_MANAGER);
840 }
841
842 return CcRosGetVacb(SharedCacheMap,
843 FileOffset,
844 &BaseOffset,
845 BaseAddress,
846 UptoDate,
847 Vacb);
848 }
849
850 static
851 VOID
852 CcFreeCachePage (
853 PVOID Context,
854 MEMORY_AREA* MemoryArea,
855 PVOID Address,
856 PFN_NUMBER Page,
857 SWAPENTRY SwapEntry,
858 BOOLEAN Dirty)
859 {
860 ASSERT(SwapEntry == 0);
861 if (Page != 0)
862 {
863 ASSERT(MmGetReferenceCountPage(Page) == 1);
864 MmReleasePageMemoryConsumer(MC_CACHE, Page);
865 }
866 }
867
868 NTSTATUS
869 CcRosInternalFreeVacb (
870 PROS_VACB Vacb)
871 /*
872 * FUNCTION: Releases a VACB associated with a shared cache map
873 */
874 {
875 DPRINT("Freeing VACB 0x%p\n", Vacb);
876 #if DBG
877 if (Vacb->SharedCacheMap->Trace)
878 {
879 DPRINT1("CacheMap 0x%p: deleting VACB: 0x%p\n", Vacb->SharedCacheMap, Vacb);
880 }
881 #endif
882
883 MmLockAddressSpace(MmGetKernelAddressSpace());
884 MmFreeMemoryArea(MmGetKernelAddressSpace(),
885 Vacb->MemoryArea,
886 CcFreeCachePage,
887 NULL);
888 MmUnlockAddressSpace(MmGetKernelAddressSpace());
889
890 ExFreeToNPagedLookasideList(&VacbLookasideList, Vacb);
891 return STATUS_SUCCESS;
892 }
893
894 /*
895 * @implemented
896 */
897 VOID
898 NTAPI
899 CcFlushCache (
900 IN PSECTION_OBJECT_POINTERS SectionObjectPointers,
901 IN PLARGE_INTEGER FileOffset OPTIONAL,
902 IN ULONG Length,
903 OUT PIO_STATUS_BLOCK IoStatus)
904 {
905 PROS_SHARED_CACHE_MAP SharedCacheMap;
906 LARGE_INTEGER Offset;
907 LONGLONG RemainingLength;
908 PROS_VACB current;
909 NTSTATUS Status;
910 KIRQL oldIrql;
911
912 DPRINT("CcFlushCache(SectionObjectPointers 0x%p, FileOffset 0x%p, Length %lu, IoStatus 0x%p)\n",
913 SectionObjectPointers, FileOffset, Length, IoStatus);
914
915 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
916 {
917 SharedCacheMap = SectionObjectPointers->SharedCacheMap;
918 ASSERT(SharedCacheMap);
919 if (FileOffset)
920 {
921 Offset = *FileOffset;
922 RemainingLength = Length;
923 }
924 else
925 {
926 Offset.QuadPart = 0;
927 RemainingLength = SharedCacheMap->FileSize.QuadPart;
928 }
929
930 if (IoStatus)
931 {
932 IoStatus->Status = STATUS_SUCCESS;
933 IoStatus->Information = 0;
934 }
935
936 while (RemainingLength > 0)
937 {
938 current = CcRosLookupVacb(SharedCacheMap, Offset.QuadPart);
939 if (current != NULL)
940 {
941 if (current->Dirty)
942 {
943 Status = CcRosFlushVacb(current);
944 if (!NT_SUCCESS(Status) && IoStatus != NULL)
945 {
946 IoStatus->Status = Status;
947 }
948 }
949 KeReleaseMutex(&current->Mutex, FALSE);
950
951 KeAcquireGuardedMutex(&ViewLock);
952 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
953 CcRosVacbDecRefCount(current);
954 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
955 KeReleaseGuardedMutex(&ViewLock);
956 }
957
958 Offset.QuadPart += VACB_MAPPING_GRANULARITY;
959 RemainingLength -= min(RemainingLength, VACB_MAPPING_GRANULARITY);
960 }
961 }
962 else
963 {
964 if (IoStatus)
965 {
966 IoStatus->Status = STATUS_INVALID_PARAMETER;
967 }
968 }
969 }
970
971 NTSTATUS
972 NTAPI
973 CcRosDeleteFileCache (
974 PFILE_OBJECT FileObject,
975 PROS_SHARED_CACHE_MAP SharedCacheMap)
976 /*
977 * FUNCTION: Releases the shared cache map associated with a file object
978 */
979 {
980 PLIST_ENTRY current_entry;
981 PROS_VACB current;
982 LIST_ENTRY FreeList;
983 KIRQL oldIrql;
984
985 ASSERT(SharedCacheMap);
986
987 SharedCacheMap->RefCount++;
988 KeReleaseGuardedMutex(&ViewLock);
989
990 CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, NULL);
991
992 KeAcquireGuardedMutex(&ViewLock);
993 SharedCacheMap->RefCount--;
994 if (SharedCacheMap->RefCount == 0)
995 {
996 FileObject->SectionObjectPointer->SharedCacheMap = NULL;
997
998 /*
999 * Release all VACBs
1000 */
1001 InitializeListHead(&FreeList);
1002 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
1003 while (!IsListEmpty(&SharedCacheMap->CacheMapVacbListHead))
1004 {
1005 current_entry = RemoveTailList(&SharedCacheMap->CacheMapVacbListHead);
1006 current = CONTAINING_RECORD(current_entry, ROS_VACB, CacheMapVacbListEntry);
1007 RemoveEntryList(&current->VacbLruListEntry);
1008 if (current->Dirty)
1009 {
1010 RemoveEntryList(&current->DirtyVacbListEntry);
1011 DirtyPageCount -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
1012 DPRINT1("Freeing dirty VACB\n");
1013 }
1014 InsertHeadList(&FreeList, &current->CacheMapVacbListEntry);
1015 }
1016 #if DBG
1017 SharedCacheMap->Trace = FALSE;
1018 #endif
1019 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
1020
1021 KeReleaseGuardedMutex(&ViewLock);
1022 ObDereferenceObject(SharedCacheMap->FileObject);
1023
1024 while (!IsListEmpty(&FreeList))
1025 {
1026 current_entry = RemoveTailList(&FreeList);
1027 current = CONTAINING_RECORD(current_entry, ROS_VACB, CacheMapVacbListEntry);
1028 CcRosInternalFreeVacb(current);
1029 }
1030 ExFreeToNPagedLookasideList(&SharedCacheMapLookasideList, SharedCacheMap);
1031 KeAcquireGuardedMutex(&ViewLock);
1032 }
1033 return STATUS_SUCCESS;
1034 }
1035
1036 VOID
1037 NTAPI
1038 CcRosReferenceCache (
1039 PFILE_OBJECT FileObject)
1040 {
1041 PROS_SHARED_CACHE_MAP SharedCacheMap;
1042 KeAcquireGuardedMutex(&ViewLock);
1043 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1044 ASSERT(SharedCacheMap);
1045 ASSERT(SharedCacheMap->RefCount != 0);
1046 SharedCacheMap->RefCount++;
1047 KeReleaseGuardedMutex(&ViewLock);
1048 }
1049
1050 VOID
1051 NTAPI
1052 CcRosRemoveIfClosed (
1053 PSECTION_OBJECT_POINTERS SectionObjectPointer)
1054 {
1055 PROS_SHARED_CACHE_MAP SharedCacheMap;
1056 DPRINT("CcRosRemoveIfClosed()\n");
1057 KeAcquireGuardedMutex(&ViewLock);
1058 SharedCacheMap = SectionObjectPointer->SharedCacheMap;
1059 if (SharedCacheMap && SharedCacheMap->RefCount == 0)
1060 {
1061 CcRosDeleteFileCache(SharedCacheMap->FileObject, SharedCacheMap);
1062 }
1063 KeReleaseGuardedMutex(&ViewLock);
1064 }
1065
1066
1067 VOID
1068 NTAPI
1069 CcRosDereferenceCache (
1070 PFILE_OBJECT FileObject)
1071 {
1072 PROS_SHARED_CACHE_MAP SharedCacheMap;
1073 KeAcquireGuardedMutex(&ViewLock);
1074 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1075 ASSERT(SharedCacheMap);
1076 if (SharedCacheMap->RefCount > 0)
1077 {
1078 SharedCacheMap->RefCount--;
1079 if (SharedCacheMap->RefCount == 0)
1080 {
1081 MmFreeSectionSegments(SharedCacheMap->FileObject);
1082 CcRosDeleteFileCache(FileObject, SharedCacheMap);
1083 }
1084 }
1085 KeReleaseGuardedMutex(&ViewLock);
1086 }
1087
1088 NTSTATUS
1089 NTAPI
1090 CcRosReleaseFileCache (
1091 PFILE_OBJECT FileObject)
1092 /*
1093 * FUNCTION: Called by the file system when a handle to a file object
1094 * has been closed.
1095 */
1096 {
1097 PROS_SHARED_CACHE_MAP SharedCacheMap;
1098
1099 KeAcquireGuardedMutex(&ViewLock);
1100
1101 if (FileObject->SectionObjectPointer->SharedCacheMap != NULL)
1102 {
1103 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1104 if (FileObject->PrivateCacheMap != NULL)
1105 {
1106 FileObject->PrivateCacheMap = NULL;
1107 if (SharedCacheMap->RefCount > 0)
1108 {
1109 SharedCacheMap->RefCount--;
1110 if (SharedCacheMap->RefCount == 0)
1111 {
1112 MmFreeSectionSegments(SharedCacheMap->FileObject);
1113 CcRosDeleteFileCache(FileObject, SharedCacheMap);
1114 }
1115 }
1116 }
1117 }
1118 KeReleaseGuardedMutex(&ViewLock);
1119 return STATUS_SUCCESS;
1120 }
1121
1122 NTSTATUS
1123 NTAPI
1124 CcTryToInitializeFileCache (
1125 PFILE_OBJECT FileObject)
1126 {
1127 PROS_SHARED_CACHE_MAP SharedCacheMap;
1128 NTSTATUS Status;
1129
1130 KeAcquireGuardedMutex(&ViewLock);
1131
1132 ASSERT(FileObject->SectionObjectPointer);
1133 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1134 if (SharedCacheMap == NULL)
1135 {
1136 Status = STATUS_UNSUCCESSFUL;
1137 }
1138 else
1139 {
1140 if (FileObject->PrivateCacheMap == NULL)
1141 {
1142 FileObject->PrivateCacheMap = SharedCacheMap;
1143 SharedCacheMap->RefCount++;
1144 }
1145 Status = STATUS_SUCCESS;
1146 }
1147 KeReleaseGuardedMutex(&ViewLock);
1148
1149 return Status;
1150 }
1151
1152
1153 NTSTATUS
1154 NTAPI
1155 CcRosInitializeFileCache (
1156 PFILE_OBJECT FileObject,
1157 PCC_FILE_SIZES FileSizes,
1158 PCACHE_MANAGER_CALLBACKS CallBacks,
1159 PVOID LazyWriterContext)
1160 /*
1161 * FUNCTION: Initializes a shared cache map for a file object
1162 */
1163 {
1164 PROS_SHARED_CACHE_MAP SharedCacheMap;
1165
1166 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1167 DPRINT("CcRosInitializeFileCache(FileObject 0x%p, SharedCacheMap 0x%p)\n",
1168 FileObject, SharedCacheMap);
1169
1170 KeAcquireGuardedMutex(&ViewLock);
1171 if (SharedCacheMap == NULL)
1172 {
1173 SharedCacheMap = ExAllocateFromNPagedLookasideList(&SharedCacheMapLookasideList);
1174 if (SharedCacheMap == NULL)
1175 {
1176 KeReleaseGuardedMutex(&ViewLock);
1177 return STATUS_UNSUCCESSFUL;
1178 }
1179 RtlZeroMemory(SharedCacheMap, sizeof(*SharedCacheMap));
1180 ObReferenceObjectByPointer(FileObject,
1181 FILE_ALL_ACCESS,
1182 NULL,
1183 KernelMode);
1184 SharedCacheMap->FileObject = FileObject;
1185 SharedCacheMap->Callbacks = CallBacks;
1186 SharedCacheMap->LazyWriteContext = LazyWriterContext;
1187 SharedCacheMap->SectionSize = FileSizes->AllocationSize;
1188 SharedCacheMap->FileSize = FileSizes->FileSize;
1189 KeInitializeSpinLock(&SharedCacheMap->CacheMapLock);
1190 InitializeListHead(&SharedCacheMap->CacheMapVacbListHead);
1191 FileObject->SectionObjectPointer->SharedCacheMap = SharedCacheMap;
1192 }
1193 if (FileObject->PrivateCacheMap == NULL)
1194 {
1195 FileObject->PrivateCacheMap = SharedCacheMap;
1196 SharedCacheMap->RefCount++;
1197 }
1198 KeReleaseGuardedMutex(&ViewLock);
1199
1200 return STATUS_SUCCESS;
1201 }
1202
1203 /*
1204 * @implemented
1205 */
1206 PFILE_OBJECT
1207 NTAPI
1208 CcGetFileObjectFromSectionPtrs (
1209 IN PSECTION_OBJECT_POINTERS SectionObjectPointers)
1210 {
1211 PROS_SHARED_CACHE_MAP SharedCacheMap;
1212 if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
1213 {
1214 SharedCacheMap = SectionObjectPointers->SharedCacheMap;
1215 ASSERT(SharedCacheMap);
1216 return SharedCacheMap->FileObject;
1217 }
1218 return NULL;
1219 }
1220
1221 VOID
1222 INIT_FUNCTION
1223 NTAPI
1224 CcInitView (
1225 VOID)
1226 {
1227 DPRINT("CcInitView()\n");
1228
1229 InitializeListHead(&DirtyVacbListHead);
1230 InitializeListHead(&VacbLruListHead);
1231 KeInitializeGuardedMutex(&ViewLock);
1232 ExInitializeNPagedLookasideList(&iBcbLookasideList,
1233 NULL,
1234 NULL,
1235 0,
1236 sizeof(INTERNAL_BCB),
1237 TAG_BCB,
1238 20);
1239 ExInitializeNPagedLookasideList(&SharedCacheMapLookasideList,
1240 NULL,
1241 NULL,
1242 0,
1243 sizeof(ROS_SHARED_CACHE_MAP),
1244 TAG_SHARED_CACHE_MAP,
1245 20);
1246 ExInitializeNPagedLookasideList(&VacbLookasideList,
1247 NULL,
1248 NULL,
1249 0,
1250 sizeof(ROS_VACB),
1251 TAG_VACB,
1252 20);
1253
1254 MmInitializeMemoryConsumer(MC_CACHE, CcRosTrimCache);
1255
1256 CcInitCacheZeroPage();
1257 }
1258
1259 /* EOF */