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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * PROJECT: ReactOS kernel
21 * FILE: ntoskrnl/mm/pagefile.c
22 * PURPOSE: Paging file functions
23 * PROGRAMMER: David Welch (welch@mcmail.com)
29 /* INCLUDES *****************************************************************/
35 #if defined (ALLOC_PRAGMA)
36 #pragma alloc_text(INIT, MmInitPagingFile)
39 /* GLOBALS *******************************************************************/
41 #define PAIRS_PER_RUN (1024)
43 /* List of paging files, both used and free */
44 PMMPAGING_FILE MmPagingFile
[MAX_PAGING_FILES
];
46 /* Lock for examining the list of paging files */
47 KGUARDED_MUTEX MmPageFileCreationLock
;
49 /* Number of paging files */
50 ULONG MmNumberOfPagingFiles
;
52 /* Number of pages that are available for swapping */
53 PFN_COUNT MiFreeSwapPages
;
55 /* Number of pages that have been allocated for swapping */
56 PFN_COUNT MiUsedSwapPages
;
58 BOOLEAN MmZeroPageFile
;
61 * Number of pages that have been reserved for swapping but not yet allocated
63 static PFN_COUNT MiReservedSwapPages
;
66 * Ratio between reserved and available swap pages, e.g. setting this to five
67 * forces one swap page to be available for every five swap pages that are
68 * reserved. Setting this to zero turns off commit checking altogether.
70 #define MM_PAGEFILE_COMMIT_RATIO (1)
73 * Number of pages that can be used for potentially swapable memory without
74 * pagefile space being reserved. The intention is that this allows smss
75 * to start up and create page files while ordinarily having a commit
78 #define MM_PAGEFILE_COMMIT_GRACE (256)
81 * Translate between a swap entry and a file and offset pair.
83 #define FILE_FROM_ENTRY(i) ((i) & 0x0f)
84 #define OFFSET_FROM_ENTRY(i) ((i) >> 11)
85 #define ENTRY_FROM_FILE_OFFSET(i, j) ((i) | ((j) << 11) | 0x400)
87 /* Make sure there can be only 16 paging files */
88 C_ASSERT(FILE_FROM_ENTRY(0xffffffff) < MAX_PAGING_FILES
);
90 static BOOLEAN MmSwapSpaceMessage
= FALSE
;
92 static BOOLEAN MmSystemPageFileLocated
= FALSE
;
94 /* FUNCTIONS *****************************************************************/
98 MmBuildMdlFromPages(PMDL Mdl
, PPFN_NUMBER Pages
)
100 memcpy(Mdl
+ 1, Pages
, sizeof(PFN_NUMBER
) * (PAGE_ROUND_UP(Mdl
->ByteOffset
+Mdl
->ByteCount
)/PAGE_SIZE
));
102 /* FIXME: this flag should be set by the caller perhaps? */
103 Mdl
->MdlFlags
|= MDL_IO_PAGE_READ
;
109 MmIsFileObjectAPagingFile(PFILE_OBJECT FileObject
)
113 /* Loop through all the paging files */
114 for (i
= 0; i
< MmNumberOfPagingFiles
; i
++)
116 /* Check if this is one of them */
117 if (MmPagingFile
[i
]->FileObject
== FileObject
) return TRUE
;
126 MmShowOutOfSpaceMessagePagingFile(VOID
)
128 if (!MmSwapSpaceMessage
)
130 DPRINT1("MM: Out of swap space.\n");
131 MmSwapSpaceMessage
= TRUE
;
137 MmWriteToSwapPage(SWAPENTRY SwapEntry
, PFN_NUMBER Page
)
141 LARGE_INTEGER file_offset
;
142 IO_STATUS_BLOCK Iosb
;
145 UCHAR MdlBase
[sizeof(MDL
) + sizeof(ULONG
)];
146 PMDL Mdl
= (PMDL
)MdlBase
;
148 DPRINT("MmWriteToSwapPage\n");
152 KeBugCheck(MEMORY_MANAGEMENT
);
153 return(STATUS_UNSUCCESSFUL
);
156 i
= FILE_FROM_ENTRY(SwapEntry
);
157 offset
= OFFSET_FROM_ENTRY(SwapEntry
) - 1;
159 if (MmPagingFile
[i
]->FileObject
== NULL
||
160 MmPagingFile
[i
]->FileObject
->DeviceObject
== NULL
)
162 DPRINT1("Bad paging file 0x%.8X\n", SwapEntry
);
163 KeBugCheck(MEMORY_MANAGEMENT
);
166 MmInitializeMdl(Mdl
, NULL
, PAGE_SIZE
);
167 MmBuildMdlFromPages(Mdl
, &Page
);
168 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
170 file_offset
.QuadPart
= offset
* PAGE_SIZE
;
172 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
173 Status
= IoSynchronousPageWrite(MmPagingFile
[i
]->FileObject
,
178 if (Status
== STATUS_PENDING
)
180 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
181 Status
= Iosb
.Status
;
184 if (Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
)
186 MmUnmapLockedPages (Mdl
->MappedSystemVa
, Mdl
);
194 MmReadFromSwapPage(SWAPENTRY SwapEntry
, PFN_NUMBER Page
)
196 return MiReadPageFile(Page
, FILE_FROM_ENTRY(SwapEntry
), OFFSET_FROM_ENTRY(SwapEntry
) - 1);
202 _In_ PFN_NUMBER Page
,
203 _In_ ULONG PageFileIndex
,
204 _In_ ULONG_PTR PageFileOffset
)
206 LARGE_INTEGER file_offset
;
207 IO_STATUS_BLOCK Iosb
;
210 UCHAR MdlBase
[sizeof(MDL
) + sizeof(ULONG
)];
211 PMDL Mdl
= (PMDL
)MdlBase
;
212 PMMPAGING_FILE PagingFile
;
214 DPRINT("MiReadSwapFile\n");
216 if (PageFileOffset
== 0)
218 KeBugCheck(MEMORY_MANAGEMENT
);
219 return(STATUS_UNSUCCESSFUL
);
222 ASSERT(PageFileIndex
< MAX_PAGING_FILES
);
224 PagingFile
= MmPagingFile
[PageFileIndex
];
226 if (PagingFile
->FileObject
== NULL
|| PagingFile
->FileObject
->DeviceObject
== NULL
)
228 DPRINT1("Bad paging file %u\n", PageFileIndex
);
229 KeBugCheck(MEMORY_MANAGEMENT
);
232 MmInitializeMdl(Mdl
, NULL
, PAGE_SIZE
);
233 MmBuildMdlFromPages(Mdl
, &Page
);
234 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
236 file_offset
.QuadPart
= PageFileOffset
* PAGE_SIZE
;
238 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
239 Status
= IoPageRead(PagingFile
->FileObject
,
244 if (Status
== STATUS_PENDING
)
246 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
247 Status
= Iosb
.Status
;
249 if (Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
)
251 MmUnmapLockedPages (Mdl
->MappedSystemVa
, Mdl
);
259 MmInitPagingFile(VOID
)
263 KeInitializeGuardedMutex(&MmPageFileCreationLock
);
267 MiReservedSwapPages
= 0;
269 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
271 MmPagingFile
[i
] = NULL
;
273 MmNumberOfPagingFiles
= 0;
278 MmFreeSwapPage(SWAPENTRY Entry
)
282 PMMPAGING_FILE PagingFile
;
284 i
= FILE_FROM_ENTRY(Entry
);
285 off
= OFFSET_FROM_ENTRY(Entry
) - 1;
287 KeAcquireGuardedMutex(&MmPageFileCreationLock
);
289 PagingFile
= MmPagingFile
[i
];
290 if (PagingFile
== NULL
)
292 KeBugCheck(MEMORY_MANAGEMENT
);
295 RtlClearBit(PagingFile
->Bitmap
, off
>> 5);
297 PagingFile
->FreeSpace
++;
298 PagingFile
->CurrentUsage
--;
303 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
308 MmAllocSwapPage(VOID
)
314 KeAcquireGuardedMutex(&MmPageFileCreationLock
);
316 if (MiFreeSwapPages
== 0)
318 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
322 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
324 if (MmPagingFile
[i
] != NULL
&&
325 MmPagingFile
[i
]->FreeSpace
>= 1)
327 off
= RtlFindClearBitsAndSet(MmPagingFile
[i
]->Bitmap
, 1, 0);
328 if (off
== 0xFFFFFFFF)
330 KeBugCheck(MEMORY_MANAGEMENT
);
331 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
332 return(STATUS_UNSUCCESSFUL
);
336 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
338 entry
= ENTRY_FROM_FILE_OFFSET(i
, off
+ 1);
343 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
344 KeBugCheck(MEMORY_MANAGEMENT
);
349 NtCreatePagingFile(IN PUNICODE_STRING FileName
,
350 IN PLARGE_INTEGER MinimumSize
,
351 IN PLARGE_INTEGER MaximumSize
,
355 OBJECT_ATTRIBUTES ObjectAttributes
;
357 IO_STATUS_BLOCK IoStatus
;
358 PFILE_OBJECT FileObject
;
359 PMMPAGING_FILE PagingFile
;
362 KPROCESSOR_MODE PreviousMode
;
363 UNICODE_STRING PageFileName
;
364 LARGE_INTEGER SafeMinimumSize
, SafeMaximumSize
, AllocationSize
;
365 FILE_FS_DEVICE_INFORMATION FsDeviceInfo
;
366 SECURITY_DESCRIPTOR SecurityDescriptor
;
369 DEVICE_TYPE DeviceType
;
373 DPRINT("NtCreatePagingFile(FileName: '%wZ', MinimumSize: %I64d, MaximumSize: %I64d)\n",
374 FileName
, MinimumSize
->QuadPart
, MaximumSize
->QuadPart
);
376 if (MmNumberOfPagingFiles
>= MAX_PAGING_FILES
)
378 return STATUS_TOO_MANY_PAGING_FILES
;
381 PreviousMode
= ExGetPreviousMode();
383 if (PreviousMode
!= KernelMode
)
385 if (SeSinglePrivilegeCheck(SeCreatePagefilePrivilege
, PreviousMode
) != TRUE
)
387 return STATUS_PRIVILEGE_NOT_HELD
;
392 SafeMinimumSize
= ProbeForReadLargeInteger(MinimumSize
);
393 SafeMaximumSize
= ProbeForReadLargeInteger(MaximumSize
);
394 PageFileName
= ProbeForReadUnicodeString(FileName
);
396 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
398 /* Return the exception code */
399 _SEH2_YIELD(return _SEH2_GetExceptionCode());
405 SafeMinimumSize
= *MinimumSize
;
406 SafeMaximumSize
= *MaximumSize
;
407 PageFileName
= *FileName
;
411 * Pagefiles can't be larger than 4GB and of course
412 * the minimum should be smaller than the maximum.
414 // TODO: Actually validate the lower bound of these sizes!
415 if (0 != SafeMinimumSize
.u
.HighPart
)
417 return STATUS_INVALID_PARAMETER_2
;
419 if (0 != SafeMaximumSize
.u
.HighPart
)
421 return STATUS_INVALID_PARAMETER_3
;
423 if (SafeMaximumSize
.u
.LowPart
< SafeMinimumSize
.u
.LowPart
)
425 return STATUS_INVALID_PARAMETER_MIX
;
428 /* Validate the name length */
429 if ((PageFileName
.Length
== 0) ||
430 (PageFileName
.Length
> 128 * sizeof(WCHAR
)))
432 return STATUS_OBJECT_NAME_INVALID
;
435 /* We don't care about any potential UNICODE_NULL */
436 PageFileName
.MaximumLength
= PageFileName
.Length
;
437 /* Allocate a buffer to keep the name copy */
438 Buffer
= ExAllocatePoolWithTag(PagedPool
, PageFileName
.Length
, TAG_MM
);
441 return STATUS_INSUFFICIENT_RESOURCES
;
445 if (PreviousMode
!= KernelMode
)
449 ProbeForRead(PageFileName
.Buffer
, PageFileName
.Length
, sizeof(WCHAR
));
450 RtlCopyMemory(Buffer
, PageFileName
.Buffer
, PageFileName
.Length
);
452 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
454 ExFreePoolWithTag(Buffer
, TAG_MM
);
456 /* Return the exception code */
457 _SEH2_YIELD(return _SEH2_GetExceptionCode());
463 RtlCopyMemory(Buffer
, PageFileName
.Buffer
, PageFileName
.Length
);
466 /* Replace caller's buffer with ours */
467 PageFileName
.Buffer
= Buffer
;
469 /* Create the security descriptor for the page file */
470 Status
= RtlCreateSecurityDescriptor(&SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
471 if (!NT_SUCCESS(Status
))
473 ExFreePoolWithTag(Buffer
, TAG_MM
);
477 /* Create the DACL: we will only allow two SIDs */
478 Count
= sizeof(ACL
) + (sizeof(ACE
) + RtlLengthSid(SeLocalSystemSid
)) +
479 (sizeof(ACE
) + RtlLengthSid(SeAliasAdminsSid
));
480 Dacl
= ExAllocatePoolWithTag(PagedPool
, Count
, 'lcaD');
483 ExFreePoolWithTag(Buffer
, TAG_MM
);
484 return STATUS_INSUFFICIENT_RESOURCES
;
487 /* Initialize the DACL */
488 Status
= RtlCreateAcl(Dacl
, Count
, ACL_REVISION
);
489 if (!NT_SUCCESS(Status
))
491 ExFreePoolWithTag(Dacl
, 'lcaD');
492 ExFreePoolWithTag(Buffer
, TAG_MM
);
496 /* Grant full access to admins */
497 Status
= RtlAddAccessAllowedAce(Dacl
, ACL_REVISION
, FILE_ALL_ACCESS
, SeAliasAdminsSid
);
498 if (!NT_SUCCESS(Status
))
500 ExFreePoolWithTag(Dacl
, 'lcaD');
501 ExFreePoolWithTag(Buffer
, TAG_MM
);
505 /* Grant full access to SYSTEM */
506 Status
= RtlAddAccessAllowedAce(Dacl
, ACL_REVISION
, FILE_ALL_ACCESS
, SeLocalSystemSid
);
507 if (!NT_SUCCESS(Status
))
509 ExFreePoolWithTag(Dacl
, 'lcaD');
510 ExFreePoolWithTag(Buffer
, TAG_MM
);
514 /* Attach the DACL to the security descriptor */
515 Status
= RtlSetDaclSecurityDescriptor(&SecurityDescriptor
, TRUE
, Dacl
, FALSE
);
516 if (!NT_SUCCESS(Status
))
518 ExFreePoolWithTag(Dacl
, 'lcaD');
519 ExFreePoolWithTag(Buffer
, TAG_MM
);
523 InitializeObjectAttributes(&ObjectAttributes
,
527 &SecurityDescriptor
);
529 /* Make sure we can at least store a complete page:
530 * If we have 2048 BytesPerAllocationUnit (FAT16 < 128MB) there is
531 * a problem if the paging file is fragmented. Suppose the first cluster
532 * of the paging file is cluster 3042 but cluster 3043 is NOT part of the
533 * paging file but of another file. We can't write a complete page (4096
534 * bytes) to the physical location of cluster 3042 then. */
535 AllocationSize
.QuadPart
= SafeMinimumSize
.QuadPart
+ PAGE_SIZE
;
537 /* First, attempt to replace the page file, if existing */
538 Status
= IoCreateFile(&FileHandle
,
539 SYNCHRONIZE
| WRITE_DAC
| FILE_READ_DATA
| FILE_WRITE_DATA
,
543 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
546 FILE_DELETE_ON_CLOSE
| FILE_NO_COMPRESSION
| FILE_NO_INTERMEDIATE_BUFFERING
,
551 SL_OPEN_PAGING_FILE
| IO_NO_PARAMETER_CHECKING
);
552 /* If we failed, relax a bit constraints, someone may be already holding the
553 * the file, so share write, don't attempt to replace and don't delete on close
554 * (basically, don't do anything conflicting)
555 * This can happen if the caller attempts to extend a page file.
557 if (!NT_SUCCESS(Status
))
561 Status
= IoCreateFile(&FileHandle
,
562 SYNCHRONIZE
| FILE_WRITE_DATA
,
566 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
567 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
569 FILE_NO_COMPRESSION
| FILE_NO_INTERMEDIATE_BUFFERING
,
574 SL_OPEN_PAGING_FILE
| IO_NO_PARAMETER_CHECKING
);
575 if (!NT_SUCCESS(Status
))
577 ExFreePoolWithTag(Dacl
, 'lcaD');
578 ExFreePoolWithTag(Buffer
, TAG_MM
);
582 /* We opened it! Check we are that "someone" ;-)
583 * First, get the opened file object.
585 Status
= ObReferenceObjectByHandle(FileHandle
,
586 FILE_READ_DATA
| FILE_WRITE_DATA
,
591 if (!NT_SUCCESS(Status
))
594 ExFreePoolWithTag(Dacl
, 'lcaD');
595 ExFreePoolWithTag(Buffer
, TAG_MM
);
599 /* Find if it matches a previous page file */
602 /* FIXME: should be calling unsafe instead,
603 * we should already be in a guarded region
605 KeAcquireGuardedMutex(&MmPageFileCreationLock
);
606 if (MmNumberOfPagingFiles
> 0)
610 while (MmPagingFile
[i
]->FileObject
->SectionObjectPointer
!= FileObject
->SectionObjectPointer
)
613 if (i
>= MmNumberOfPagingFiles
)
619 /* This is the matching page file */
620 PagingFile
= MmPagingFile
[i
];
623 /* If we didn't find the page file, fail */
624 if (PagingFile
== NULL
)
626 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
627 ObDereferenceObject(FileObject
);
629 ExFreePoolWithTag(Dacl
, 'lcaD');
630 ExFreePoolWithTag(Buffer
, TAG_MM
);
631 return STATUS_NOT_FOUND
;
634 /* Don't allow page file shrinking */
635 if (PagingFile
->MinimumSize
> (SafeMinimumSize
.QuadPart
>> PAGE_SHIFT
))
637 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
638 ObDereferenceObject(FileObject
);
640 ExFreePoolWithTag(Dacl
, 'lcaD');
641 ExFreePoolWithTag(Buffer
, TAG_MM
);
642 return STATUS_INVALID_PARAMETER_2
;
645 if ((SafeMaximumSize
.QuadPart
>> PAGE_SHIFT
) < PagingFile
->MaximumSize
)
647 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
648 ObDereferenceObject(FileObject
);
650 ExFreePoolWithTag(Dacl
, 'lcaD');
651 ExFreePoolWithTag(Buffer
, TAG_MM
);
652 return STATUS_INVALID_PARAMETER_3
;
655 /* FIXME: implement parameters checking and page file extension */
658 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
659 ObDereferenceObject(FileObject
);
661 ExFreePoolWithTag(Dacl
, 'lcaD');
662 ExFreePoolWithTag(Buffer
, TAG_MM
);
663 return STATUS_NOT_IMPLEMENTED
;
666 if (!NT_SUCCESS(Status
))
668 DPRINT1("Failed creating page file: %lx\n", Status
);
669 ExFreePoolWithTag(Dacl
, 'lcaD');
670 ExFreePoolWithTag(Buffer
, TAG_MM
);
674 /* Set the security descriptor */
675 if (NT_SUCCESS(IoStatus
.Status
))
677 Status
= ZwSetSecurityObject(FileHandle
, DACL_SECURITY_INFORMATION
, &SecurityDescriptor
);
678 if (!NT_SUCCESS(Status
))
680 ExFreePoolWithTag(Dacl
, 'lcaD');
682 ExFreePoolWithTag(Buffer
, TAG_MM
);
687 /* DACL is no longer needed, free it */
688 ExFreePoolWithTag(Dacl
, 'lcaD');
690 /* FIXME: To enable once page file managment is moved to ARM3 */
692 /* Check we won't overflow commit limit with the page file */
693 if (MmTotalCommitLimitMaximum
+ (SafeMaximumSize
.QuadPart
>> PAGE_SHIFT
) <= MmTotalCommitLimitMaximum
)
696 ExFreePoolWithTag(Buffer
, TAG_MM
);
697 return STATUS_INVALID_PARAMETER_3
;
701 /* Set its end of file to minimal size */
702 Status
= ZwSetInformationFile(FileHandle
,
705 sizeof(LARGE_INTEGER
),
706 FileEndOfFileInformation
);
707 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(IoStatus
.Status
))
710 ExFreePoolWithTag(Buffer
, TAG_MM
);
714 Status
= ObReferenceObjectByHandle(FileHandle
,
715 FILE_READ_DATA
| FILE_WRITE_DATA
,
720 if (!NT_SUCCESS(Status
))
723 ExFreePoolWithTag(Buffer
, TAG_MM
);
727 /* Only allow page file on a few device types */
728 DeviceType
= IoGetRelatedDeviceObject(FileObject
)->DeviceType
;
729 if (DeviceType
!= FILE_DEVICE_DISK_FILE_SYSTEM
&& DeviceType
!= FILE_DEVICE_NETWORK_FILE_SYSTEM
&&
730 DeviceType
!= FILE_DEVICE_DFS_VOLUME
&& DeviceType
!= FILE_DEVICE_DFS_FILE_SYSTEM
)
732 ObDereferenceObject(FileObject
);
734 ExFreePoolWithTag(Buffer
, TAG_MM
);
738 /* Deny page file creation on a floppy disk */
739 FsDeviceInfo
.Characteristics
= 0;
740 IoQueryVolumeInformation(FileObject
, FileFsDeviceInformation
, sizeof(FsDeviceInfo
), &FsDeviceInfo
, &Count
);
741 if (BooleanFlagOn(FsDeviceInfo
.Characteristics
, FILE_FLOPPY_DISKETTE
))
743 ObDereferenceObject(FileObject
);
745 ExFreePoolWithTag(Buffer
, TAG_MM
);
746 return STATUS_FLOPPY_VOLUME
;
749 PagingFile
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(*PagingFile
), TAG_MM
);
750 if (PagingFile
== NULL
)
752 ObDereferenceObject(FileObject
);
754 ExFreePoolWithTag(Buffer
, TAG_MM
);
755 return STATUS_INSUFFICIENT_RESOURCES
;
758 RtlZeroMemory(PagingFile
, sizeof(*PagingFile
));
760 PagingFile
->FileHandle
= FileHandle
;
761 PagingFile
->FileObject
= FileObject
;
762 PagingFile
->MaximumSize
= (SafeMaximumSize
.QuadPart
>> PAGE_SHIFT
);
763 PagingFile
->Size
= (SafeMinimumSize
.QuadPart
>> PAGE_SHIFT
);
764 PagingFile
->MinimumSize
= (SafeMinimumSize
.QuadPart
>> PAGE_SHIFT
);
765 /* First page is never used: it's the header
768 PagingFile
->FreeSpace
= (ULONG
)(SafeMinimumSize
.QuadPart
/ PAGE_SIZE
) - 1;
769 PagingFile
->CurrentUsage
= 0;
770 PagingFile
->PageFileName
= PageFileName
;
771 ASSERT(PagingFile
->Size
== PagingFile
->FreeSpace
+ PagingFile
->CurrentUsage
+ 1);
773 AllocMapSize
= sizeof(RTL_BITMAP
) + (((PagingFile
->MaximumSize
+ 31) / 32) * sizeof(ULONG
));
774 PagingFile
->Bitmap
= ExAllocatePoolWithTag(NonPagedPool
,
777 if (PagingFile
->Bitmap
== NULL
)
779 ExFreePoolWithTag(PagingFile
, TAG_MM
);
780 ObDereferenceObject(FileObject
);
782 ExFreePoolWithTag(Buffer
, TAG_MM
);
783 return STATUS_INSUFFICIENT_RESOURCES
;
786 RtlInitializeBitMap(PagingFile
->Bitmap
,
787 (PULONG
)(PagingFile
->Bitmap
+ 1),
788 (ULONG
)(PagingFile
->MaximumSize
));
789 RtlClearAllBits(PagingFile
->Bitmap
);
791 /* FIXME: should be calling unsafe instead,
792 * we should already be in a guarded region
794 KeAcquireGuardedMutex(&MmPageFileCreationLock
);
795 ASSERT(MmPagingFile
[MmNumberOfPagingFiles
] == NULL
);
796 MmPagingFile
[MmNumberOfPagingFiles
] = PagingFile
;
797 MmNumberOfPagingFiles
++;
798 MiFreeSwapPages
= MiFreeSwapPages
+ PagingFile
->FreeSpace
;
799 KeReleaseGuardedMutex(&MmPageFileCreationLock
);
801 MmSwapSpaceMessage
= FALSE
;
803 if (!MmSystemPageFileLocated
&& BooleanFlagOn(FileObject
->DeviceObject
->Flags
, DO_SYSTEM_BOOT_PARTITION
))
805 MmSystemPageFileLocated
= IoInitializeCrashDump(FileHandle
);
808 return STATUS_SUCCESS
;