On FAT16 partitions smaller than 128MB, the cluster size is 2048, which is
[reactos.git] / reactos / ntoskrnl / mm / pagefile.c
index e5727fe..91d8beb 100644 (file)
 #define NDEBUG
 #include <internal/debug.h>
 
+#if defined (ALLOC_PRAGMA)
+#pragma alloc_text(INIT, MmInitPagingFile)
+#endif
+
+
 /* TYPES *********************************************************************/
 
 typedef struct _PAGINGFILE
@@ -78,7 +83,7 @@ ULONG MiFreeSwapPages;
 ULONG MiUsedSwapPages;
 
 /*
- * Number of pages that have been reserved for swapping but not yet allocated 
+ * Number of pages that have been reserved for swapping but not yet allocated
  */
 static ULONG MiReservedSwapPages;
 
@@ -117,7 +122,25 @@ static BOOLEAN MmSwapSpaceMessage = FALSE;
 
 /* FUNCTIONS *****************************************************************/
 
+BOOLEAN
+STDCALL
+MmIsFileAPagingFile(PFILE_OBJECT FileObject)
+{
+    ULONG i;
+
+    /* Loop through all the paging files */
+    for (i = 0; i < MiPagingFileCount; i++)
+    {
+        /* Check if this is one of them */
+        if (PagingFileList[i]->FileObject == FileObject) return TRUE;
+    }
+
+    /* Nothing found */
+    return FALSE;
+}
+
 VOID
+NTAPI
 MmShowOutOfSpaceMessagePagingFile(VOID)
 {
    if (!MmSwapSpaceMessage)
@@ -184,7 +207,9 @@ MmGetOffsetPageFile(PRETRIEVAL_POINTERS_BUFFER RetrievalPointers, LARGE_INTEGER
 #endif
 }
 
-NTSTATUS MmWriteToSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
+NTSTATUS
+NTAPI
+MmWriteToSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
 {
    ULONG i, offset;
    LARGE_INTEGER file_offset;
@@ -224,21 +249,23 @@ NTSTATUS MmWriteToSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
    file_offset = MmGetOffsetPageFile(PagingFileList[i]->RetrievalPointers, file_offset);
 
    KeInitializeEvent(&Event, NotificationEvent, FALSE);
-   Status = IoPageWrite(PagingFileList[i]->FileObject,
-                        Mdl,
-                        &file_offset,
-                        &Event,
-                        &Iosb);
+   Status = IoSynchronousPageWrite(PagingFileList[i]->FileObject,
+                                   Mdl,
+                                   &file_offset,
+                                   &Event,
+                                   &Iosb);
    if (Status == STATUS_PENDING)
    {
       KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
       Status = Iosb.Status;
    }
-   MmUnmapLockedPages(Mdl->MappedSystemVa, Mdl);            
+   MmUnmapLockedPages(Mdl->MappedSystemVa, Mdl);
    return(Status);
 }
 
-NTSTATUS MmReadFromSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
+NTSTATUS
+NTAPI
+MmReadFromSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
 {
    ULONG i, offset;
    LARGE_INTEGER file_offset;
@@ -288,11 +315,13 @@ NTSTATUS MmReadFromSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
       KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
       Status = Iosb.Status;
    }
-   MmUnmapLockedPages(Mdl->MappedSystemVa, Mdl);            
+   MmUnmapLockedPages(Mdl->MappedSystemVa, Mdl);
    return(Status);
 }
 
-VOID INIT_FUNCTION
+VOID
+INIT_FUNCTION
+NTAPI
 MmInitPagingFile(VOID)
 {
    ULONG i;
@@ -327,6 +356,7 @@ MmInitPagingFile(VOID)
 }
 
 BOOLEAN
+NTAPI
 MmReserveSwapPages(ULONG Nr)
 {
    KIRQL oldIrql;
@@ -346,6 +376,7 @@ MmReserveSwapPages(ULONG Nr)
 }
 
 VOID
+NTAPI
 MmDereserveSwapPages(ULONG Nr)
 {
    KIRQL oldIrql;
@@ -383,6 +414,7 @@ MiAllocPageFromPagingFile(PPAGINGFILE PagingFile)
 }
 
 VOID
