update for HEAD-2003021201
[reactos.git] / ntoskrnl / mm / section.c
index 5c3af8c..3c7dddb 100644 (file)
@@ -329,6 +329,8 @@ MiReadPage(PMEMORY_AREA MemoryArea,
   FileObject = MemoryArea->Data.SectionData.Section->FileObject;
   Fcb = (PREACTOS_COMMON_FCB_HEADER)FileObject->FsContext;
   
+  assert(Fcb->Bcb);
+
   /*
    * If the file system is letting us go directly to the cache and the
    * memory area was mapped at an offset in the file which is page aligned
@@ -562,6 +564,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        if (PageOp->OpType != MM_PAGEOP_PAGEIN)
         {
           MmLockAddressSpace(AddressSpace);
+           KeSetEvent(&PageOp->CompletionEvent, IO_NO_INCREMENT, FALSE);
           MmReleasePageOp(PageOp);
           DPRINT("Address 0x%.8X\n", Address);
           return(STATUS_MM_RESTART_OPERATION);
@@ -572,8 +575,11 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        if (!NT_SUCCESS(PageOp->Status))
         {
           MmLockAddressSpace(AddressSpace);
+          Status = PageOp->Status;
+          KeSetEvent(&PageOp->CompletionEvent, IO_NO_INCREMENT, FALSE);
+          MmReleasePageOp(PageOp);
           DPRINT("Address 0x%.8X\n", Address);
-          return(PageOp->Status);
+          return(Status);
         }
        MmLockAddressSpace(AddressSpace);
        MmLockSection(Section);
@@ -589,6 +595,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
           {
                MmUnlockSectionSegment(Segment);
                MmUnlockSection(Section);
+                KeSetEvent(&PageOp->CompletionEvent, IO_NO_INCREMENT, FALSE);
                MmReleasePageOp(PageOp);
                return(STATUS_MM_RESTART_OPERATION);
           } 
@@ -597,7 +604,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
           MmReferencePage(Page);       
           MmSharePageEntrySectionSegment(Segment, Offset.u.LowPart);
 
-          Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+          Status = MmCreateVirtualMapping(MemoryArea->Process,
                                           Address,
                                           MemoryArea->Attributes,
                                           Page,
@@ -607,7 +614,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
               DbgPrint("Unable to create virtual mapping\n");
               KeBugCheck(0);
             }
-          MmInsertRmap(Page, PsGetCurrentProcess()
+          MmInsertRmap(Page, MemoryArea->Process
                        (PVOID)PAGE_ROUND_DOWN(Address));
         }
        if (Locked)
@@ -616,6 +623,8 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
         }
        MmUnlockSectionSegment(Segment);
        MmUnlockSection(Section);
+       PageOp->Status = STATUS_SUCCESS;
+       KeSetEvent(&PageOp->CompletionEvent, IO_NO_INCREMENT, FALSE);
        MmReleasePageOp(PageOp);
        DPRINT("Address 0x%.8X\n", Address);
        return(STATUS_SUCCESS);
@@ -648,7 +657,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
           KeBugCheck(0);
         }
        
-       Status = MmCreateVirtualMapping(PsGetCurrentProcess(),                
+       Status = MmCreateVirtualMapping(AddressSpace->Process,                
                                       Address,
                                       Region->Protect,
                                       Page,
@@ -656,7 +665,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        while (Status == STATUS_NO_MEMORY)
         {
           MmUnlockAddressSpace(AddressSpace);     
-          Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+          Status = MmCreateVirtualMapping(AddressSpace->Process,
                                           Address,
                                           Region->Protect,
                                           Page,
@@ -678,7 +687,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        /*
        * Add the page to the process's working set
        */
-       MmInsertRmap(Page, PsGetCurrentProcess()
+       MmInsertRmap(Page, AddressSpace->Process
                    (PVOID)PAGE_ROUND_DOWN(Address));       
        
        /*
@@ -703,7 +712,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        /*
        * Just map the desired physical page 
        */
-       Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+       Status = MmCreateVirtualMapping(AddressSpace->Process,
                                       Address,
                                       Region->Protect,
                                       Offset,
@@ -746,12 +755,12 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
            MmLockSectionSegment(Segment);
         }
 
-       Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+       Status = MmCreateVirtualMapping(AddressSpace->Process,
                                       Address,
                                       Region->Protect,
                                       Page,
                                       FALSE);
-       MmInsertRmap(Page, PsGetCurrentProcess()
+       MmInsertRmap(Page, AddressSpace->Process
                    (PVOID)PAGE_ROUND_DOWN(Address));
        if (Locked)
         {
@@ -840,7 +849,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        MmSetPageEntrySectionSegment(Segment, Offset.QuadPart, Entry);
        MmSharePageEntrySectionSegment(Segment, Offset.QuadPart);
        
-       Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+       Status = MmCreateVirtualMapping(AddressSpace->Process,
                                       Address,
                                       Attributes,
                                       Page,
@@ -850,7 +859,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
           MmUnlockSectionSegment(Segment);
           MmUnlockSection(Section);
           MmUnlockAddressSpace(AddressSpace);
-          Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+          Status = MmCreateVirtualMapping(AddressSpace->Process,
                                           Address,
                                           Attributes,
                                           Page,
@@ -863,7 +872,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
           MmLockSection(Section);
           MmLockSectionSegment(Segment);
         }
-       MmInsertRmap(Page, PsGetCurrentProcess()
+       MmInsertRmap(Page, AddressSpace->Process
                    (PVOID)PAGE_ROUND_DOWN(Address));     
        if (!NT_SUCCESS(Status))
         {
@@ -941,12 +950,12 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        */
        MmSetSavedSwapEntryPage(Page, SwapEntry);
        
-       Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+       Status = MmCreateVirtualMapping(AddressSpace->Process,
                                       Address,
                                       Attributes,
                                       Page,
                                       FALSE);
-       MmInsertRmap(Page, PsGetCurrentProcess()
+       MmInsertRmap(Page, AddressSpace->Process
                    (PVOID)PAGE_ROUND_DOWN(Address));
        if (!NT_SUCCESS(Status))
         {
@@ -976,12 +985,12 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        MmReferencePage(Page);  
        MmSharePageEntrySectionSegment(Segment, Offset.QuadPart);
        
-       Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+       Status = MmCreateVirtualMapping(AddressSpace->Process,
                                        Address,
                                        Attributes,
                                        Page,
                                        FALSE);
-       MmInsertRmap(Page, PsGetCurrentProcess()
+       MmInsertRmap(Page, AddressSpace->Process
                     (PVOID)PAGE_ROUND_DOWN(Address));
        if (!NT_SUCCESS(Status))
          {
@@ -1107,6 +1116,7 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
        * Restart the operation
        */
        MmLockAddressSpace(AddressSpace);
+       KeSetEvent(&PageOp->CompletionEvent, IO_NO_INCREMENT, FALSE);
        MmReleasePageOp(PageOp);
        return(STATUS_MM_RESTART_OPERATION);
      }
@@ -1135,18 +1145,18 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
    /*
     * Delete the old entry.
     */
-   MmDeleteVirtualMapping(PsGetCurrentProcess(), Address, FALSE, NULL, NULL);
+   MmDeleteVirtualMapping(AddressSpace->Process, Address, FALSE, NULL, NULL);
 
    /*
     * Set the PTE to point to the new page
     */
    MmLockAddressSpace(AddressSpace);
-   Status = MmCreateVirtualMapping(PsGetCurrentProcess(),
+   Status = MmCreateVirtualMapping(AddressSpace->Process,
                                   Address,
                                   Region->Protect,
                                   NewPage,
                                   FALSE);   
-   MmInsertRmap(NewPage, PsGetCurrentProcess()
+   MmInsertRmap(NewPage, AddressSpace->Process
                (PVOID)PAGE_ROUND_DOWN(Address));
    if (!NT_SUCCESS(Status))
      {
@@ -1162,7 +1172,7 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
     * Unshare the old page.
     */
    MmUnsharePageEntrySectionSegment(Section, Segment, Offset.QuadPart, FALSE);
-   MmDeleteRmap(OldPage, PsGetCurrentProcess(),
+   MmDeleteRmap(OldPage, AddressSpace->Process,
                (PVOID)PAGE_ROUND_DOWN(Address));
    MmReleasePageMemoryConsumer(MC_USER, OldPage);
 
@@ -1773,7 +1783,7 @@ MmProtectSectionView(PMADDRESS_SPACE AddressSpace,
   NTSTATUS Status;
 
   Length = 
-    min(Length, MemoryArea->BaseAddress + MemoryArea->Length - BaseAddress);
+    min(Length, (ULONG) (MemoryArea->BaseAddress + MemoryArea->Length - BaseAddress));
   Region = MmFindRegion(MemoryArea->BaseAddress,
                        &MemoryArea->Data.SectionData.RegionListHead,
                        BaseAddress, NULL);
@@ -1841,6 +1851,12 @@ MmpDeleteSection(PVOID ObjectBody)
       ObDereferenceObject(Section->FileObject);
       Section->FileObject = NULL;
     }
+
+  if (Section->Segments->Flags & MM_PAGEFILE_SEGMENT)
+  {
+     MmFreePageTablesSectionSegment(Section->Segments);
+     ExFreePool(Section->Segments);
+  }
 }
 
 VOID STDCALL
@@ -2022,6 +2038,7 @@ MmCreatePageFileSection(PHANDLE SectionHandle,
   Segment->Length = MaximumSize.u.LowPart;
   Segment->Flags = MM_PAGEFILE_SEGMENT;
   Segment->WriteCopy = FALSE;
+  ObDereferenceObject(Section);
   return(STATUS_SUCCESS);
 }
 
@@ -2044,6 +2061,9 @@ MmCreateDataFileSection(PHANDLE SectionHandle,
   PFILE_OBJECT FileObject;
   PMM_SECTION_SEGMENT Segment;
   ULONG FileAccess;
+  IO_STATUS_BLOCK Iosb;
+  LARGE_INTEGER Offset;
+  CHAR Buffer;
   
   /*
    * Check the protection
@@ -2053,7 +2073,27 @@ MmCreateDataFileSection(PHANDLE SectionHandle,
     {
       return(STATUS_INVALID_PAGE_PROTECTION);
     }
-  
+
+  /*
+   * Read a bit so caching is initiated for the file object.
+   * This is only needed because MiReadPage currently cannot
+   * handle non-cached streams.
+   */
+  Offset.QuadPart = 0;
+  Status = ZwReadFile(FileHandle,
+                     NULL,
+                     NULL,
+                     NULL,
+                     &Iosb,
+                     &Buffer,
+                     sizeof (Buffer),
+                     &Offset,
+                     0);
+  if (!NT_SUCCESS(Status) && (Status != STATUS_END_OF_FILE))
+    {
+      return(Status);
+    }
+
   /*
    * Create the section
    */
@@ -2962,8 +3002,6 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
              MmFreeSwapPage(SavedSwapEntry);
              MmSetSavedSwapEntryPage(PhysAddr, 0);
            }
-         MmDeleteRmap(PhysAddr, MArea->Process, Address);
-         MmReleasePageMemoryConsumer(MC_USER, PhysAddr);
        }
       else
        {
@@ -2971,9 +3009,9 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
                                           MArea->Data.SectionData.Segment,
                                           Offset,
                                           Dirty);
-         MmDeleteRmap(PhysAddr, MArea->Process, Address);
-         MmReleasePageMemoryConsumer(MC_USER, PhysAddr);
        }
+      MmDeleteRmap(PhysAddr, MArea->Process, Address);
+      MmReleasePageMemoryConsumer(MC_USER, PhysAddr);
     }
 }
 
@@ -3483,8 +3521,20 @@ BOOLEAN STDCALL
 MmFlushImageSection (IN        PSECTION_OBJECT_POINTERS        SectionObjectPointer,
                     IN MMFLUSH_TYPE                    FlushType)
 {
-  UNIMPLEMENTED;
-  return (FALSE);
+   switch(FlushType)
+   {
+      case MmFlushForDelete: 
+         if (SectionObjectPointer->ImageSectionObject || 
+            SectionObjectPointer->DataSectionObject)
+        {
+            return FALSE;
+        }
+        CcRosSetRemoveOnClose(SectionObjectPointer);
+         return TRUE;
+      case MmFlushForWrite:
+        break;
+   }
+   return FALSE;
 }
 
 BOOLEAN STDCALL