On FAT16 partitions smaller than 128MB, the cluster size is 2048, which is
[reactos.git] / reactos / ntoskrnl / mm / pagefile.c
index a14a812..91d8beb 100644 (file)
 #define NDEBUG
 #include <internal/debug.h>
 
+#if defined (ALLOC_PRAGMA)
+#pragma alloc_text(INIT, MmInitPagingFile)
+#endif
+
+
 /* TYPES *********************************************************************/
 
 typedef struct _PAGINGFILE
@@ -135,6 +140,7 @@ MmIsFileAPagingFile(PFILE_OBJECT FileObject)
 }
 
 VOID
+NTAPI
 MmShowOutOfSpaceMessagePagingFile(VOID)
 {
    if (!MmSwapSpaceMessage)
@@ -201,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;
@@ -255,7 +263,9 @@ NTSTATUS MmWriteToSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
    return(Status);
 }
 
-NTSTATUS MmReadFromSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
+NTSTATUS
+NTAPI
+MmReadFromSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
 {
    ULONG i, offset;
    LARGE_INTEGER file_offset;
@@ -309,7 +319,9 @@ NTSTATUS MmReadFromSwapPage(SWAPENTRY SwapEntry, PFN_TYPE Page)
    return(Status);
 }
 
-VOID INIT_FUNCTION
+VOID
+INIT_FUNCTION
+NTAPI
 MmInitPagingFile(VOID)
 {
    ULONG i;
@@ -344,6 +356,7 @@ MmInitPagingFile(VOID)
 }
 
 BOOLEAN
+NTAPI
 MmReserveSwapPages(ULONG Nr)
 {
    KIRQL oldIrql;
@@ -363,6 +376,7 @@ MmReserveSwapPages(ULONG Nr)
 }
 
 VOID
+NTAPI
 MmDereserveSwapPages(ULONG Nr)
 {
    KIRQL oldIrql;
@@ -400,6 +414,7 @@ MiAllocPageFromPagingFile(PPAGINGFILE PagingFile)
 }
 
 VOID
+NTAPI
 MmFreeSwapPage(SWAPENTRY Entry)
 {
    ULONG i;
@@ -435,12 +450,14 @@ MmFreeSwapPage(SWAPENTRY Entry)
 }
 
 BOOLEAN
+NTAPI
 MmIsAvailableSwapPage(VOID)
 {
    return(MiFreeSwapPages > 0);
 }
 
 SWAPENTRY
+NTAPI
 MmAllocSwapPage(VOID)
 {
    KIRQL oldIrql;
@@ -532,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)));
@@ -792,15 +809,6 @@ NtCreatePagingFile(IN PUNICODE_STRING FileName,
 
    PreviousMode = ExGetPreviousMode();
 
-   Status = RtlCaptureUnicodeString(&CapturedFileName,
-                                    PreviousMode,
-                                    PagedPool,
-                                    FALSE,
-                                    FileName);
-   if (!NT_SUCCESS(Status))
-   {
-      return(Status);
-   }
    if (PreviousMode != KernelMode)
    {
       _SEH_TRY
@@ -816,9 +824,6 @@ NtCreatePagingFile(IN PUNICODE_STRING FileName,
 
       if (!NT_SUCCESS(Status))
       {
-         RtlReleaseCapturedUnicodeString(&CapturedFileName,
-                                         PreviousMode,
-                                         FALSE);
          return Status;
       }
    }
@@ -828,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,
@@ -849,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);
@@ -868,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,