985557825f9dc564902ea61f4ce19e8ae2f2c18d
[reactos.git] / ntoskrnl / mm / pagefile.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /*
20 * PROJECT: ReactOS kernel
21 * FILE: ntoskrnl/mm/pagefile.c
22 * PURPOSE: Paging file functions
23 * PROGRAMMER: David Welch (welch@mcmail.com)
24 * Pierre Schweitzer
25 * UPDATE HISTORY:
26 * Created 22/05/98
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include <ntoskrnl.h>
32 #define NDEBUG
33 #include <debug.h>
34
35 #if defined (ALLOC_PRAGMA)
36 #pragma alloc_text(INIT, MmInitPagingFile)
37 #endif
38
39 /* GLOBALS *******************************************************************/
40
41 #define PAIRS_PER_RUN (1024)
42
43 /* List of paging files, both used and free */
44 PMMPAGING_FILE MmPagingFile[MAX_PAGING_FILES];
45
46 /* Lock for examining the list of paging files */
47 KGUARDED_MUTEX MmPageFileCreationLock;
48
49 /* Number of paging files */
50 ULONG MmNumberOfPagingFiles;
51
52 /* Number of pages that are available for swapping */
53 PFN_COUNT MiFreeSwapPages;
54
55 /* Number of pages that have been allocated for swapping */
56 PFN_COUNT MiUsedSwapPages;
57
58 BOOLEAN MmZeroPageFile;
59
60 /*
61 * Number of pages that have been reserved for swapping but not yet allocated
62 */
63 static PFN_COUNT MiReservedSwapPages;
64
65 /*
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.
69 */
70 #define MM_PAGEFILE_COMMIT_RATIO (1)
71
72 /*
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
76 * ratio of one.
77 */
78 #define MM_PAGEFILE_COMMIT_GRACE (256)
79
80 /*
81 * Translate between a swap entry and a file and offset pair.
82 */
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)
86
87 /* Make sure there can be only 16 paging files */
88 C_ASSERT(FILE_FROM_ENTRY(0xffffffff) < MAX_PAGING_FILES);
89
90 static BOOLEAN MmSwapSpaceMessage = FALSE;
91
92 /* FUNCTIONS *****************************************************************/
93
94 VOID
95 NTAPI
96 MmBuildMdlFromPages(PMDL Mdl, PPFN_NUMBER Pages)
97 {
98 memcpy(Mdl + 1, Pages, sizeof(PFN_NUMBER) * (PAGE_ROUND_UP(Mdl->ByteOffset+Mdl->ByteCount)/PAGE_SIZE));
99
100 /* FIXME: this flag should be set by the caller perhaps? */
101 Mdl->MdlFlags |= MDL_IO_PAGE_READ;
102 }
103
104
105 BOOLEAN
106 NTAPI
107 MmIsFileObjectAPagingFile(PFILE_OBJECT FileObject)
108 {
109 ULONG i;
110
111 /* Loop through all the paging files */
112 for (i = 0; i < MmNumberOfPagingFiles; i++)
113 {
114 /* Check if this is one of them */
115 if (MmPagingFile[i]->FileObject == FileObject) return TRUE;
116 }
117
118 /* Nothing found */
119 return FALSE;
120 }
121
122 VOID
123 NTAPI
124 MmShowOutOfSpaceMessagePagingFile(VOID)
125 {
126 if (!MmSwapSpaceMessage)
127 {
128 DPRINT1("MM: Out of swap space.\n");
129 MmSwapSpaceMessage = TRUE;
130 }
131 }
132
133 NTSTATUS
134 NTAPI
135 MmWriteToSwapPage(SWAPENTRY SwapEntry, PFN_NUMBER Page)
136 {
137 ULONG i;
138 ULONG_PTR offset;
139 LARGE_INTEGER file_offset;
140 IO_STATUS_BLOCK Iosb;
141 NTSTATUS Status;
142 KEVENT Event;
143 UCHAR MdlBase[sizeof(MDL) + sizeof(ULONG)];
144 PMDL Mdl = (PMDL)MdlBase;
145
146 DPRINT("MmWriteToSwapPage\n");
147
148 if (SwapEntry == 0)
149 {
150 KeBugCheck(MEMORY_MANAGEMENT);
151 return(STATUS_UNSUCCESSFUL);
152 }
153
154 i = FILE_FROM_ENTRY(SwapEntry);
155 offset = OFFSET_FROM_ENTRY(SwapEntry) - 1;
156
157 if (MmPagingFile[i]->FileObject == NULL ||
158 MmPagingFile[i]->FileObject->DeviceObject == NULL)
159 {
160 DPRINT1("Bad paging file 0x%.8X\n", SwapEntry);
161 KeBugCheck(MEMORY_MANAGEMENT);
162 }
163
164 MmInitializeMdl(Mdl, NULL, PAGE_SIZE);
165 MmBuildMdlFromPages(Mdl, &Page);
166 Mdl->MdlFlags |= MDL_PAGES_LOCKED;
167
168 file_offset.QuadPart = offset * PAGE_SIZE;
169
170 KeInitializeEvent(&Event, NotificationEvent, FALSE);
171 Status = IoSynchronousPageWrite(MmPagingFile[i]->FileObject,
172 Mdl,
173 &file_offset,
174 &Event,
175 &Iosb);
176 if (Status == STATUS_PENDING)
177 {
178 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
179 Status = Iosb.Status;
180 }
181
182 if (Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA)
183 {
184 MmUnmapLockedPages (Mdl->MappedSystemVa, Mdl);
185 }
186 return(Status);
187 }
188
189
190 NTSTATUS
191 NTAPI
192 MmReadFromSwapPage(SWAPENTRY SwapEntry, PFN_NUMBER Page)
193 {
194 return MiReadPageFile(Page, FILE_FROM_ENTRY(SwapEntry), OFFSET_FROM_ENTRY(SwapEntry) - 1);
195 }
196
197 NTSTATUS
198 NTAPI
199 MiReadPageFile(
200 _In_ PFN_NUMBER Page,
201 _In_ ULONG PageFileIndex,
202 _In_ ULONG_PTR PageFileOffset)
203 {
204 LARGE_INTEGER file_offset;
205 IO_STATUS_BLOCK Iosb;
206 NTSTATUS Status;
207 KEVENT Event;
208 UCHAR MdlBase[sizeof(MDL) + sizeof(ULONG)];
209 PMDL Mdl = (PMDL)MdlBase;
210 PMMPAGING_FILE PagingFile;
211
212 DPRINT("MiReadSwapFile\n");
213
214 if (PageFileOffset == 0)
215 {
216 KeBugCheck(MEMORY_MANAGEMENT);
217 return(STATUS_UNSUCCESSFUL);
218 }
219
220 ASSERT(PageFileIndex < MAX_PAGING_FILES);
221
222 PagingFile = MmPagingFile[PageFileIndex];
223
224 if (PagingFile->FileObject == NULL || PagingFile->FileObject->DeviceObject == NULL)
225 {
226 DPRINT1("Bad paging file %u\n", PageFileIndex);
227 KeBugCheck(MEMORY_MANAGEMENT);
228 }
229
230 MmInitializeMdl(Mdl, NULL, PAGE_SIZE);
231 MmBuildMdlFromPages(Mdl, &Page);
232 Mdl->MdlFlags |= MDL_PAGES_LOCKED;
233
234 file_offset.QuadPart = PageFileOffset * PAGE_SIZE;
235
236 KeInitializeEvent(&Event, NotificationEvent, FALSE);
237 Status = IoPageRead(PagingFile->FileObject,
238 Mdl,
239 &file_offset,
240 &Event,
241 &Iosb);
242 if (Status == STATUS_PENDING)
243 {
244 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
245 Status = Iosb.Status;
246 }
247 if (Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA)
248 {
249 MmUnmapLockedPages (Mdl->MappedSystemVa, Mdl);
250 }
251 return(Status);
252 }
253
254 VOID
255 INIT_FUNCTION
256 NTAPI
257 MmInitPagingFile(VOID)
258 {
259 ULONG i;
260
261 KeInitializeGuardedMutex(&MmPageFileCreationLock);
262
263 MiFreeSwapPages = 0;
264 MiUsedSwapPages = 0;
265 MiReservedSwapPages = 0;
266
267 for (i = 0; i < MAX_PAGING_FILES; i++)
268 {
269 MmPagingFile[i] = NULL;
270 }
271 MmNumberOfPagingFiles = 0;
272 }
273
274 VOID
275 NTAPI
276 MmFreeSwapPage(SWAPENTRY Entry)
277 {
278 ULONG i;
279 ULONG_PTR off;
280 PMMPAGING_FILE PagingFile;
281
282 i = FILE_FROM_ENTRY(Entry);
283 off = OFFSET_FROM_ENTRY(Entry) - 1;
284
285 KeAcquireGuardedMutex(&MmPageFileCreationLock);
286
287 PagingFile = MmPagingFile[i];
288 if (PagingFile == NULL)
289 {
290 KeBugCheck(MEMORY_MANAGEMENT);
291 }
292
293 RtlClearBit(PagingFile->Bitmap, off >> 5);
294
295 PagingFile->FreeSpace++;
296 PagingFile->CurrentUsage--;
297
298 MiFreeSwapPages++;
299 MiUsedSwapPages--;
300
301 KeReleaseGuardedMutex(&MmPageFileCreationLock);
302 }
303
304 SWAPENTRY
305 NTAPI
306 MmAllocSwapPage(VOID)
307 {
308 ULONG i;
309 ULONG off;
310 SWAPENTRY entry;
311
312 KeAcquireGuardedMutex(&MmPageFileCreationLock);
313
314 if (MiFreeSwapPages == 0)
315 {
316 KeReleaseGuardedMutex(&MmPageFileCreationLock);
317 return(0);
318 }
319
320 for (i = 0; i < MAX_PAGING_FILES; i++)
321 {
322 if (MmPagingFile[i] != NULL &&
323 MmPagingFile[i]->FreeSpace >= 1)
324 {
325 off = RtlFindClearBitsAndSet(MmPagingFile[i]->Bitmap, 1, 0);
326 if (off == 0xFFFFFFFF)
327 {
328 KeBugCheck(MEMORY_MANAGEMENT);
329 KeReleaseGuardedMutex(&MmPageFileCreationLock);
330 return(STATUS_UNSUCCESSFUL);
331 }
332 MiUsedSwapPages++;
333 MiFreeSwapPages--;
334 KeReleaseGuardedMutex(&MmPageFileCreationLock);
335
336 entry = ENTRY_FROM_FILE_OFFSET(i, off + 1);
337 return(entry);
338 }
339 }
340
341 KeReleaseGuardedMutex(&MmPageFileCreationLock);
342 KeBugCheck(MEMORY_MANAGEMENT);
343 return(0);
344 }
345
346 NTSTATUS NTAPI
347 NtCreatePagingFile(IN PUNICODE_STRING FileName,
348 IN PLARGE_INTEGER MinimumSize,
349 IN PLARGE_INTEGER MaximumSize,
350 IN ULONG Reserved)
351 {
352 NTSTATUS Status;
353 OBJECT_ATTRIBUTES ObjectAttributes;
354 HANDLE FileHandle;
355 IO_STATUS_BLOCK IoStatus;
356 PFILE_OBJECT FileObject;
357 PMMPAGING_FILE PagingFile;
358 ULONG AllocMapSize;
359 ULONG Count;
360 KPROCESSOR_MODE PreviousMode;
361 UNICODE_STRING PageFileName;
362 LARGE_INTEGER SafeMinimumSize, SafeMaximumSize, AllocationSize;
363 FILE_FS_DEVICE_INFORMATION FsDeviceInfo;
364 SECURITY_DESCRIPTOR SecurityDescriptor;
365 PACL Dacl;
366 PWSTR Buffer;
367
368 DPRINT("NtCreatePagingFile(FileName %wZ, MinimumSize %I64d)\n",
369 FileName, MinimumSize->QuadPart);
370
371 PAGED_CODE();
372
373 if (MmNumberOfPagingFiles >= MAX_PAGING_FILES)
374 {
375 return STATUS_TOO_MANY_PAGING_FILES;
376 }
377
378 PreviousMode = ExGetPreviousMode();
379
380 if (PreviousMode != KernelMode)
381 {
382 if (SeSinglePrivilegeCheck(SeCreatePagefilePrivilege, PreviousMode) != TRUE)
383 {
384 return STATUS_PRIVILEGE_NOT_HELD;
385 }
386
387 _SEH2_TRY
388 {
389 SafeMinimumSize = ProbeForReadLargeInteger(MinimumSize);
390 SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
391
392 PageFileName.Length = FileName->Length;
393 PageFileName.MaximumLength = FileName->MaximumLength;
394 PageFileName.Buffer = FileName->Buffer;
395 }
396 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
397 {
398 /* Return the exception code */
399 _SEH2_YIELD(return _SEH2_GetExceptionCode());
400 }
401 _SEH2_END;
402 }
403 else
404 {
405 SafeMinimumSize = *MinimumSize;
406 SafeMaximumSize = *MaximumSize;
407
408 PageFileName.Length = FileName->Length;
409 PageFileName.MaximumLength = FileName->MaximumLength;
410 PageFileName.Buffer = FileName->Buffer;
411 }
412
413 /* Pagefiles can't be larger than 4GB and ofcourse the minimum should be
414 smaller than the maximum */
415 if (0 != SafeMinimumSize.u.HighPart)
416 {
417 return STATUS_INVALID_PARAMETER_2;
418 }
419 if (0 != SafeMaximumSize.u.HighPart)
420 {
421 return STATUS_INVALID_PARAMETER_3;
422 }
423 if (SafeMaximumSize.u.LowPart < SafeMinimumSize.u.LowPart)
424 {
425 return STATUS_INVALID_PARAMETER_MIX;
426 }
427
428 /* Validate name length */
429 if (PageFileName.Length > 128 * sizeof(WCHAR))
430 {
431 return STATUS_OBJECT_NAME_INVALID;
432 }
433
434 /* We won't care about any potential UNICODE_NULL */
435 PageFileName.MaximumLength = PageFileName.Length;
436 /* Allocate a buffer to keep name copy */
437 Buffer = ExAllocatePoolWithTag(PagedPool, PageFileName.Length, TAG_MM);
438 if (Buffer == NULL)
439 {
440 return STATUS_INSUFFICIENT_RESOURCES;
441 }
442
443 /* Copy name */
444 if (PreviousMode != KernelMode)
445 {
446 _SEH2_TRY
447 {
448 if (PageFileName.Length != 0)
449 {
450 ProbeForRead(PageFileName.Buffer, PageFileName.Length, sizeof(WCHAR));
451 }
452
453 RtlCopyMemory(Buffer, PageFileName.Buffer, PageFileName.Length);
454 }
455 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
456 {
457 ExFreePoolWithTag(Buffer, TAG_MM);
458
459 /* Return the exception code */
460 _SEH2_YIELD(return _SEH2_GetExceptionCode());
461 }
462 _SEH2_END;
463 }
464 else
465 {
466 RtlCopyMemory(Buffer, PageFileName.Buffer, PageFileName.Length);
467 }
468
469 /* Erase caller's buffer with ours */
470 PageFileName.Buffer = Buffer;
471
472 /* Create the security descriptor for the page file */
473 Status = RtlCreateSecurityDescriptor(&SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
474 if (!NT_SUCCESS(Status))
475 {
476 ExFreePoolWithTag(Buffer, TAG_MM);
477 return Status;
478 }
479
480 /* Create the DACL: we will only allow two SIDs */
481 Count = sizeof(ACL) + (sizeof(ACE) + RtlLengthSid(SeLocalSystemSid)) +
482 (sizeof(ACE) + RtlLengthSid(SeAliasAdminsSid));
483 Dacl = ExAllocatePoolWithTag(PagedPool, Count, 'lcaD');
484 if (Dacl == NULL)
485 {
486 ExFreePoolWithTag(Buffer, TAG_MM);
487 return STATUS_INSUFFICIENT_RESOURCES;
488 }
489
490 /* Initialize the DACL */
491 Status = RtlCreateAcl(Dacl, Count, ACL_REVISION);
492 if (!NT_SUCCESS(Status))
493 {
494 ExFreePoolWithTag(Dacl, 'lcaD');
495 ExFreePoolWithTag(Buffer, TAG_MM);
496 return Status;
497 }
498
499 /* Grant full access to admins */
500 Status = RtlAddAccessAllowedAce(Dacl, ACL_REVISION, FILE_ALL_ACCESS, SeAliasAdminsSid);
501 if (!NT_SUCCESS(Status))
502 {
503 ExFreePoolWithTag(Dacl, 'lcaD');
504 ExFreePoolWithTag(Buffer, TAG_MM);
505 return Status;
506 }
507
508 /* Grant full access to SYSTEM */
509 Status = RtlAddAccessAllowedAce(Dacl, ACL_REVISION, FILE_ALL_ACCESS, SeLocalSystemSid);
510 if (!NT_SUCCESS(Status))
511 {
512 ExFreePoolWithTag(Dacl, 'lcaD');
513 ExFreePoolWithTag(Buffer, TAG_MM);
514 return Status;
515 }
516
517 /* Attach the DACL to the security descriptor */
518 Status = RtlSetDaclSecurityDescriptor(&SecurityDescriptor, TRUE, Dacl, FALSE);
519 if (!NT_SUCCESS(Status))
520 {
521 ExFreePoolWithTag(Dacl, 'lcaD');
522 ExFreePoolWithTag(Buffer, TAG_MM);
523 return Status;
524 }
525
526 InitializeObjectAttributes(&ObjectAttributes,
527 &PageFileName,
528 OBJ_KERNEL_HANDLE,
529 NULL,
530 &SecurityDescriptor);
531
532 /* Make sure we can at least store a complete page:
533 * If we have 2048 BytesPerAllocationUnit (FAT16 < 128MB) there is
534 * a problem if the paging file is fragmented. Suppose the first cluster
535 * of the paging file is cluster 3042 but cluster 3043 is NOT part of the
536 * paging file but of another file. We can't write a complete page (4096
537 * bytes) to the physical location of cluster 3042 then. */
538 AllocationSize.QuadPart = SafeMinimumSize.QuadPart + PAGE_SIZE;
539
540 /* First, attempt to replace the page file, if existing */
541 Status = IoCreateFile(&FileHandle,
542 SYNCHRONIZE | WRITE_DAC | FILE_READ_DATA | FILE_WRITE_DATA,
543 &ObjectAttributes,
544 &IoStatus,
545 &AllocationSize,
546 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
547 FILE_SHARE_WRITE,
548 FILE_SUPERSEDE,
549 FILE_DELETE_ON_CLOSE | FILE_NO_COMPRESSION | FILE_NO_INTERMEDIATE_BUFFERING,
550 NULL,
551 0,
552 CreateFileTypeNone,
553 NULL,
554 SL_OPEN_PAGING_FILE | IO_NO_PARAMETER_CHECKING);
555 /* If we failed, relax a bit constraints, someone may be already holding the
556 * the file, so share write, don't attempt to replace and don't delete on close
557 * (basically, don't do anything conflicting)
558 * This can happen if the caller attempts to extend a page file.
559 */
560 if (!NT_SUCCESS(Status))
561 {
562 ULONG i;
563
564 Status = IoCreateFile(&FileHandle,
565 SYNCHRONIZE | FILE_WRITE_DATA,
566 &ObjectAttributes,
567 &IoStatus,
568 &AllocationSize,
569 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
570 FILE_SHARE_WRITE | FILE_SHARE_READ,
571 FILE_OPEN,
572 FILE_NO_COMPRESSION | FILE_NO_INTERMEDIATE_BUFFERING,
573 NULL,
574 0,
575 CreateFileTypeNone,
576 NULL,
577 SL_OPEN_PAGING_FILE | IO_NO_PARAMETER_CHECKING);
578 if (!NT_SUCCESS(Status))
579 {
580 ExFreePoolWithTag(Dacl, 'lcaD');
581 ExFreePoolWithTag(Buffer, TAG_MM);
582 return Status;
583 }
584
585 /* We opened it! Check we are that "someone" ;-)
586 * First, get the opened file object.
587 */
588 Status = ObReferenceObjectByHandle(FileHandle,
589 FILE_READ_DATA | FILE_WRITE_DATA,
590 IoFileObjectType,
591 KernelMode,
592 (PVOID*)&FileObject,
593 NULL);
594 if (!NT_SUCCESS(Status))
595 {
596 ZwClose(FileHandle);
597 ExFreePoolWithTag(Dacl, 'lcaD');
598 ExFreePoolWithTag(Buffer, TAG_MM);
599 return Status;
600 }
601
602 /* Find if it matches a previous page file */
603 PagingFile = NULL;
604
605 /* FIXME: should be calling unsafe instead,
606 * we should already be in a guarded region
607 */
608 KeAcquireGuardedMutex(&MmPageFileCreationLock);
609 if (MmNumberOfPagingFiles > 0)
610 {
611 i = 0;
612
613 while (MmPagingFile[i]->FileObject->SectionObjectPointer != FileObject->SectionObjectPointer)
614 {
615 ++i;
616 if (i >= MmNumberOfPagingFiles)
617 {
618 break;
619 }
620 }
621
622 /* This is the matching page file */
623 PagingFile = MmPagingFile[i];
624 }
625
626 /* If we didn't find the page file, fail */
627 if (PagingFile == NULL)
628 {
629 KeReleaseGuardedMutex(&MmPageFileCreationLock);
630 ObDereferenceObject(FileObject);
631 ZwClose(FileHandle);
632 ExFreePoolWithTag(Dacl, 'lcaD');
633 ExFreePoolWithTag(Buffer, TAG_MM);
634 return STATUS_NOT_FOUND;
635 }
636
637 /* Don't allow page file shrinking */
638 if (PagingFile->MinimumSize > (SafeMinimumSize.QuadPart >> PAGE_SHIFT))
639 {
640 KeReleaseGuardedMutex(&MmPageFileCreationLock);
641 ObDereferenceObject(FileObject);
642 ZwClose(FileHandle);
643 ExFreePoolWithTag(Dacl, 'lcaD');
644 ExFreePoolWithTag(Buffer, TAG_MM);
645 return STATUS_INVALID_PARAMETER_2;
646 }
647
648 if ((SafeMaximumSize.QuadPart >> PAGE_SHIFT) < PagingFile->MaximumSize)
649 {
650 KeReleaseGuardedMutex(&MmPageFileCreationLock);
651 ObDereferenceObject(FileObject);
652 ZwClose(FileHandle);
653 ExFreePoolWithTag(Dacl, 'lcaD');
654 ExFreePoolWithTag(Buffer, TAG_MM);
655 return STATUS_INVALID_PARAMETER_3;
656 }
657
658 /* FIXME: implement parameters checking and page file extension */
659 UNIMPLEMENTED;
660
661 KeReleaseGuardedMutex(&MmPageFileCreationLock);
662 ObDereferenceObject(FileObject);
663 ZwClose(FileHandle);
664 ExFreePoolWithTag(Dacl, 'lcaD');
665 ExFreePoolWithTag(Buffer, TAG_MM);
666 return STATUS_NOT_IMPLEMENTED;
667 }
668
669 if (!NT_SUCCESS(Status))
670 {
671 DPRINT1("Failed creating page file: %lx\n", Status);
672 ExFreePoolWithTag(Dacl, 'lcaD');
673 ExFreePoolWithTag(Buffer, TAG_MM);
674 return Status;
675 }
676
677 /* Set the security descriptor */
678 if (NT_SUCCESS(IoStatus.Status))
679 {
680 Status = ZwSetSecurityObject(FileHandle, DACL_SECURITY_INFORMATION, &SecurityDescriptor);
681 if (!NT_SUCCESS(Status))
682 {
683 ExFreePoolWithTag(Dacl, 'lcaD');
684 ZwClose(FileHandle);
685 ExFreePoolWithTag(Buffer, TAG_MM);
686 return Status;
687 }
688 }
689
690 /* DACL is no longer needed, free it */
691 ExFreePoolWithTag(Dacl, 'lcaD');
692
693 /* FIXME: To enable once page file managment is moved to ARM3 */
694 #if 0
695 /* Check we won't overflow commit limit with the page file */
696 if (MmTotalCommitLimitMaximum + (SafeMaximumSize.QuadPart >> PAGE_SHIFT) <= MmTotalCommitLimitMaximum)
697 {
698 ZwClose(FileHandle);
699 ExFreePoolWithTag(Buffer, TAG_MM);
700 return STATUS_INVALID_PARAMETER_3;
701 }
702 #endif
703
704 /* Set its end of file to minimal size */
705 Status = ZwSetInformationFile(FileHandle,
706 &IoStatus,
707 &SafeMinimumSize,
708 sizeof(LARGE_INTEGER),
709 FileEndOfFileInformation);
710 if (!NT_SUCCESS(Status) || !NT_SUCCESS(IoStatus.Status))
711 {
712 ZwClose(FileHandle);
713 ExFreePoolWithTag(Buffer, TAG_MM);
714 return Status;
715 }
716
717 Status = ObReferenceObjectByHandle(FileHandle,
718 FILE_READ_DATA | FILE_WRITE_DATA,
719 IoFileObjectType,
720 KernelMode,
721 (PVOID*)&FileObject,
722 NULL);
723 if (!NT_SUCCESS(Status))
724 {
725 ZwClose(FileHandle);
726 ExFreePoolWithTag(Buffer, TAG_MM);
727 return Status;
728 }
729
730 /* Deny page file creation on a floppy disk */
731 FsDeviceInfo.Characteristics = 0;
732 IoQueryVolumeInformation(FileObject, FileFsDeviceInformation, sizeof(FsDeviceInfo), &FsDeviceInfo, &Count);
733 if (BooleanFlagOn(FsDeviceInfo.Characteristics, FILE_FLOPPY_DISKETTE))
734 {
735 ObDereferenceObject(FileObject);
736 ZwClose(FileHandle);
737 ExFreePoolWithTag(Buffer, TAG_MM);
738 return STATUS_FLOPPY_VOLUME;
739 }
740
741 PagingFile = ExAllocatePoolWithTag(NonPagedPool, sizeof(*PagingFile), TAG_MM);
742 if (PagingFile == NULL)
743 {
744 ObDereferenceObject(FileObject);
745 ZwClose(FileHandle);
746 ExFreePoolWithTag(Buffer, TAG_MM);
747 return STATUS_INSUFFICIENT_RESOURCES;
748 }
749
750 RtlZeroMemory(PagingFile, sizeof(*PagingFile));
751
752 PagingFile->FileHandle = FileHandle;
753 PagingFile->FileObject = FileObject;
754 PagingFile->MaximumSize = (SafeMaximumSize.QuadPart >> PAGE_SHIFT);
755 PagingFile->Size = (SafeMinimumSize.QuadPart >> PAGE_SHIFT);
756 PagingFile->MinimumSize = (SafeMinimumSize.QuadPart >> PAGE_SHIFT);
757 /* First page is never used: it's the header
758 * TODO: write it
759 */
760 PagingFile->FreeSpace = (ULONG)(SafeMinimumSize.QuadPart / PAGE_SIZE) - 1;
761 PagingFile->CurrentUsage = 0;
762 PagingFile->PageFileName = PageFileName;
763 ASSERT(PagingFile->Size == PagingFile->FreeSpace + PagingFile->CurrentUsage + 1);
764
765 AllocMapSize = sizeof(RTL_BITMAP) + (((PagingFile->MaximumSize + 31) / 32) * sizeof(ULONG));
766 PagingFile->Bitmap = ExAllocatePoolWithTag(NonPagedPool,
767 AllocMapSize,
768 TAG_MM);
769 if (PagingFile->Bitmap == NULL)
770 {
771 ExFreePoolWithTag(PagingFile, TAG_MM);
772 ObDereferenceObject(FileObject);
773 ZwClose(FileHandle);
774 ExFreePoolWithTag(Buffer, TAG_MM);
775 return STATUS_INSUFFICIENT_RESOURCES;
776 }
777
778 RtlInitializeBitMap(PagingFile->Bitmap,
779 (PULONG)(PagingFile->Bitmap + 1),
780 (ULONG)(PagingFile->MaximumSize));
781 RtlClearAllBits(PagingFile->Bitmap);
782
783 /* FIXME: should be calling unsafe instead,
784 * we should already be in a guarded region
785 */
786 KeAcquireGuardedMutex(&MmPageFileCreationLock);
787 ASSERT(MmPagingFile[MmNumberOfPagingFiles] == NULL);
788 MmPagingFile[MmNumberOfPagingFiles] = PagingFile;
789 MmNumberOfPagingFiles++;
790 MiFreeSwapPages = MiFreeSwapPages + PagingFile->FreeSpace;
791 KeReleaseGuardedMutex(&MmPageFileCreationLock);
792
793 MmSwapSpaceMessage = FALSE;
794
795 return STATUS_SUCCESS;
796 }
797
798 /* EOF */