started moving tags to a private internal header
[reactos.git] / reactos / ntoskrnl / cc / view.c
index 1f0d979..35266a1 100644 (file)
@@ -1,72 +1,47 @@
-/*
- *  ReactOS kernel
- *  Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-/* $Id: view.c,v 1.65 2003/07/10 06:27:13 royce Exp $
+/* $Id$
  *
+ * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS kernel
  * FILE:            ntoskrnl/cc/view.c
  * PURPOSE:         Cache manager
- * PROGRAMMER:      David Welch (welch@mcmail.com)
- * PORTABILITY:     Checked
- * UPDATE HISTORY:
- *                  Created 22/05/98
+ *
+ * PROGRAMMERS:     David Welch (welch@mcmail.com)
  */
 
 /* NOTES **********************************************************************
  *
- * This is not the NT implementation of a file cache nor anything much like 
- * it. 
+ * This is not the NT implementation of a file cache nor anything much like
+ * it.
  *
- * The general procedure for a filesystem to implement a read or write 
+ * The general procedure for a filesystem to implement a read or write
  * dispatch routine is as follows
- * 
+ *
  * (1) If caching for the FCB hasn't been initiated then so do by calling
  * CcInitializeFileCache.
- * 
+ *
  * (2) For each 4k region which is being read or written obtain a cache page
- * by calling CcRequestCachePage. 
+ * by calling CcRequestCachePage.
  *
- * (3) If either the page is being read or not completely written, and it is 
+ * (3) If either the page is being read or not completely written, and it is
  * not up to date then read its data from the underlying medium. If the read
- * fails then call CcReleaseCachePage with VALID as FALSE and return a error.  
- * 
+ * fails then call CcReleaseCachePage with VALID as FALSE and return a error.
+ *
  * (4) Copy the data into or out of the page as necessary.
- * 
+ *
  * (5) Release the cache page
  */
 /* INCLUDES ******************************************************************/
 
-#include <ddk/ntddk.h>
-#include <ddk/ntifs.h>
-#include <internal/mm.h>
-#include <internal/cc.h>
-#include <internal/pool.h>
-#include <ntos/minmax.h>
-
+#include <ntoskrnl.h>
 #define NDEBUG
 #include <internal/debug.h>
 
 /* GLOBALS *******************************************************************/
 
 /*
- * If CACHE_BITMAP is defined, the cache manager uses one large memory region 
- * within the kernel address space and allocate/deallocate space from this block 
- * over a bitmap. If CACHE_BITMAP is used, the size of the mdl mapping region 
+ * If CACHE_BITMAP is defined, the cache manager uses one large memory region
+ * within the kernel address space and allocate/deallocate space from this block
+ * over a bitmap. If CACHE_BITMAP is used, the size of the mdl mapping region
  * must be reduced (ntoskrnl\mm\mdl.c, MI_MDLMAPPING_REGION_SIZE).
  */
 //#define CACHE_BITMAP
 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
 #define ROUND_DOWN(N, S) (((N) % (S)) ? ROUND_UP(N, S) - S : N)
 
-#define TAG_CSEG  TAG('C', 'S', 'E', 'G')
-#define TAG_BCB   TAG('B', 'C', 'B', ' ')
-#define TAG_IBCB  TAG('i', 'B', 'C', 'B')
-
 static LIST_ENTRY DirtySegmentListHead;
 static LIST_ENTRY CacheSegmentListHead;
 static LIST_ENTRY CacheSegmentLRUListHead;
@@ -105,14 +76,21 @@ static HANDLE LazyCloseThreadHandle;
 static CLIENT_ID LazyCloseThreadId;
 static volatile BOOLEAN LazyCloseThreadShouldTerminate;
 
-void * alloca(size_t size);
+#if defined(__GNUC__)
+/* void * alloca(size_t size); */
+#elif defined(_MSC_VER)
+void* _alloca(size_t size);
+#else
+#error Unknown compiler for alloca intrinsic stack allocation "function"
+#endif
+
 
 NTSTATUS
 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg);
 
 /* FUNCTIONS *****************************************************************/
 
-NTSTATUS STATIC
+NTSTATUS
 CcRosFlushCacheSegment(PCACHE_SEGMENT CacheSegment)
 {
   NTSTATUS Status;
@@ -132,9 +110,6 @@ CcRosFlushCacheSegment(PCACHE_SEGMENT CacheSegment)
   return(Status);
 }
 
-VOID CcRosRemoveUnusedFiles(VOID);
-
-
 NTSTATUS
 CcRosFlushDirtyPages(ULONG Target, PULONG Count)
 {
@@ -169,7 +144,7 @@ CcRosFlushDirtyPages(ULONG Target, PULONG Count)
   }
 
   NewTarget = WriteCount[0];