+NTAPI
 MmFreeSwapPage(SWAPENTRY Entry)
 {
    ULONG i;
@@ -391,7 +423,7 @@ MmFreeSwapPage(SWAPENTRY Entry)
 
    i = FILE_FROM_ENTRY(Entry);
    off = OFFSET_FROM_ENTRY(Entry);
-   
+
    if (i >= MAX_PAGING_FILES)
    {
        DPRINT1("Bad swap entry 0x%.8X\n", Entry);
@@ -404,9 +436,9 @@ MmFreeSwapPage(SWAPENTRY Entry)
       KEBUGCHECK(0);
    }
    KeAcquireSpinLockAtDpcLevel(&PagingFileList[i]->AllocMapLock);
-   
+
    PagingFileList[i]->AllocMap[off >> 5] &= (~(1 << (off % 32)));
-   
+
    PagingFileList[i]->FreePages++;
    PagingFileList[i]->UsedPages--;
 
@@ -418,12 +450,14 @@ MmFreeSwapPage(SWAPENTRY Entry)
 }
 
 BOOLEAN
+NTAPI
 MmIsAvailableSwapPage(VOID)
 {
    return(MiFreeSwapPages > 0);
 }
 
 SWAPENTRY
+NTAPI
 MmAllocSwapPage(VOID)
 {
    KIRQL oldIrql;
@@ -515,7 +549,7 @@ MmDumpToPagingFile(ULONG BugCode,
    Headers->Type = MmCoreDumpType;
    if (TrapFrame != NULL)
    {
-      if (!(TrapFrame->Eflags & (1 << 17)))
+      if (!(TrapFrame->EFlags & (1 << 17)))
       {
          memcpy(&Headers->TrapFrame, TrapFrame,
                 sizeof(KTRAP_FRAME) - (4 * sizeof(DWORD)));
@@ -585,7 +619,7 @@ MmDumpToPagingFile(ULONG BugCode,
       for (i = 0; i < MmStats.NrTotalPages; i++)
       {
          MdlMap[0] = i;
-         MmCreateVirtualMappingForKernel(MmCoreDumpPageFrame, 
+         MmCreateVirtualMappingForKernel(MmCoreDumpPageFrame,
                                         PAGE_READWRITE,
                                          MdlMap,
                                         1);
@@ -631,10 +665,10 @@ MmInitializeCrashDump(HANDLE PageFileHandle, ULONG PageFileNum)
    PIRP Irp;
    KEVENT Event;
    IO_STATUS_BLOCK Iosb;
-   UNICODE_STRING DiskDumpName;
+   UNICODE_STRING DiskDumpName = RTL_CONSTANT_STRING(L"DiskDump");
    ANSI_STRING ProcName;
    PIO_STACK_LOCATION StackPtr;
-   PMODULE_OBJECT ModuleObject;
+   PLDR_DATA_TABLE_ENTRY ModuleObject;
 
    Status = ZwFsControlFile(PageFileHandle,
                             0,
@@ -678,7 +712,7 @@ MmInitializeCrashDump(HANDLE PageFileHandle, ULONG PageFileNum)
                                        FALSE,
                                        &Event,
                                        &Iosb);
-   if(Irp == NULL) 
+   if(Irp == NULL)
    {
       ObDereferenceObject(PageFile);
       return(STATUS_NO_MEMORY);// tMk - is this correct return code ???
@@ -707,14 +741,13 @@ MmInitializeCrashDump(HANDLE PageFileHandle, ULONG PageFileNum)
    }
 
    /* Load the diskdump driver. */
-   RtlRosInitUnicodeStringFromLiteral(&DiskDumpName, L"DiskDump");
    ModuleObject = LdrGetModuleObject(&DiskDumpName);
    if (ModuleObject == NULL)
    {
       return(STATUS_OBJECT_NAME_NOT_FOUND);
    }
    RtlInitAnsiString(&ProcName, "DiskDumpFunctions");
-   Status = LdrGetProcedureAddress(ModuleObject->Base,
+   Status = LdrGetProcedureAddress(ModuleObject->DllBase,
                                    &ProcName,
                                    0,
                                    (PVOID*)&MmCoreDumpFunctions);
@@ -775,40 +808,22 @@ NtCreatePagingFile(IN PUNICODE_STRING FileName,
    }
 
    PreviousMode = ExGetPreviousMode();
-   
-   Status = RtlCaptureUnicodeString(&CapturedFileName,
-                                    PreviousMode,
-                                    PagedPool,
-                                    FALSE,
-                                    FileName);
-   if (!NT_SUCCESS(Status))
-   {
-      return(Status);
-   }
-   if (PreviousMode == UserMode)
+
+   if (PreviousMode != KernelMode)
    {
       _SEH_TRY
       {
-         ProbeForRead(InitialSize,
-                      sizeof(LARGE_INTEGER),
-                      sizeof(ULONG));
-         SafeInitialSize = *InitialSize;
-         ProbeForRead(MaximumSize,
-                      sizeof(LARGE_INTEGER),
-                      sizeof(ULONG));
-         SafeMaximumSize = *MaximumSize;
+         SafeInitialSize = ProbeForReadLargeInteger(InitialSize);
+         SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
       }
       _SEH_HANDLE
       {
          Status = _SEH_GetExceptionCode();
       }
       _SEH_END;
-    
+
       if (!NT_SUCCESS(Status))
       {
-         RtlReleaseCapturedUnicodeString(&CapturedFileName,
-                                         PreviousMode,
-                                         FALSE);
          return Status;
       }
    }
@@ -818,6 +833,29 @@ NtCreatePagingFile(IN PUNICODE_STRING FileName,
       SafeMaximumSize = *MaximumSize;
    }
 
+   /* Pagefiles can't be larger than 4GB and ofcourse the minimum should be
+      smaller than the maximum */
+   if (0 != SafeInitialSize.u.HighPart)
+   {
+      return STATUS_INVALID_PARAMETER_2;
+   }
+   if (0 != SafeMaximumSize.u.HighPart)
+   {
+      return STATUS_INVALID_PARAMETER_3;
+   }
+   if (SafeMaximumSize.u.LowPart < SafeInitialSize.u.LowPart)
+   {
+      return STATUS_INVALID_PARAMETER_MIX;
+   }
+
+   Status = ProbeAndCaptureUnicodeString(&CapturedFileName,
+                                         PreviousMode,
+                                         FileName);
+   if (!NT_SUCCESS(Status))
+   {
+      return(Status);
+   }
+
    InitializeObjectAttributes(&ObjectAttributes,
                               &CapturedFileName,
                               0,
@@ -839,9 +877,8 @@ NtCreatePagingFile(IN PUNICODE_STRING FileName,
                          NULL,
                          SL_OPEN_PAGING_FILE | IO_NO_PARAMETER_CHECKING);
 
-   RtlReleaseCapturedUnicodeString(&CapturedFileName,
-                                   PreviousMode,
-                                   FALSE);
+   ReleaseCapturedUnicodeString(&CapturedFileName,
+                                PreviousMode);
    if (!NT_SUCCESS(Status))
    {
       return(Status);
@@ -858,12 +895,24 @@ NtCreatePagingFile(IN PUNICODE_STRING FileName,
       return Status;
    }
 
-   BytesPerAllocationUnit = FsSizeInformation.SectorsPerAllocationUnit * FsSizeInformation.BytesPerSector;
-   if (BytesPerAllocationUnit % PAGE_SIZE)
+   BytesPerAllocationUnit = FsSizeInformation.SectorsPerAllocationUnit *
+                            FsSizeInformation.BytesPerSector;
+
+   /* We have to find a value which is a multiple of both PAGE_SIZE and
+      BytesPerAllocationUnit */
+   SafeInitialSize.u.LowPart = ((SafeInitialSize.u.LowPart + PAGE_SIZE - 1) /
+                                PAGE_SIZE) * PAGE_SIZE;
+   while (0 != (SafeInitialSize.u.LowPart % BytesPerAllocationUnit) &&
+          SafeInitialSize.u.LowPart <= SafeMaximumSize.u.LowPart - PAGE_SIZE)
+   {
+      SafeInitialSize.u.LowPart += PAGE_SIZE;
+   }
+   if (0 != (SafeInitialSize.u.LowPart % BytesPerAllocationUnit))
    {
       ZwClose(FileHandle);
-      return STATUS_UNSUCCESSFUL;
+      return STATUS_ALLOTTED_SPACE_EXCEEDED;
    }
+   ASSERT(0 == (SafeInitialSize.u.LowPart % PAGE_SIZE));
 
    Status = ZwSetInformationFile(FileHandle,
                                  &IoStatus,