[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / cc / view.c
index 1e53144..70fb7ce 100644 (file)
@@ -166,12 +166,10 @@ CcRosFlushDirtyPages (
     PROS_VACB current;
     BOOLEAN Locked;
     NTSTATUS Status;
-    LARGE_INTEGER ZeroTimeout;
 
     DPRINT("CcRosFlushDirtyPages(Target %lu)\n", Target);
 
     (*Count) = 0;
-    ZeroTimeout.QuadPart = 0;
 
     KeEnterCriticalRegion();
     KeAcquireGuardedMutex(&ViewLock);
@@ -199,12 +197,8 @@ CcRosFlushDirtyPages (
             continue;
         }
 
-        Status = KeWaitForSingleObject(&current->Mutex,
-                                       Executive,
-                                       KernelMode,
-                                       FALSE,
-                                       Wait ? NULL : &ZeroTimeout);
-        if (Status != STATUS_SUCCESS)
+        Locked = ExAcquireResourceExclusiveLite(&current->Lock, Wait);
+        if (!Locked)
         {
             current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
                 current->SharedCacheMap->LazyWriteContext);
@@ -217,7 +211,7 @@ CcRosFlushDirtyPages (
         /* One reference is added above */
         if (current->ReferenceCount > 2)
         {
-            KeReleaseMutex(&current->Mutex, FALSE);
+            ExReleaseResourceLite(&current->Lock);
             current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
                 current->SharedCacheMap->LazyWriteContext);
             CcRosVacbDecRefCount(current);
@@ -228,14 +222,15 @@ CcRosFlushDirtyPages (
 
         Status = CcRosFlushVacb(current);
 
-        KeReleaseMutex(&current->Mutex, FALSE);
+        ExReleaseResourceLite(&current->Lock);
         current->SharedCacheMap->Callbacks->ReleaseFromLazyWrite(
             current->SharedCacheMap->LazyWriteContext);
 
         KeAcquireGuardedMutex(&ViewLock);
         CcRosVacbDecRefCount(current);
 
-        if (!NT_SUCCESS(Status) &&  (Status != STATUS_END_OF_FILE))
+        if (!NT_SUCCESS(Status) && (Status != STATUS_END_OF_FILE) &&
+            (Status != STATUS_MEDIA_WRITE_PROTECTED))
         {
             DPRINT1("CC: Failed to flush VACB.\n");
         }
@@ -424,7 +419,7 @@ CcRosReleaseVacb (
 
     KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
     KeReleaseGuardedMutex(&ViewLock);
-    KeReleaseMutex(&Vacb->Mutex, FALSE);
+    ExReleaseResourceLite(&Vacb->Lock);
 
     return STATUS_SUCCESS;
 }
@@ -461,11 +456,7 @@ CcRosLookupVacb (
             CcRosVacbIncRefCount(current);
             KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
             KeReleaseGuardedMutex(&ViewLock);
-            KeWaitForSingleObject(&current->Mutex,
-                                  Executive,
-                                  KernelMode,
-                                  FALSE,
-                                  NULL);
+            ExAcquireResourceExclusiveLite(&current->Lock, TRUE);
             return current;
         }
         if (current->FileOffset.QuadPart > FileOffset)
@@ -520,7 +511,7 @@ CcRosMarkDirtyVacb (
 
     KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
     KeReleaseGuardedMutex(&ViewLock);
-    KeReleaseMutex(&Vacb->Mutex, FALSE);
+    ExReleaseResourceLite(&Vacb->Lock);
 
     return STATUS_SUCCESS;
 }
@@ -573,44 +564,65 @@ CcRosUnmapVacb (
 
     KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldIrql);
     KeReleaseGuardedMutex(&ViewLock);
-    KeReleaseMutex(&Vacb->Mutex, FALSE);
+    ExReleaseResourceLite(&Vacb->Lock);
 
     return STATUS_SUCCESS;
 }
 
 static
-VOID
-CcRosMapVacb(PVOID BaseAddress,
-                SIZE_T Length,
-                ULONG Consumer,
-                ULONG Protection)
+NTSTATUS
+CcRosMapVacb(
+    PROS_VACB Vacb)
 {
     ULONG i;
     NTSTATUS Status;
+    ULONG_PTR NumberOfPages;
+
+    /* Create a memory area. */
+    MmLockAddressSpace(MmGetKernelAddressSpace());
+    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
+                                0, // nothing checks for VACB mareas, so set to 0
+                                &Vacb->BaseAddress,
+                                VACB_MAPPING_GRANULARITY,
+                                PAGE_READWRITE,
+                                (PMEMORY_AREA*)&Vacb->MemoryArea,
+                                0,
+                                PAGE_SIZE);
+    MmUnlockAddressSpace(MmGetKernelAddressSpace());
+    if (!NT_SUCCESS(Status))
+    {
+        KeBugCheck(CACHE_MANAGER);
+    }
 
-    ASSERT(((ULONG_PTR)BaseAddress % PAGE_SIZE) == 0);
+    ASSERT(((ULONG_PTR)Vacb->BaseAddress % PAGE_SIZE) == 0);
+    ASSERT((ULONG_PTR)Vacb->BaseAddress > (ULONG_PTR)MmSystemRangeStart);
 
-    for (i = 0; i < PAGE_ROUND_UP(Length) / PAGE_SIZE; i++)
+    /* Create a virtual mapping for this memory area */
+    NumberOfPages = BYTES_TO_PAGES(VACB_MAPPING_GRANULARITY);
+    for (i = 0; i < NumberOfPages; i++)
     {
-        PFN_NUMBER Page;
+        PFN_NUMBER PageFrameNumber;
 
-        Status = MmRequestPageMemoryConsumer(Consumer, TRUE, &Page);
-        if (!NT_SUCCESS(Status))
+        Status = MmRequestPageMemoryConsumer(MC_CACHE, TRUE, &PageFrameNumber);
+        if (PageFrameNumber == 0)
         {
             DPRINT1("Unable to allocate page\n");
             KeBugCheck(MEMORY_MANAGEMENT);
         }
-        Status = MmCreateVirtualMapping (NULL,
-                                         (PVOID)((ULONG_PTR)BaseAddress + (i * PAGE_SIZE)),
-                                         Protection,
-                                         &Page,
-                                         1);
+
+        Status = MmCreateVirtualMapping(NULL,
+                                        (PVOID)((ULONG_PTR)Vacb->BaseAddress + (i * PAGE_SIZE)),
+                                        PAGE_READWRITE,
+                                        &PageFrameNumber,
+                                        1);
         if (!NT_SUCCESS(Status))
         {
             DPRINT1("Unable to create virtual mapping\n");
             KeBugCheck(MEMORY_MANAGEMENT);
         }
     }
+
+    return STATUS_SUCCESS;
 }
 
 static
@@ -637,6 +649,7 @@ CcRosCreateVacb (
     }
 
     current = ExAllocateFromNPagedLookasideList(&VacbLookasideList);
+    current->BaseAddress = NULL;
     current->Valid = FALSE;
     current->Dirty = FALSE;
     current->PageOut = FALSE;
@@ -652,12 +665,8 @@ CcRosCreateVacb (
     current->DirtyVacbListEntry.Flink = NULL;
     current->DirtyVacbListEntry.Blink = NULL;
     current->ReferenceCount = 1;
-    KeInitializeMutex(&current->Mutex, 0);
-    KeWaitForSingleObject(&current->Mutex,
-                          Executive,
-                          KernelMode,
-                          FALSE,
-                          NULL);
+    ExInitializeResourceLite(&current->Lock);
+    ExAcquireResourceExclusiveLite(&current->Lock, TRUE);
     KeAcquireGuardedMutex(&ViewLock);
 
     *Vacb = current;
@@ -689,15 +698,11 @@ CcRosCreateVacb (
                         current);
             }
 #endif
-            KeReleaseMutex(&(*Vacb)->Mutex, FALSE);
+            ExReleaseResourceLite(&(*Vacb)->Lock);
             KeReleaseGuardedMutex(&ViewLock);
             ExFreeToNPagedLookasideList(&VacbLookasideList, *Vacb);
             *Vacb = current;
-            KeWaitForSingleObject(&current->Mutex,
-                                  Executive,
-                                  KernelMode,
-                                  FALSE,
-                                  NULL);
+            ExAcquireResourceExclusiveLite(&current->Lock, TRUE);
             return STATUS_SUCCESS;
         }
         if (current->FileOffset.QuadPart < FileOffset)
@@ -724,24 +729,6 @@ CcRosCreateVacb (
     InsertTailList(&VacbLruListHead, &current->VacbLruListEntry);
     KeReleaseGuardedMutex(&ViewLock);
 
-    MmLockAddressSpace(MmGetKernelAddressSpace());
-    current->BaseAddress = NULL;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                0, // nothing checks for VACB mareas, so set to 0
-                                &current->BaseAddress,
-                                VACB_MAPPING_GRANULARITY,
-                                PAGE_READWRITE,
-                                (PMEMORY_AREA*)&current->MemoryArea,
-                                FALSE,
-                                0,
-                                PAGE_SIZE);
-    MmUnlockAddressSpace(MmGetKernelAddressSpace());
-    if (!NT_SUCCESS(Status))
-    {
-        KeBugCheck(CACHE_MANAGER);
-    }
-
-    /* Create a virtual mapping for this memory area */
     MI_SET_USAGE(MI_USAGE_CACHE);
 #if MI_TRACE_PFNS
     if ((SharedCacheMap->FileObject) && (SharedCacheMap->FileObject->FileName.Buffer))
@@ -754,10 +741,9 @@ CcRosCreateVacb (
     }
 #endif
 
-    CcRosMapVacb(current->BaseAddress, VACB_MAPPING_GRANULARITY,
-                    MC_CACHE, PAGE_READWRITE);
+    Status = CcRosMapVacb(current);
 
-    return STATUS_SUCCESS;
+    return Status;
 }
 
 NTSTATUS