-  
+
   Target = max(NewTarget, Target);
 
   current_entry = DirtySegmentListHead.Flink;
@@ -179,7 +154,7 @@ CcRosFlushDirtyPages(ULONG Target, PULONG Count)
   }
   while (current_entry != &DirtySegmentListHead && Target > 0)
     {
-      current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, 
+      current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
                                  DirtySegmentListEntry);
       current_entry = current_entry->Flink;
       Locked = ExTryToAcquireFastMutex(&current->Lock);
@@ -187,7 +162,7 @@ CcRosFlushDirtyPages(ULONG Target, PULONG Count)
        {
          continue;
        }
-      assert(current->Dirty);
+      ASSERT(current->Dirty);
       if (current->ReferenceCount > 1)
        {
          ExReleaseFastMutex(&current->Lock);
@@ -195,7 +170,7 @@ CcRosFlushDirtyPages(ULONG Target, PULONG Count)
        }
       ExReleaseFastMutex(&ViewLock);
       PagesPerSegment = current->Bcb->CacheSegmentSize / PAGE_SIZE;
-      Status = CcRosFlushCacheSegment(current);      
+      Status = CcRosFlushCacheSegment(current);
       ExReleaseFastMutex(&current->Lock);
       if (!NT_SUCCESS(Status) &&  (Status != STATUS_END_OF_FILE))
       {
@@ -204,7 +179,7 @@ CcRosFlushDirtyPages(ULONG Target, PULONG Count)
       else
       {
          (*Count) += PagesPerSegment;
-         Target -= PagesPerSegment;     
+         Target -= PagesPerSegment;
       }
       ExAcquireFastMutex(&ViewLock);
       current_entry = DirtySegmentListHead.Flink;
@@ -226,12 +201,12 @@ CcRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
  * ARGUMENTS:
  *       Target - The number of pages to be freed.
  *       Priority - The priority of free (currently unused).
- *       NrFreed - Points to a variable where the number of pages 
+ *       NrFreed - Points to a variable where the number of pages
  *                 actually freed is returned.
  */
 {
   PLIST_ENTRY current_entry;
-  PCACHE_SEGMENT current;
+  PCACHE_SEGMENT current, last = NULL;
   ULONG PagesPerSegment;
   ULONG PagesFreed;
   KIRQL oldIrql;
@@ -240,17 +215,17 @@ CcRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
   DPRINT("CcRosTrimCache(Target %d)\n", Target);
 
   *NrFreed = 0;
-  
+
   InitializeListHead(&FreeList);
-  
+
   ExAcquireFastMutex(&ViewLock);
   current_entry = CacheSegmentLRUListHead.Flink;
   while (current_entry != &CacheSegmentLRUListHead && Target > 0)
     {
-      current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, 
+      current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
                                  CacheSegmentLRUListEntry);
       current_entry = current_entry->Flink;
-      
+
       KeAcquireSpinLock(&current->Bcb->BcbLock, &oldIrql);
       if (current->ReferenceCount == 0)
       {
@@ -266,7 +241,35 @@ CcRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
       }
       else
       {
-         KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
+         if (last != current && current->MappedCount > 0 && !current->Dirty && !current->PageOut)
+          {
+            ULONG i;
+            NTSTATUS Status;
+
+             current->ReferenceCount++;
+            last = current;
+            current->PageOut = TRUE;
+             KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
+            ExReleaseFastMutex(&ViewLock);
+            for (i = 0; i < current->Bcb->CacheSegmentSize / PAGE_SIZE; i++)
+              {
+                PFN_TYPE Page;
+                Page = MmGetPhysicalAddress((char*)current->BaseAddress + i * PAGE_SIZE).QuadPart >> PAGE_SHIFT;
+                 Status = MmPageOutPhysicalAddress(Page);
+                if (!NT_SUCCESS(Status))
+                  {
+                    break;
+                  }
+              }
+             ExAcquireFastMutex(&ViewLock);
+             KeAcquireSpinLock(&current->Bcb->BcbLock, &oldIrql);
+            current->ReferenceCount--;
+            current->PageOut = FALSE;
+             KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
+             current_entry = &current->CacheSegmentLRUListEntry;
+            continue;
+          }
+        KeReleaseSpinLock(&current->Bcb->BcbLock, oldIrql);
       }
   }
   ExReleaseFastMutex(&ViewLock);
@@ -274,7 +277,7 @@ CcRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
   while (!IsListEmpty(&FreeList))
   {
      current_entry = RemoveHeadList(&FreeList);
-     current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, 
+     current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
                                 BcbSegmentListEntry);
      CcRosInternalFreeCacheSegment(current);
   }
@@ -283,7 +286,7 @@ CcRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
   return(STATUS_SUCCESS);
 }
 
