3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: pagefile.c,v 1.44 2004/04/22 01:57:49 jimtabor Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/mm/pagefile.c
23 * PURPOSE: Paging file functions
24 * PROGRAMMER: David Welch (welch@mcmail.com)
29 /* INCLUDES *****************************************************************/
31 #include <ddk/ntddk.h>
32 #include <internal/io.h>
33 #include <internal/mm.h>
34 #include <napi/core.h>
35 #include <internal/ps.h>
36 #include <internal/ldr.h>
37 #include <rosrtl/string.h>
40 #include <internal/debug.h>
42 /* TYPES *********************************************************************/
44 typedef struct _PAGINGFILE
46 LIST_ENTRY PagingFileListEntry
;
47 PFILE_OBJECT FileObject
;
48 LARGE_INTEGER MaximumSize
;
49 LARGE_INTEGER CurrentSize
;
53 KSPIN_LOCK AllocMapLock
;
55 PGET_RETRIEVAL_DESCRIPTOR RetrievalPointers
;
57 PAGINGFILE
, *PPAGINGFILE
;
59 typedef struct _RETRIEVEL_DESCRIPTOR_LIST
61 struct _RETRIEVEL_DESCRIPTOR_LIST
* Next
;
62 GET_RETRIEVAL_DESCRIPTOR RetrievalPointers
;
64 RETRIEVEL_DESCRIPTOR_LIST
, *PRETRIEVEL_DESCRIPTOR_LIST
;
66 /* GLOBALS *******************************************************************/
68 #define PAIRS_PER_RUN (1024)
70 #define MAX_PAGING_FILES (32)
72 /* List of paging files, both used and free */
73 static PPAGINGFILE PagingFileList
[MAX_PAGING_FILES
];
75 /* Lock for examining the list of paging files */
76 static KSPIN_LOCK PagingFileListLock
;
78 /* Number of paging files */
79 static ULONG MiPagingFileCount
;
81 /* Number of pages that are available for swapping */
82 ULONG MiFreeSwapPages
;
84 /* Number of pages that have been allocated for swapping */
85 ULONG MiUsedSwapPages
;
88 * Number of pages that have been reserved for swapping but not yet allocated
90 static ULONG MiReservedSwapPages
;
93 * Ratio between reserved and available swap pages, e.g. setting this to five
94 * forces one swap page to be available for every five swap pages that are
95 * reserved. Setting this to zero turns off commit checking altogether.
97 #define MM_PAGEFILE_COMMIT_RATIO (1)
100 * Number of pages that can be used for potentially swapable memory without
101 * pagefile space being reserved. The intention is that this allows smss
102 * to start up and create page files while ordinarily having a commit
105 #define MM_PAGEFILE_COMMIT_GRACE (256)
107 static PVOID MmCoreDumpPageFrame
= NULL
;
108 static ULONG MmCoreDumpSize
;
109 static DUMP_POINTERS MmCoreDumpPointers
;
110 static PMM_CORE_DUMP_FUNCTIONS MmCoreDumpFunctions
;
111 static ULONG MmCoreDumpPageFile
= 0xFFFFFFFF;
112 static ROS_QUERY_LCN_MAPPING MmCoreDumpLcnMapping
;
114 ULONG MmCoreDumpType
= MM_CORE_DUMP_TYPE_NONE
;
117 * Translate between a swap entry and a file and offset pair.
119 #define FILE_FROM_ENTRY(i) ((i) >> 24)
120 #define OFFSET_FROM_ENTRY(i) (((i) & 0xffffff) - 1)
121 #define ENTRY_FROM_FILE_OFFSET(i, j) (((i) << 24) | ((j) + 1))
123 static BOOLEAN MmSwapSpaceMessage
= FALSE
;
125 /* FUNCTIONS *****************************************************************/
128 MmShowOutOfSpaceMessagePagingFile(VOID
)
130 if (!MmSwapSpaceMessage
)
132 DPRINT1("MM: Out of swap space.\n");
133 MmSwapSpaceMessage
= TRUE
;
138 MmGetOffsetPageFile(PGET_RETRIEVAL_DESCRIPTOR RetrievalPointers
, LARGE_INTEGER Offset
)
140 /* Simple binary search */
141 ULONG first
, last
, mid
;
143 last
= RetrievalPointers
->NumberOfPairs
- 1;
144 while (first
<= last
)
146 mid
= (last
- first
) / 2 + first
;
147 if ((ULONGLONG
) Offset
.QuadPart
< RetrievalPointers
->Pair
[mid
].Vcn
)
151 Offset
.QuadPart
+= RetrievalPointers
->Pair
[0].Lcn
- RetrievalPointers
->StartVcn
;
156 if ((ULONGLONG
) Offset
.QuadPart
>= RetrievalPointers
->Pair
[mid
-1].Vcn
)
158 Offset
.QuadPart
+= RetrievalPointers
->Pair
[mid
].Lcn
- RetrievalPointers
->Pair
[mid
-1].Vcn
;
166 if (mid
== RetrievalPointers
->NumberOfPairs
- 1)
170 if ((ULONGLONG
) Offset
.QuadPart
< RetrievalPointers
->Pair
[mid
+1].Vcn
)
172 Offset
.QuadPart
+= RetrievalPointers
->Pair
[mid
+1].Lcn
- RetrievalPointers
->Pair
[mid
].Vcn
;
179 #if defined(__GNUC__)
181 return (LARGE_INTEGER
)0LL;
185 const LARGE_INTEGER dummy
=
194 NTSTATUS
MmWriteToSwapPage(SWAPENTRY SwapEntry
, PMDL Mdl
)
197 LARGE_INTEGER file_offset
;
198 IO_STATUS_BLOCK Iosb
;
202 DPRINT("MmWriteToSwapPage\n");
207 return(STATUS_UNSUCCESSFUL
);
210 i
= FILE_FROM_ENTRY(SwapEntry
);
211 offset
= OFFSET_FROM_ENTRY(SwapEntry
);
213 if (i
>= MAX_PAGING_FILES
)
215 DPRINT1("Bad swap entry 0x%.8X\n", SwapEntry
);
218 if (PagingFileList
[i
]->FileObject
== NULL
||
219 PagingFileList
[i
]->FileObject
->DeviceObject
== NULL
)
221 DPRINT1("Bad paging file 0x%.8X\n", SwapEntry
);
225 file_offset
.QuadPart
= offset
* PAGE_SIZE
;
226 file_offset
= MmGetOffsetPageFile(PagingFileList
[i
]->RetrievalPointers
, file_offset
);
228 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
229 Status
= IoPageWrite(PagingFileList
[i
]->FileObject
,
234 if (Status
== STATUS_PENDING
)
236 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
242 NTSTATUS
MmReadFromSwapPage(SWAPENTRY SwapEntry
, PMDL Mdl
)
245 LARGE_INTEGER file_offset
;
246 IO_STATUS_BLOCK Iosb
;
250 DPRINT("MmReadFromSwapPage\n");
255 return(STATUS_UNSUCCESSFUL
);
258 i
= FILE_FROM_ENTRY(SwapEntry
);
259 offset
= OFFSET_FROM_ENTRY(SwapEntry
);
261 if (i
>= MAX_PAGING_FILES
)
263 DPRINT1("Bad swap entry 0x%.8X\n", SwapEntry
);
266 if (PagingFileList
[i
]->FileObject
== NULL
||
267 PagingFileList
[i
]->FileObject
->DeviceObject
== NULL
)
269 DPRINT1("Bad paging file 0x%.8X\n", SwapEntry
);
273 file_offset
.QuadPart
= offset
* PAGE_SIZE
;
274 file_offset
= MmGetOffsetPageFile(PagingFileList
[i
]->RetrievalPointers
, file_offset
);
276 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
277 Status
= IoPageRead(PagingFileList
[i
]->FileObject
,
282 if (Status
== STATUS_PENDING
)
284 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
291 MmInitPagingFile(VOID
)
295 KeInitializeSpinLock(&PagingFileListLock
);
299 MiReservedSwapPages
= 0;
301 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
303 PagingFileList
[i
] = NULL
;
305 MiPagingFileCount
= 0;
308 * Initialize the crash dump support.
310 if (MmCoreDumpType
!= MM_CORE_DUMP_TYPE_NONE
)
312 MmCoreDumpPageFrame
= MmAllocateSection(PAGE_SIZE
);
313 if (MmCoreDumpType
== MM_CORE_DUMP_TYPE_FULL
)
315 MmCoreDumpSize
= MmStats
.NrTotalPages
* 4096 + 1024 * 1024;
319 MmCoreDumpSize
= 1024 * 1024;
325 MmReserveSwapPages(ULONG Nr
)
328 ULONG MiAvailSwapPages
;
330 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
332 (MiFreeSwapPages
* MM_PAGEFILE_COMMIT_RATIO
) + MM_PAGEFILE_COMMIT_GRACE
;
333 MiReservedSwapPages
= MiReservedSwapPages
+ Nr
;
334 if (MM_PAGEFILE_COMMIT_RATIO
!= 0 && MiAvailSwapPages
< MiReservedSwapPages
)
336 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
339 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
344 MmDereserveSwapPages(ULONG Nr
)
348 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
349 MiReservedSwapPages
= MiReservedSwapPages
- Nr
;
350 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
354 MiAllocPageFromPagingFile(PPAGINGFILE PagingFile
)
359 KeAcquireSpinLock(&PagingFile
->AllocMapLock
, &oldIrql
);
361 for (i
= 0; i
< PagingFile
->AllocMapSize
; i
++)
363 for (j
= 0; j
< 32; j
++)
365 if (!(PagingFile
->AllocMap
[i
] & (1 << j
)))
374 PagingFile
->AllocMap
[i
] |= (1 << j
);
375 PagingFile
->UsedPages
++;
376 PagingFile
->FreePages
--;
377 KeReleaseSpinLock(&PagingFile
->AllocMapLock
, oldIrql
);
378 return((i
* 32) + j
);
381 KeReleaseSpinLock(&PagingFile
->AllocMapLock
, oldIrql
);
386 MmFreeSwapPage(SWAPENTRY Entry
)
392 i
= FILE_FROM_ENTRY(Entry
);
393 off
= OFFSET_FROM_ENTRY(Entry
);
395 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
396 if (PagingFileList
[i
] == NULL
)
400 KeAcquireSpinLockAtDpcLevel(&PagingFileList
[i
]->AllocMapLock
);
402 PagingFileList
[i
]->AllocMap
[off
/ 32] &= (~(1 << (off
% 32)));
404 PagingFileList
[i
]->FreePages
++;
405 PagingFileList
[i
]->UsedPages
--;
410 KeReleaseSpinLockFromDpcLevel(&PagingFileList
[i
]->AllocMapLock
);
411 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
415 MmIsAvailableSwapPage(VOID
)
417 return(MiFreeSwapPages
> 0);
421 MmAllocSwapPage(VOID
)
428 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
430 if (MiFreeSwapPages
== 0)
432 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
436 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
438 if (PagingFileList
[i
] != NULL
&&
439 PagingFileList
[i
]->FreePages
>= 1)
441 off
= MiAllocPageFromPagingFile(PagingFileList
[i
]);
442 if (off
== 0xFFFFFFFF)
445 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
446 return(STATUS_UNSUCCESSFUL
);
450 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
452 entry
= ENTRY_FROM_FILE_OFFSET(i
, off
);
457 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
462 STATIC PRETRIEVEL_DESCRIPTOR_LIST FASTCALL
463 MmAllocRetrievelDescriptorList(ULONG Pairs
)
466 PRETRIEVEL_DESCRIPTOR_LIST RetDescList
;
468 Size
= sizeof(RETRIEVEL_DESCRIPTOR_LIST
) + Pairs
* sizeof(MAPPING_PAIR
);
469 RetDescList
= ExAllocatePool(NonPagedPool
, Size
);
472 RtlZeroMemory(RetDescList
, Size
);
479 MmDumpToPagingFile(ULONG BugCode
,
480 ULONG BugCodeParameter1
,
481 ULONG BugCodeParameter2
,
482 ULONG BugCodeParameter3
,
483 ULONG BugCodeParameter4
,
484 PKTRAP_FRAME TrapFrame
)
486 PMM_CORE_DUMP_HEADER Headers
;
488 UCHAR MdlBase
[sizeof(MDL
) + sizeof(PVOID
)];
489 PMDL Mdl
= (PMDL
)MdlBase
;
490 PETHREAD Thread
= PsGetCurrentThread();
493 LONGLONG NextOffset
= 0;
495 PGET_RETRIEVAL_DESCRIPTOR RetrievalPointers
;
496 LARGE_INTEGER DiskOffset
;
498 if (MmCoreDumpPageFile
== 0xFFFFFFFF)
500 return(STATUS_UNSUCCESSFUL
);
503 DbgPrint("\nMM: Dumping core: ");
505 /* Prepare the dump headers. */
506 Headers
= (PMM_CORE_DUMP_HEADER
)MmCoreDumpPageFrame
;
507 Headers
->Magic
= MM_CORE_DUMP_HEADER_MAGIC
;
508 Headers
->Version
= MM_CORE_DUMP_HEADER_VERSION
;
509 Headers
->Type
= MmCoreDumpType
;
510 if (TrapFrame
!= NULL
)
512 if (!(TrapFrame
->Eflags
& (1 << 17)))
514 memcpy(&Headers
->TrapFrame
, TrapFrame
,
515 sizeof(KTRAP_FRAME
) - (4 * sizeof(DWORD
)));
519 memcpy(&Headers
->TrapFrame
, TrapFrame
, sizeof(KTRAP_FRAME
));
522 Headers
->BugCheckCode
= BugCode
;
523 Headers
->BugCheckParameters
[0] = BugCodeParameter1
;
524 Headers
->BugCheckParameters
[1] = BugCodeParameter2
;
525 Headers
->BugCheckParameters
[2] = BugCodeParameter3
;
526 Headers
->BugCheckParameters
[3] = BugCodeParameter4
;
527 Headers
->FaultingStackBase
= (PVOID
)Thread
->Tcb
.StackLimit
;
528 Headers
->FaultingStackSize
= StackSize
=
529 (ULONG
)((char*)Thread
->Tcb
.StackBase
- Thread
->Tcb
.StackLimit
);
530 Headers
->PhysicalMemorySize
= MmStats
.NrTotalPages
* PAGE_SIZE
;
532 /* Initialize the dump device. */
533 Status
= MmCoreDumpFunctions
->DumpInit();
534 if (!NT_SUCCESS(Status
))
536 DPRINT1("MM: Failed to initialize core dump device.\n");
540 /* Initialize the MDL. */
542 Mdl
->Size
= sizeof(MDL
) + sizeof(PVOID
);
543 Mdl
->MdlFlags
= MDL_SOURCE_IS_NONPAGED_POOL
;
545 Mdl
->MappedSystemVa
= MmCoreDumpPageFrame
;
547 Mdl
->ByteCount
= PAGE_SIZE
;
549 MdlMap
= (PULONG
)(Mdl
+ 1);
552 /* Initialize the retrieval offsets. */
553 RetrievalPointers
= PagingFileList
[MmCoreDumpPageFile
]->RetrievalPointers
;
555 /* Dump the header. */
556 MdlMap
[0] = MmGetPhysicalAddress(MmCoreDumpPageFrame
).u
.LowPart
;
557 #if defined(__GNUC__)
559 DiskOffset
= MmGetOffsetPageFile(RetrievalPointers
, (LARGE_INTEGER
)0LL);
563 const LARGE_INTEGER dummy
=
567 DiskOffset
= MmGetOffsetPageFile(RetrievalPointers
, dummy
);
570 DiskOffset
.QuadPart
+= MmCoreDumpLcnMapping
.LcnDiskOffset
.QuadPart
;
571 Status
= MmCoreDumpFunctions
->DumpWrite(DiskOffset
, Mdl
);
572 if (!NT_SUCCESS(Status
))
574 DPRINT1("MM: Failed to write core dump header\n.");
577 NextOffset
+= PAGE_SIZE
;
582 /* Write out the contents of physical memory. */
583 if (MmCoreDumpType
== MM_CORE_DUMP_TYPE_FULL
)
585 for (i
= 0; i
< MmStats
.NrTotalPages
; i
++)
587 LARGE_INTEGER PhysicalAddress
;
588 PhysicalAddress
.QuadPart
= i
* PAGE_SIZE
;
589 MdlMap
[0] = i
* PAGE_SIZE
;
590 MmCreateVirtualMappingDump(MmCoreDumpPageFrame
,
593 #if defined(__GNUC__)
595 DiskOffset
= MmGetOffsetPageFile(RetrievalPointers
,
596 (LARGE_INTEGER
)NextOffset
);
601 dummy
.QuadPart
= NextOffset
;
602 DiskOffset
= MmGetOffsetPageFile(RetrievalPointers
, dummy
);
605 DiskOffset
.QuadPart
+= MmCoreDumpLcnMapping
.LcnDiskOffset
.QuadPart
;
606 Status
= MmCoreDumpFunctions
->DumpWrite(DiskOffset
, Mdl
);
607 if (!NT_SUCCESS(Status
))
609 DPRINT1("MM: Failed to write page to core dump.\n");
612 if ((i
% ((1024*1024) / PAGE_SIZE
)) == 0)
614 DbgPrint("\b\b%.2d", i
/ ((1024*1024)/PAGE_SIZE
));
616 NextOffset
+= PAGE_SIZE
;
621 MmCoreDumpFunctions
->DumpFinish();
622 return(STATUS_SUCCESS
);
626 MmInitializeCrashDump(HANDLE PageFileHandle
, ULONG PageFileNum
)
628 PFILE_OBJECT PageFile
;
629 PDEVICE_OBJECT PageFileDevice
;
633 IO_STATUS_BLOCK Iosb
;
634 UNICODE_STRING DiskDumpName
;
635 ANSI_STRING ProcName
;
636 PIO_STACK_LOCATION StackPtr
;
637 PMODULE_OBJECT ModuleObject
;
639 Status
= ZwFsControlFile(PageFileHandle
,
644 FSCTL_ROS_QUERY_LCN_MAPPING
,
647 &MmCoreDumpLcnMapping
,
648 sizeof(ROS_QUERY_LCN_MAPPING
));
649 if (!NT_SUCCESS(Status
) ||
650 Iosb
.Information
!= sizeof(ROS_QUERY_LCN_MAPPING
))
655 /* Get the underlying storage device. */
657 ObReferenceObjectByHandle(PageFileHandle
,
663 if (!NT_SUCCESS(Status
))
668 PageFileDevice
= PageFile
->Vpb
->RealDevice
;
670 /* Get the dump pointers. */
671 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
672 Irp
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_GET_DUMP_POINTERS
,
677 sizeof(MmCoreDumpPointers
),
681 StackPtr
= IoGetNextIrpStackLocation(Irp
);
682 StackPtr
->FileObject
= PageFile
;
683 StackPtr
->DeviceObject
= PageFileDevice
;
684 StackPtr
->Parameters
.DeviceIoControl
.InputBufferLength
= 0;
685 StackPtr
->Parameters
.DeviceIoControl
.OutputBufferLength
= sizeof(MmCoreDumpPointers
);
687 Status
= IoCallDriver(PageFileDevice
,Irp
);
688 if (Status
== STATUS_PENDING
)
690 Status
= KeWaitForSingleObject(&Event
,
696 if (Status
!= STATUS_SUCCESS
||
697 Iosb
.Information
!= sizeof(MmCoreDumpPointers
))
699 ObDereferenceObject(PageFile
);
703 /* Load the diskdump driver. */
704 RtlRosInitUnicodeStringFromLiteral(&DiskDumpName
, L
"DiskDump");
705 ModuleObject
= LdrGetModuleObject(&DiskDumpName
);
706 if (ModuleObject
== NULL
)
708 return(STATUS_OBJECT_NAME_NOT_FOUND
);
710 RtlInitAnsiString(&ProcName
, "DiskDumpFunctions");
711 Status
= LdrGetProcedureAddress(ModuleObject
->Base
,
714 (PVOID
*)&MmCoreDumpFunctions
);
715 if (!NT_SUCCESS(Status
))
717 ObDereferenceObject(PageFile
);
721 /* Prepare for disk dumping. */
722 Status
= MmCoreDumpFunctions
->DumpPrepare(PageFileDevice
,
723 &MmCoreDumpPointers
);
724 if (!NT_SUCCESS(Status
))
726 ObDereferenceObject(PageFile
);
730 MmCoreDumpPageFile
= PageFileNum
;
731 ObDereferenceObject(PageFile
);
732 return(STATUS_SUCCESS
);
736 NtCreatePagingFile(IN PUNICODE_STRING FileName
,
737 IN PLARGE_INTEGER InitialSize
,
738 IN PLARGE_INTEGER MaximumSize
,
742 OBJECT_ATTRIBUTES ObjectAttributes
;
744 IO_STATUS_BLOCK IoStatus
;
745 PFILE_OBJECT FileObject
;
746 PPAGINGFILE PagingFile
;
749 FILE_FS_SIZE_INFORMATION FsSizeInformation
;
750 PRETRIEVEL_DESCRIPTOR_LIST RetDescList
;
751 PRETRIEVEL_DESCRIPTOR_LIST CurrentRetDescList
;
753 ULONG BytesPerAllocationUnit
;
760 DPRINT("NtCreatePagingFile(FileName %wZ, InitialSize %I64d)\n",
761 FileName
, InitialSize
->QuadPart
);
763 if (MiPagingFileCount
>= MAX_PAGING_FILES
)
765 return(STATUS_TOO_MANY_PAGING_FILES
);
768 InitializeObjectAttributes(&ObjectAttributes
,
774 Status
= IoCreateFile(&FileHandle
,
782 FILE_SYNCHRONOUS_IO_NONALERT
,
787 SL_OPEN_PAGING_FILE
);
788 if (!NT_SUCCESS(Status
))
793 Status
= NtQueryVolumeInformationFile(FileHandle
,
796 sizeof(FILE_FS_SIZE_INFORMATION
),
797 FileFsSizeInformation
);
798 if (!NT_SUCCESS(Status
))
804 BytesPerAllocationUnit
= FsSizeInformation
.SectorsPerAllocationUnit
* FsSizeInformation
.BytesPerSector
;
805 if (BytesPerAllocationUnit
% PAGE_SIZE
)
808 return STATUS_UNSUCCESSFUL
;
811 Status
= NtSetInformationFile(FileHandle
,
814 sizeof(LARGE_INTEGER
),
815 FileAllocationInformation
);
816 if (!NT_SUCCESS(Status
))
822 Status
= ObReferenceObjectByHandle(FileHandle
,
828 if (!NT_SUCCESS(Status
))
834 CurrentRetDescList
= RetDescList
= MmAllocRetrievelDescriptorList(PAIRS_PER_RUN
);
836 if (CurrentRetDescList
== NULL
)
838 ObDereferenceObject(FileObject
);
840 return(STATUS_NO_MEMORY
);
843 #if defined(__GNUC__)
851 MaxVcn
= (ULONG
)((InitialSize
->QuadPart
+ BytesPerAllocationUnit
- 1) / BytesPerAllocationUnit
);
854 Status
= NtFsControlFile(FileHandle
,
859 FSCTL_GET_RETRIEVAL_POINTERS
,
861 sizeof(LARGE_INTEGER
),
862 &CurrentRetDescList
->RetrievalPointers
,
863 sizeof(GET_RETRIEVAL_DESCRIPTOR
) + PAIRS_PER_RUN
* sizeof(MAPPING_PAIR
));
864 if (!NT_SUCCESS(Status
))
868 CurrentRetDescList
= RetDescList
;
869 RetDescList
= RetDescList
->Next
;
870 ExFreePool(CurrentRetDescList
);
872 ObDereferenceObject(FileObject
);
876 ExtentCount
+= CurrentRetDescList
->RetrievalPointers
.NumberOfPairs
;
877 if ((ULONG
)CurrentRetDescList
->RetrievalPointers
.Pair
[CurrentRetDescList
->RetrievalPointers
.NumberOfPairs
-1].Vcn
< MaxVcn
)
879 CurrentRetDescList
->Next
= MmAllocRetrievelDescriptorList(PAIRS_PER_RUN
);
880 if (CurrentRetDescList
->Next
== NULL
)
884 CurrentRetDescList
= RetDescList
;
885 RetDescList
= RetDescList
->Next
;
886 ExFreePool(CurrentRetDescList
);
888 ObDereferenceObject(FileObject
);
890 return(STATUS_NO_MEMORY
);
892 Vcn
.QuadPart
= CurrentRetDescList
->RetrievalPointers
.Pair
[CurrentRetDescList
->RetrievalPointers
.NumberOfPairs
-1].Vcn
;
893 CurrentRetDescList
= CurrentRetDescList
->Next
;
901 PagingFile
= ExAllocatePool(NonPagedPool
, sizeof(*PagingFile
));
902 if (PagingFile
== NULL
)
906 CurrentRetDescList
= RetDescList
;
907 RetDescList
= RetDescList
->Next
;
908 ExFreePool(CurrentRetDescList
);
910 ObDereferenceObject(FileObject
);
912 return(STATUS_NO_MEMORY
);
915 RtlZeroMemory(PagingFile
, sizeof(*PagingFile
));
917 PagingFile
->FileObject
= FileObject
;
918 PagingFile
->MaximumSize
.QuadPart
= MaximumSize
->QuadPart
;
919 PagingFile
->CurrentSize
.QuadPart
= InitialSize
->QuadPart
;
920 PagingFile
->FreePages
= (ULONG
)(InitialSize
->QuadPart
/ PAGE_SIZE
);
921 PagingFile
->UsedPages
= 0;
922 KeInitializeSpinLock(&PagingFile
->AllocMapLock
);
924 AllocMapSize
= (PagingFile
->FreePages
/ 32) + 1;
925 PagingFile
->AllocMap
= ExAllocatePool(NonPagedPool
,
926 AllocMapSize
* sizeof(ULONG
));
927 PagingFile
->AllocMapSize
= AllocMapSize
;
929 if (PagingFile
->AllocMap
== NULL
)
933 CurrentRetDescList
= RetDescList
;
934 RetDescList
= RetDescList
->Next
;
935 ExFreePool(CurrentRetDescList
);
937 ExFreePool(PagingFile
);
938 ObDereferenceObject(FileObject
);
940 return(STATUS_NO_MEMORY
);
942 DPRINT("ExtentCount: %d\n", ExtentCount
);
943 Size
= sizeof(GET_RETRIEVAL_DESCRIPTOR
) + ExtentCount
* sizeof(MAPPING_PAIR
);
944 PagingFile
->RetrievalPointers
= ExAllocatePool(NonPagedPool
, Size
);
945 if (PagingFile
->RetrievalPointers
== NULL
)
949 CurrentRetDescList
= RetDescList
;
950 RetDescList
= RetDescList
->Next
;
951 ExFreePool(CurrentRetDescList
);
953 ExFreePool(PagingFile
->AllocMap
);
954 ExFreePool(PagingFile
);
955 ObDereferenceObject(FileObject
);
957 return(STATUS_NO_MEMORY
);
960 RtlZeroMemory(PagingFile
->AllocMap
, AllocMapSize
* sizeof(ULONG
));
961 RtlZeroMemory(PagingFile
->RetrievalPointers
, Size
);
964 PagingFile
->RetrievalPointers
->NumberOfPairs
= ExtentCount
;
965 PagingFile
->RetrievalPointers
->StartVcn
= RetDescList
->RetrievalPointers
.StartVcn
;
966 CurrentRetDescList
= RetDescList
;
967 while (CurrentRetDescList
)
969 memcpy(&PagingFile
->RetrievalPointers
->Pair
[Count
],
970 CurrentRetDescList
->RetrievalPointers
.Pair
,
971 CurrentRetDescList
->RetrievalPointers
.NumberOfPairs
* sizeof(MAPPING_PAIR
));
972 Count
+= CurrentRetDescList
->RetrievalPointers
.NumberOfPairs
;
973 RetDescList
= CurrentRetDescList
;
974 CurrentRetDescList
= CurrentRetDescList
->Next
;
975 ExFreePool(RetDescList
);
978 if (PagingFile
->RetrievalPointers
->NumberOfPairs
!= ExtentCount
||
979 (ULONG
)PagingFile
->RetrievalPointers
->Pair
[ExtentCount
- 1].Vcn
!= MaxVcn
)
981 ExFreePool(PagingFile
->RetrievalPointers
);
982 ExFreePool(PagingFile
->AllocMap
);
983 ExFreePool(PagingFile
);
984 ObDereferenceObject(FileObject
);
986 return(STATUS_UNSUCCESSFUL
);
990 * Change the entries from lcn's to volume offset's.
992 PagingFile
->RetrievalPointers
->StartVcn
*= BytesPerAllocationUnit
;
993 for (i
= 0; i
< ExtentCount
; i
++)
995 PagingFile
->RetrievalPointers
->Pair
[i
].Lcn
*= BytesPerAllocationUnit
;
996 PagingFile
->RetrievalPointers
->Pair
[i
].Vcn
*= BytesPerAllocationUnit
;
999 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
1000 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
1002 if (PagingFileList
[i
] == NULL
)
1004 PagingFileList
[i
] = PagingFile
;
1008 MiFreeSwapPages
= MiFreeSwapPages
+ PagingFile
->FreePages
;
1009 MiPagingFileCount
++;
1010 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
1012 /* Check whether this pagefile can be a crash dump target. */
1013 if (MmCoreDumpType
!= MM_CORE_DUMP_TYPE_NONE
&&
1014 PagingFile
->CurrentSize
.QuadPart
>= MmCoreDumpSize
&&
1015 MmCoreDumpPageFile
== 0xFFFFFFFF)
1017 MmInitializeCrashDump(FileHandle
, i
);
1019 NtClose(FileHandle
);
1021 MmSwapSpaceMessage
= FALSE
;
1023 return(STATUS_SUCCESS
);