@@ -882,6 +868,7 @@ CcRosInternalFreeVacb (
                      CcFreeCachePage,
                      NULL);
     MmUnlockAddressSpace(MmGetKernelAddressSpace());
+    ExDeleteResourceLite(&Vacb->Lock);
 
     ExFreeToNPagedLookasideList(&VacbLookasideList, Vacb);
     return STATUS_SUCCESS;
@@ -905,6 +892,9 @@ CcFlushCache (
     NTSTATUS Status;
     KIRQL oldIrql;
 
+    CCTRACE(CC_API_DEBUG, "SectionObjectPointers=%p FileOffset=%p Length=%lu\n",
+        SectionObjectPointers, FileOffset, Length);
+
     DPRINT("CcFlushCache(SectionObjectPointers 0x%p, FileOffset 0x%p, Length %lu, IoStatus 0x%p)\n",
            SectionObjectPointers, FileOffset, Length, IoStatus);
 
@@ -942,7 +932,7 @@ CcFlushCache (
                         IoStatus->Status = Status;
                     }
                 }
-                KeReleaseMutex(&current->Mutex, FALSE);
+                ExReleaseResourceLite(&current->Lock);
 
                 KeAcquireGuardedMutex(&ViewLock);
                 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldIrql);
@@ -1170,7 +1160,7 @@ CcRosInitializeFileCache (
         if (SharedCacheMap == NULL)
         {
             KeReleaseGuardedMutex(&ViewLock);
-            return STATUS_UNSUCCESSFUL;
+            return STATUS_INSUFFICIENT_RESOURCES;
         }
         RtlZeroMemory(SharedCacheMap, sizeof(*SharedCacheMap));
         ObReferenceObjectByPointer(FileObject,
@@ -1205,6 +1195,9 @@ CcGetFileObjectFromSectionPtrs (
     IN PSECTION_OBJECT_POINTERS SectionObjectPointers)
 {
     PROS_SHARED_CACHE_MAP SharedCacheMap;
+
+    CCTRACE(CC_API_DEBUG, "SectionObjectPointers=%p\n", SectionObjectPointers);
+
     if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
     {
         SharedCacheMap = SectionObjectPointers->SharedCacheMap;