-NTSTATUS 
+NTSTATUS
 CcRosReleaseCacheSegment(PBCB Bcb,
                         PCACHE_SEGMENT CacheSeg,
                         BOOLEAN Valid,
@@ -293,7 +296,7 @@ CcRosReleaseCacheSegment(PBCB Bcb,
   BOOLEAN WasDirty = CacheSeg->Dirty;
   KIRQL oldIrql;
 
-  assert(Bcb);
+  ASSERT(Bcb);
 
   DPRINT("CcReleaseCacheSegment(Bcb %x, CacheSeg %x, Valid %d)\n",
         Bcb, CacheSeg, Valid);
@@ -327,18 +330,18 @@ CcRosReleaseCacheSegment(PBCB Bcb,
   KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
   ExReleaseFastMutex(&ViewLock);
   ExReleaseFastMutex(&CacheSeg->Lock);
-  
+
   return(STATUS_SUCCESS);
 }
 
-PCACHE_SEGMENT 
+PCACHE_SEGMENT
 CcRosLookupCacheSegment(PBCB Bcb, ULONG FileOffset)
 {
   PLIST_ENTRY current_entry;
   PCACHE_SEGMENT current;
   KIRQL oldIrql;
 
-  assert(Bcb);
+  ASSERT(Bcb);
 
   DPRINT("CcRosLookupCacheSegment(Bcb %x, FileOffset %d)\n", Bcb, FileOffset);
 
@@ -346,13 +349,14 @@ CcRosLookupCacheSegment(PBCB Bcb, ULONG FileOffset)
   current_entry = Bcb->BcbSegmentListHead.Flink;
   while (current_entry != &Bcb->BcbSegmentListHead)
     {
-      current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, 
+      current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
                                  BcbSegmentListEntry);
       if (current->FileOffset <= FileOffset &&
          (current->FileOffset + Bcb->CacheSegmentSize) > FileOffset)
        {
          current->ReferenceCount++;
          KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
+          ExAcquireFastMutex(&current->Lock);
          return(current);
        }
       current_entry = current_entry->Flink;
@@ -367,16 +371,15 @@ CcRosMarkDirtyCacheSegment(PBCB Bcb, ULONG FileOffset)
   PCACHE_SEGMENT CacheSeg;
   KIRQL oldIrql;
 
-  assert(Bcb);
+  ASSERT(Bcb);
 
   DPRINT("CcRosMarkDirtyCacheSegment(Bcb %x, FileOffset %d)\n", Bcb, FileOffset);
 
   CacheSeg = CcRosLookupCacheSegment(Bcb, FileOffset);
   if (CacheSeg == NULL)
     {
-      KeBugCheck(0);
+      KEBUGCHECKCC;
     }
-  ExAcquireFastMutex(&CacheSeg->Lock);
   if (!CacheSeg->Dirty)
     {
       ExAcquireFastMutex(&ViewLock);
@@ -405,7 +408,7 @@ CcRosUnmapCacheSegment(PBCB Bcb, ULONG FileOffset, BOOLEAN NowDirty)
   BOOLEAN WasDirty;
   KIRQL oldIrql;
 
-  assert(Bcb);
+  ASSERT(Bcb);
 
   DPRINT("CcRosUnmapCacheSegment(Bcb %x, FileOffset %d, NowDirty %d)\n",
           Bcb, FileOffset, NowDirty);
@@ -415,7 +418,6 @@ CcRosUnmapCacheSegment(PBCB Bcb, ULONG FileOffset, BOOLEAN NowDirty)
     {
       return(STATUS_UNSUCCESSFUL);
     }
-  ExAcquireFastMutex(&CacheSeg->Lock);
 
   WasDirty = CacheSeg->Dirty;
   CacheSeg->Dirty = CacheSeg->Dirty || NowDirty;
@@ -449,8 +451,7 @@ CcRosUnmapCacheSegment(PBCB Bcb, ULONG FileOffset, BOOLEAN NowDirty)
 NTSTATUS STATIC
 CcRosCreateCacheSegment(PBCB Bcb,
                        ULONG FileOffset,
-                       PCACHE_SEGMENT* CacheSeg,
-                       BOOLEAN Lock)
+                       PCACHE_SEGMENT* CacheSeg)
 {
   ULONG i;
   PCACHE_SEGMENT current;
@@ -458,14 +459,18 @@ CcRosCreateCacheSegment(PBCB Bcb,
   PLIST_ENTRY current_entry;
   NTSTATUS Status;
   KIRQL oldIrql;
+  PPFN_TYPE Pfn;
 #ifdef CACHE_BITMAP
   ULONG StartingOffset;
+#else
 #endif
+  PHYSICAL_ADDRESS BoundaryAddressMultiple;
 
-  assert(Bcb);
+  ASSERT(Bcb);
 
   DPRINT("CcRosCreateCacheSegment()\n");
 
+  BoundaryAddressMultiple.QuadPart = 0;
   if (FileOffset >= Bcb->FileSize.u.LowPart)
   {
      CacheSeg = NULL;
@@ -475,6 +480,7 @@ CcRosCreateCacheSegment(PBCB Bcb,
   current = ExAllocateFromNPagedLookasideList(&CacheSegLookasideList);
   current->Valid = FALSE;
   current->Dirty = FALSE;
+  current->PageOut = FALSE;
   current->FileOffset = ROUND_DOWN(FileOffset, Bcb->CacheSegmentSize);
   current->Bcb = Bcb;
   current->MappedCount = 0;
@@ -489,14 +495,14 @@ CcRosCreateCacheSegment(PBCB Bcb,
   /* There is window between the call to CcRosLookupCacheSegment
    * and CcRosCreateCacheSegment. We must check if a segment on
    * the fileoffset exist. If there exist a segment, we release
-   * our new created segment and return the existing one. 
+   * our new created segment and return the existing one.
    */
   KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql);
   current_entry = Bcb->BcbSegmentListHead.Flink;
   previous = NULL;
   while (current_entry != &Bcb->BcbSegmentListHead)
   {
-     current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, 
+     current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT,
                                 BcbSegmentListEntry);
      if (current->FileOffset <= FileOffset &&
        (current->FileOffset + Bcb->CacheSegmentSize) > FileOffset)
@@ -507,10 +513,7 @@ CcRosCreateCacheSegment(PBCB Bcb,
        ExReleaseFastMutex(&ViewLock);
        ExFreeToNPagedLookasideList(&CacheSegLookasideList, *CacheSeg);
        *CacheSeg = current;
-       if (Lock)
-       {
-          ExAcquireFastMutex(&current->Lock);
-       }
+        ExAcquireFastMutex(&current->Lock);
        return STATUS_SUCCESS;
      }
      if (current->FileOffset < FileOffset)
@@ -547,18 +550,18 @@ CcRosCreateCacheSegment(PBCB Bcb,
   KeAcquireSpinLock(&CiCacheSegMappingRegionLock, &oldIrql);
 
   StartingOffset = RtlFindClearBitsAndSet(&CiCacheSegMappingRegionAllocMap, Bcb->CacheSegmentSize / PAGE_SIZE, CiCacheSegMappingRegionHint);
-  
+
   if (StartingOffset == 0xffffffff)
   {
      DPRINT1("Out of CacheSeg mapping space\n");
-     KeBugCheck(0);
+     KEBUGCHECKCC;
   }
 
   current->BaseAddress = CiCacheSegMappingRegionBase + StartingOffset * PAGE_SIZE;
 
   if (CiCacheSegMappingRegionHint == StartingOffset)
   {
-     CiCacheSegMappingRegionHint += Bcb->CacheSegmentSize / PAGE_SIZE; 
+     CiCacheSegMappingRegionHint += Bcb->CacheSegmentSize / PAGE_SIZE;
   }
 
   KeReleaseSpinLock(&CiCacheSegMappingRegionLock, oldIrql);
@@ -573,38 +576,32 @@ CcRosCreateCacheSegment(PBCB Bcb,
                              PAGE_READWRITE,
                              (PMEMORY_AREA*)&current->MemoryArea,
                              FALSE,
-                             FALSE);
+                             FALSE,
+                             BoundaryAddressMultiple);
   MmUnlockAddressSpace(MmGetKernelAddressSpace());
   if (!NT_SUCCESS(Status))
   {
-     KeBugCheck(0);
+     KEBUGCHECKCC;
   }
 #endif
+  Pfn = alloca(sizeof(PFN_TYPE) * (Bcb->CacheSegmentSize / PAGE_SIZE));
   for (i = 0; i < (Bcb->CacheSegmentSize / PAGE_SIZE); i++)
   {
-     PHYSICAL_ADDRESS Page;
-      
-     Status = MmRequestPageMemoryConsumer(MC_CACHE, TRUE, &Page);
-     if (!NT_SUCCESS(Status))
-     {
-       KeBugCheck(0);
-     }
-      
-     Status = MmCreateVirtualMapping(NULL,
-                                    current->BaseAddress + (i * PAGE_SIZE),
-                                    PAGE_READWRITE,
-                                    Page,
-                                    TRUE);
+     Status = MmRequestPageMemoryConsumer(MC_CACHE, TRUE, &Pfn[i]);
      if (!NT_SUCCESS(Status))
      {
-       KeBugCheck(0);
+       KEBUGCHECKCC;
      }
   }
-  if (!Lock)
+  Status = MmCreateVirtualMapping(NULL,
+                                 current->BaseAddress,
+                                 PAGE_READWRITE,
+                                 Pfn,
+                                 Bcb->CacheSegmentSize / PAGE_SIZE);
+  if (!NT_SUCCESS(Status))
   {
-     ExReleaseFastMutex(&current->Lock);
+    KEBUGCHECKCC;
   }
-
   return(STATUS_SUCCESS);
 }
 
@@ -619,14 +616,21 @@ CcRosGetCacheSegmentChain(PBCB Bcb,
   PCACHE_SEGMENT* CacheSegList;
   PCACHE_SEGMENT Previous = NULL;
 
-  assert(Bcb);
+  ASSERT(Bcb);
 
   DPRINT("CcRosGetCacheSegmentChain()\n");
 
   Length = ROUND_UP(Length, Bcb->CacheSegmentSize);
 
-  CacheSegList = alloca(sizeof(PCACHE_SEGMENT) * 
+#if defined(__GNUC__)
+  CacheSegList = alloca(sizeof(PCACHE_SEGMENT) *
                        (Length / Bcb->CacheSegmentSize));
+#elif defined(_MSC_VER)
+  CacheSegList = _alloca(sizeof(PCACHE_SEGMENT) *
+                       (Length / Bcb->CacheSegmentSize));
+#else
+#error Unknown compiler for alloca intrinsic stack allocation "function"
+#endif
 
   /*
    * Look for a cache segment already mapping the same data.
@@ -641,14 +645,13 @@ CcRosGetCacheSegmentChain(PBCB Bcb,
        }
       else
        {
-         CcRosCreateCacheSegment(Bcb, CurrentOffset, &current, FALSE);
+         CcRosCreateCacheSegment(Bcb, CurrentOffset, &current);
          CacheSegList[i] = current;
        }
     }
 
   for (i = 0; i < (Length / Bcb->CacheSegmentSize); i++)
     {
-      ExAcquireFastMutex(&CacheSegList[i]->Lock);
       if (i == 0)
        {
          *CacheSeg = CacheSegList[i];
@@ -661,7 +664,7 @@ CcRosGetCacheSegmentChain(PBCB Bcb,
        }
     }
   Previous->NextInChain = NULL;
-  
+
   return(STATUS_SUCCESS);
 }
 
@@ -676,7 +679,7 @@ CcRosGetCacheSegment(PBCB Bcb,
    PCACHE_SEGMENT current;
    NTSTATUS Status;
 
-   assert(Bcb);
+   ASSERT(Bcb);
 
    DPRINT("CcRosGetCacheSegment()\n");
 
@@ -684,16 +687,12 @@ CcRosGetCacheSegment(PBCB Bcb,
     * Look for a cache segment already mapping the same data.
     */
    current = CcRosLookupCacheSegment(Bcb, FileOffset);
-   if (current != NULL)
-   {
-      ExAcquireFastMutex(&current->Lock);
-   }
-   else
+   if (current == NULL)
    {
      /*
       * Otherwise create a new segment.
       */
-      Status = CcRosCreateCacheSegment(Bcb, FileOffset, &current, TRUE);
+      Status = CcRosCreateCacheSegment(Bcb, FileOffset, &current);
       if (!NT_SUCCESS(Status))
       {
        return Status;
@@ -710,7 +709,7 @@ CcRosGetCacheSegment(PBCB Bcb,
    return(STATUS_SUCCESS);
 }
 
-NTSTATUS STDCALL 
+NTSTATUS STDCALL
 CcRosRequestCacheSegment(PBCB Bcb,
                      ULONG FileOffset,
                      PVOID* BaseAddress,
@@ -722,13 +721,13 @@ CcRosRequestCacheSegment(PBCB Bcb,
 {
   ULONG BaseOffset;
 
-  assert(Bcb);
+  ASSERT(Bcb);
 
   if ((FileOffset % Bcb->CacheSegmentSize) != 0)
     {
       CPRINT("Bad fileoffset %x should be multiple of %x",
         FileOffset, Bcb->CacheSegmentSize);
-      KeBugCheck(0);
+      KEBUGCHECKCC;
     }
 
   return(CcRosGetCacheSegment(Bcb,
@@ -740,18 +739,18 @@ CcRosRequestCacheSegment(PBCB Bcb,
 }
 #ifdef CACHE_BITMAP
 #else
-STATIC VOID 
-CcFreeCachePage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address, 
-               PHYSICAL_ADDRESS PhysAddr, SWAPENTRY SwapEntry, BOOLEAN Dirty)
+STATIC VOID
+CcFreeCachePage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
+               PFN_TYPE Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
 {
-  assert(SwapEntry == 0);
-  if (PhysAddr.QuadPart != 0)
+  ASSERT(SwapEntry == 0);
+  if (Page != 0)
     {
-      MmReleasePageMemoryConsumer(MC_CACHE, PhysAddr);
+      MmReleasePageMemoryConsumer(MC_CACHE, Page);
     }
 }
 #endif
-NTSTATUS 
+NTSTATUS
 CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg)
 /*
  * FUNCTION: Releases a cache segment associated with a BCB
@@ -761,7 +760,7 @@ CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg)
   ULONG i;
   ULONG RegionSize;
   ULONG Base;
-  PHYSICAL_ADDRESS PhysicalAddr;
+  PFN_TYPE Page;
   KIRQL oldIrql;
 #endif
   DPRINT("Freeing cache segment %x\n", CacheSeg);
@@ -771,18 +770,18 @@ CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg)
   /* Unmap all the pages. */
   for (i = 0; i < RegionSize; i++)
     {
-      MmDeleteVirtualMapping(NULL, 
+      MmDeleteVirtualMapping(NULL,
                             CacheSeg->BaseAddress + (i * PAGE_SIZE),
                             FALSE,
                             NULL,
-                            &PhysicalAddr);
-      MmReleasePageMemoryConsumer(MC_CACHE, PhysicalAddr);
+                            &Page);
+      MmReleasePageMemoryConsumer(MC_CACHE, Page);
     }
 
   KeAcquireSpinLock(&CiCacheSegMappingRegionLock, &oldIrql);
   /* Deallocate all the pages used. */
   Base = (ULONG)(CacheSeg->BaseAddress - CiCacheSegMappingRegionBase) / PAGE_SIZE;
-  
+
   RtlClearBits(&CiCacheSegMappingRegionAllocMap, Base, RegionSize);
 
   CiCacheSegMappingRegionHint = min (CiCacheSegMappingRegionHint, Base);
@@ -791,8 +790,7 @@ CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg)
 #else
   MmLockAddressSpace(MmGetKernelAddressSpace());
   MmFreeMemoryArea(MmGetKernelAddressSpace(),
-                  CacheSeg->BaseAddress,
-                  CacheSeg->Bcb->CacheSegmentSize,
+                  CacheSeg->MemoryArea,
                   CcFreeCachePage,
                   NULL);
   MmUnlockAddressSpace(MmGetKernelAddressSpace());
@@ -807,7 +805,7 @@ CcRosFreeCacheSegment(PBCB Bcb, PCACHE_SEGMENT CacheSeg)
   NTSTATUS Status;
   KIRQL oldIrql;
 
-  assert(Bcb);
+  ASSERT(Bcb);
 
   DPRINT("CcRosFreeCacheSegment(Bcb %x, CacheSeg %x)\n",
          Bcb, CacheSeg);
@@ -851,17 +849,17 @@ CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointers,
    if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
    {
       Bcb = (PBCB)SectionObjectPointers->SharedCacheMap;
-      assert(Bcb);
+      ASSERT(Bcb);
       if (FileOffset)
       {
         Offset = *FileOffset;
       }
-      else 
+      else
       {
-        Offset.QuadPart = 0LL;
+        Offset.QuadPart = (LONGLONG)0;
         Length = Bcb->FileSize.u.LowPart;
       }
-   
+
       if (IoStatus)
       {
         IoStatus->Status = STATUS_SUCCESS;
@@ -873,7 +871,6 @@ CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointers,
         current = CcRosLookupCacheSegment (Bcb, Offset.u.LowPart);
         if (current != NULL)
         {
-           ExAcquireFastMutex(&current->Lock);
            if (current->Dirty)
            {
               Status = CcRosFlushCacheSegment(current);
@@ -908,7 +905,7 @@ CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointers,
    }
 }
 
-NTSTATUS 
+NTSTATUS
 CcRosDeleteFileCache(PFILE_OBJECT FileObject, PBCB Bcb)
 /*
  * FUNCTION: Releases the BCB associated with a file object
@@ -920,8 +917,8 @@ CcRosDeleteFileCache(PFILE_OBJECT FileObject, PBCB Bcb)
    LIST_ENTRY FreeList;
    KIRQL oldIrql;
 
-   assert(Bcb);
-   
+   ASSERT(Bcb);
+
    Bcb->RefCount++;
    ExReleaseFastMutex(&ViewLock);
 
@@ -937,7 +934,7 @@ CcRosDeleteFileCache(PFILE_OBJECT FileObject, PBCB Bcb)
          Bcb->BcbRemoveListEntry.Flink = NULL;
       }
 
-      FileObject->SectionObjectPointer->SharedCacheMap = NULL;  
+      FileObject->SectionObjectPointer->SharedCacheMap = NULL;
 
       /*
        * Release all cache segments.
@@ -959,7 +956,7 @@ CcRosDeleteFileCache(PFILE_OBJECT FileObject, PBCB Bcb)
         }
          InsertHeadList(&FreeList, &current->BcbSegmentListEntry);
       }
-      KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);       
+      KeReleaseSpinLock(&Bcb->BcbLock, oldIrql);
 
       ExReleaseFastMutex(&ViewLock);
       ObDereferenceObject (Bcb->FileObject);
@@ -970,7 +967,7 @@ CcRosDeleteFileCache(PFILE_OBJECT FileObject, PBCB Bcb)
          current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry);
          Status = CcRosInternalFreeCacheSegment(current);
       }
-      ExFreeToNPagedLookasideList(&BcbLookasideList, Bcb);   
+      ExFreeToNPagedLookasideList(&BcbLookasideList, Bcb);
       ExAcquireFastMutex(&ViewLock);
    }
    return(STATUS_SUCCESS);
@@ -981,17 +978,17 @@ VOID CcRosReferenceCache(PFILE_OBJECT FileObject)
   PBCB Bcb;
   ExAcquireFastMutex(&ViewLock);
   Bcb = (PBCB)FileObject->SectionObjectPointer->SharedCacheMap;
-  assert(Bcb);
+  ASSERT(Bcb);
   if (Bcb->RefCount == 0)
   {
-     assert(Bcb->BcbRemoveListEntry.Flink != NULL);
+     ASSERT(Bcb->BcbRemoveListEntry.Flink != NULL);
      RemoveEntryList(&Bcb->BcbRemoveListEntry);
      Bcb->BcbRemoveListEntry.Flink = NULL;
 
   }
   else
   {
-     assert(Bcb->BcbRemoveListEntry.Flink == NULL);
+     ASSERT(Bcb->BcbRemoveListEntry.Flink == NULL);
   }
   Bcb->RefCount++;
   ExReleaseFastMutex(&ViewLock);
@@ -1000,7 +997,7 @@ VOID CcRosReferenceCache(PFILE_OBJECT FileObject)
 VOID CcRosSetRemoveOnClose(PSECTION_OBJECT_POINTERS SectionObjectPointer)
 {
   PBCB Bcb;
-//  DPRINT1("CcRosSetRemoveOnClose()\n");
+  DPRINT("CcRosSetRemoveOnClose()\n");
   ExAcquireFastMutex(&ViewLock);
   Bcb = (PBCB)SectionObjectPointer->SharedCacheMap;
   if (Bcb)
@@ -1020,7 +1017,7 @@ VOID CcRosDereferenceCache(PFILE_OBJECT FileObject)
   PBCB Bcb;
   ExAcquireFastMutex(&ViewLock);
   Bcb = (PBCB)FileObject->SectionObjectPointer->SharedCacheMap;
-  assert(Bcb);
+  ASSERT(Bcb);
   if (Bcb->RefCount > 0)
   {
     Bcb->RefCount--;
@@ -1041,10 +1038,7 @@ VOID CcRosDereferenceCache(PFILE_OBJECT FileObject)
   ExReleaseFastMutex(&ViewLock);
 }
 
-/*
- * @implemented
- */
-NTSTATUS STDCALL 
+NTSTATUS STDCALL
 CcRosReleaseFileCache(PFILE_OBJECT FileObject)
 /*
  * FUNCTION: Called by the file system when a handle to a file object
@@ -1066,6 +1060,7 @@ CcRosReleaseFileCache(PFILE_OBJECT FileObject)
          Bcb->RefCount--;
         if (Bcb->RefCount == 0)
         {
+            MmFreeSectionSegments(Bcb->FileObject);
            if (Bcb->RemoveOnClose)
            {
               CcRosDeleteFileCache(FileObject, Bcb);
@@ -1083,7 +1078,7 @@ CcRosReleaseFileCache(PFILE_OBJECT FileObject)
   return(STATUS_SUCCESS);
 }
 
-NTSTATUS 
+NTSTATUS
 CcTryToInitializeFileCache(PFILE_OBJECT FileObject)
 {
    PBCB Bcb;
@@ -1116,10 +1111,7 @@ CcTryToInitializeFileCache(PFILE_OBJECT FileObject)
 }
 
 
-/*
- * @implemented
- */
-NTSTATUS STDCALL 
+NTSTATUS STDCALL
 CcRosInitializeFileCache(PFILE_OBJECT FileObject,
                         ULONG CacheSegmentSize)
 /*
@@ -1127,21 +1119,21 @@ CcRosInitializeFileCache(PFILE_OBJECT FileObject,
  */
 {
    PBCB Bcb;
+
+   Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
    DPRINT("CcRosInitializeFileCache(FileObject %x, *Bcb %x, CacheSegmentSize %d)\n",
            FileObject, Bcb, CacheSegmentSize);
 
    ExAcquireFastMutex(&ViewLock);
-
-   Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
    if (Bcb == NULL)
    {
-      Bcb = ExAllocateFromNPagedLookasideList(&BcbLookasideList);      
+      Bcb = ExAllocateFromNPagedLookasideList(&BcbLookasideList);
       if (Bcb == NULL)
       {
         ExReleaseFastMutex(&ViewLock);
        return(STATUS_UNSUCCESSFUL);
       }
-      memset(Bcb, 0, sizeof(BCB));      
+      memset(Bcb, 0, sizeof(BCB));
       ObReferenceObjectByPointer(FileObject,
                                 FILE_ALL_ACCESS,
                                 NULL,
@@ -1150,9 +1142,9 @@ CcRosInitializeFileCache(PFILE_OBJECT FileObject,
       Bcb->CacheSegmentSize = CacheSegmentSize;
       if (FileObject->FsContext)
       {
-         Bcb->AllocationSize = 
+         Bcb->AllocationSize =
           ((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->AllocationSize;
-         Bcb->FileSize = 
+         Bcb->FileSize =
           ((PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext)->FileSize;
       }
       KeInitializeSpinLock(&Bcb->BcbLock);
@@ -1184,7 +1176,7 @@ CcGetFileObjectFromSectionPtrs(IN PSECTION_OBJECT_POINTERS SectionObjectPointers
    if (SectionObjectPointers && SectionObjectPointers->SharedCacheMap)
    {
       Bcb = (PBCB)SectionObjectPointers->SharedCacheMap;
-      assert(Bcb);
+      ASSERT(Bcb);
       return Bcb->FileObject;
    }
    return NULL;
@@ -1203,7 +1195,7 @@ CmLazyCloseThreadMain(PVOID Ignored)
 
    while (1)
    {
-      Timeout.QuadPart += 100000000LL; // 10sec
+      Timeout.QuadPart += (LONGLONG)100000000; // 10sec
       Status = KeWaitForSingleObject(&LazyCloseThreadEvent,
                                     0,
                                     KernelMode,
@@ -1215,7 +1207,7 @@ CmLazyCloseThreadMain(PVOID Ignored)
       if (!NT_SUCCESS(Status))
       {
          DbgPrint("LazyCloseThread: Wait failed\n");
-         KeBugCheck(0);
+         KEBUGCHECKCC;
          break;
       }
       if (LazyCloseThreadShouldTerminate)
@@ -1223,7 +1215,7 @@ CmLazyCloseThreadMain(PVOID Ignored)
           DbgPrint("LazyCloseThread: Terminating\n");
          break;
       }
-      
+
       ExAcquireFastMutex(&ViewLock);
       CcTimeStamp++;
       if (CcTimeStamp >= 30)
@@ -1244,18 +1236,20 @@ CmLazyCloseThreadMain(PVOID Ignored)
    }
 }
 
-VOID
+VOID INIT_FUNCTION
 CcInitView(VOID)
 {
 #ifdef CACHE_BITMAP
   PMEMORY_AREA marea;
   PVOID Buffer;
+  PHYSICAL_ADDRESS BoundaryAddressMultiple;
 #endif
   NTSTATUS Status;
   KPRIORITY Priority;
 
   DPRINT("CcInitView()\n");
 #ifdef CACHE_BITMAP
+  BoundaryAddressMultiple.QuadPart = 0;
   CiCacheSegMappingRegionHint = 0;
   CiCacheSegMappingRegionBase = NULL;
 
@@ -1269,11 +1263,12 @@ CcInitView(VOID)
                              0,
                              &marea,
                              FALSE,
-                             FALSE);
+                             FALSE,
+                             BoundaryAddressMultiple);
   MmUnlockAddressSpace(MmGetKernelAddressSpace());
   if (!NT_SUCCESS(Status))
     {
-      KeBugCheck(0);
+      KEBUGCHECKCC;
     }
 
   Buffer = ExAllocatePool(NonPagedPool, CI_CACHESEG_MAPPING_REGION_SIZE / (PAGE_SIZE * 8));
@@ -1282,7 +1277,7 @@ CcInitView(VOID)
   RtlClearAllBits(&CiCacheSegMappingRegionAllocMap);
 
   KeInitializeSpinLock(&CiCacheSegMappingRegionLock);
-#endif  
+#endif
   InitializeListHead(&CacheSegmentListHead);
   InitializeListHead(&DirtySegmentListHead);
   InitializeListHead(&CacheSegmentLRUListHead);
@@ -1311,10 +1306,10 @@ CcInitView(VOID)
                                   20);
 
   MmInitializeMemoryConsumer(MC_CACHE, CcRosTrimCache);
-  
+
   CcInitCacheZeroPage();
 
-  CcTimeStamp = 0;  
+  CcTimeStamp = 0;
   LazyCloseThreadShouldTerminate = FALSE;
   KeInitializeEvent (&LazyCloseThreadEvent, SynchronizationEvent, FALSE);
   Status = PsCreateSystemThread(&LazyCloseThreadHandle,