dba39b76530acad381022ca20d2a639c2601f74e
[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 static KSPIN_LOCK PagingFileListLock;
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 KeInitializeSpinLock(&PagingFileListLock);
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 static ULONG
275 MiAllocPageFromPagingFile(PMMPAGING_FILE PagingFile)
276 {
277 KIRQL oldIrql;
278 ULONG off;
279
280 KeAcquireSpinLock(&PagingFile->AllocMapLock, &oldIrql);
281 off = RtlFindClearBitsAndSet(PagingFile->AllocMap, 1, 0);
282 KeReleaseSpinLock(&PagingFile->AllocMapLock, oldIrql);
283
284 return off;
285 }
286
287 VOID
288 NTAPI
289 MmFreeSwapPage(SWAPENTRY Entry)
290 {
291 ULONG i;
292 ULONG_PTR off;
293 KIRQL oldIrql;
294 PMMPAGING_FILE PagingFile;
295
296 i = FILE_FROM_ENTRY(Entry);
297 off = OFFSET_FROM_ENTRY(Entry) - 1;
298
299 KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
300
301 PagingFile = MmPagingFile[i];
302 if (PagingFile == NULL)
303 {
304 KeBugCheck(MEMORY_MANAGEMENT);
305 }
306 KeAcquireSpinLockAtDpcLevel(&PagingFile->AllocMapLock);
307
308 RtlClearBit(PagingFile->AllocMap, off >> 5);
309
310 PagingFile->FreePages++;
311 PagingFile->UsedPages--;
312
313 MiFreeSwapPages++;
314 MiUsedSwapPages--;
315
316 KeReleaseSpinLockFromDpcLevel(&PagingFile->AllocMapLock);
317 KeReleaseSpinLock(&PagingFileListLock, oldIrql);
318 }
319
320 SWAPENTRY
321 NTAPI
322 MmAllocSwapPage(VOID)
323 {
324 KIRQL oldIrql;
325 ULONG i;
326 ULONG off;
327 SWAPENTRY entry;
328
329 KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
330
331 if (MiFreeSwapPages == 0)
332 {
333 KeReleaseSpinLock(&PagingFileListLock, oldIrql);
334 return(0);
335 }
336
337 for (i = 0; i < MAX_PAGING_FILES; i++)
338 {
339 if (MmPagingFile[i] != NULL &&
340 MmPagingFile[i]->FreePages >= 1)
341 {
342 off = MiAllocPageFromPagingFile(MmPagingFile[i]);
343 if (off == 0xFFFFFFFF)
344 {
345 KeBugCheck(MEMORY_MANAGEMENT);
346 KeReleaseSpinLock(&PagingFileListLock, oldIrql);
347 return(STATUS_UNSUCCESSFUL);
348 }
349 MiUsedSwapPages++;
350 MiFreeSwapPages--;
351 KeReleaseSpinLock(&PagingFileListLock, oldIrql);
352
353 entry = ENTRY_FROM_FILE_OFFSET(i, off + 1);
354 return(entry);
355 }
356 }
357
358 KeReleaseSpinLock(&PagingFileListLock, oldIrql);
359 KeBugCheck(MEMORY_MANAGEMENT);
360 return(0);
361 }
362
363 NTSTATUS NTAPI
364 NtCreatePagingFile(IN PUNICODE_STRING FileName,
365 IN PLARGE_INTEGER InitialSize,
366 IN PLARGE_INTEGER MaximumSize,
367 IN ULONG Reserved)
368 {
369 NTSTATUS Status;
370 OBJECT_ATTRIBUTES ObjectAttributes;
371 HANDLE FileHandle;
372 IO_STATUS_BLOCK IoStatus;
373 PFILE_OBJECT FileObject;
374 PMMPAGING_FILE PagingFile;
375 KIRQL oldIrql;
376 ULONG AllocMapSize;
377 ULONG Count;
378 KPROCESSOR_MODE PreviousMode;
379 UNICODE_STRING PageFileName;
380 LARGE_INTEGER SafeInitialSize, SafeMaximumSize, AllocationSize;
381 FILE_FS_DEVICE_INFORMATION FsDeviceInfo;
382 SECURITY_DESCRIPTOR SecurityDescriptor;
383 PACL Dacl;
384 PWSTR Buffer;
385
386 DPRINT("NtCreatePagingFile(FileName %wZ, InitialSize %I64d)\n",
387 FileName, InitialSize->QuadPart);
388
389 if (MmNumberOfPagingFiles >= MAX_PAGING_FILES)
390 {
391 return STATUS_TOO_MANY_PAGING_FILES;
392 }
393
394 PreviousMode = ExGetPreviousMode();
395
396 if (PreviousMode != KernelMode)
397 {
398 if (SeSinglePrivilegeCheck(SeCreatePagefilePrivilege, PreviousMode) != TRUE)
399 {
400 return STATUS_PRIVILEGE_NOT_HELD;
401 }
402
403 _SEH2_TRY
404 {
405 SafeInitialSize = ProbeForReadLargeInteger(InitialSize);
406 SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
407
408 PageFileName.Length = FileName->Length;
409 PageFileName.MaximumLength = FileName->MaximumLength;
410 PageFileName.Buffer = FileName->Buffer;
411 }
412 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
413 {
414 /* Return the exception code */
415 _SEH2_YIELD(return _SEH2_GetExceptionCode());
416 }
417 _SEH2_END;
418 }
419 else
420 {
421 SafeInitialSize = *InitialSize;
422 SafeMaximumSize = *MaximumSize;
423
424 PageFileName.Length = FileName->Length;
425 PageFileName.MaximumLength = FileName->MaximumLength;
426 PageFileName.Buffer = FileName->Buffer;
427 }
428
429 /* Pagefiles can't be larger than 4GB and ofcourse the minimum should be
430 smaller than the maximum */
431 if (0 != SafeInitialSize.u.HighPart)
432 {
433 return STATUS_INVALID_PARAMETER_2;
434 }
435 if (0 != SafeMaximumSize.u.HighPart)
436 {
437 return STATUS_INVALID_PARAMETER_3;
438 }
439 if (SafeMaximumSize.u.LowPart < SafeInitialSize.u.LowPart)
440 {
441 return STATUS_INVALID_PARAMETER_MIX;
442 }
443
444 /* Validate name length */
445 if (PageFileName.Length > 128 * sizeof(WCHAR))
446 {
447 return STATUS_OBJECT_NAME_INVALID;
448 }
449
450 /* We won't care about any potential UNICODE_NULL */
451 PageFileName.MaximumLength = PageFileName.Length;
452 /* Allocate a buffer to keep name copy */
453 Buffer = ExAllocatePoolWithTag(PagedPool, PageFileName.Length, TAG_MM);
454 if (Buffer == NULL)
455 {
456 return STATUS_INSUFFICIENT_RESOURCES;
457 }
458
459 /* Copy name */
460 if (PreviousMode != KernelMode)
461 {
462 _SEH2_TRY
463 {
464 if (PageFileName.Length != 0)
465 {
466 ProbeForRead(PageFileName.Buffer, PageFileName.Length, sizeof(WCHAR));
467 }
468
469 RtlCopyMemory(Buffer, PageFileName.Buffer, PageFileName.Length);
470 }
471 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
472 {
473 ExFreePoolWithTag(Buffer, TAG_MM);
474
475 /* Return the exception code */
476 _SEH2_YIELD(return _SEH2_GetExceptionCode());
477 }
478 _SEH2_END;
479 }
480 else
481 {
482 RtlCopyMemory(Buffer, PageFileName.Buffer, PageFileName.Length);
483 }
484
485 /* Erase caller's buffer with ours */
486 PageFileName.Buffer = Buffer;
487
488 /* Create the security descriptor for the page file */
489 Status = RtlCreateSecurityDescriptor(&SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
490 if (!NT_SUCCESS(Status))
491 {
492 ExFreePoolWithTag(Buffer, TAG_MM);
493 return Status;
494 }
495
496 /* Create the DACL: we will only allow two SIDs */
497 Count = sizeof(ACL) + (sizeof(ACE) + RtlLengthSid(SeLocalSystemSid)) +
498 (sizeof(ACE) + RtlLengthSid(SeAliasAdminsSid));
499 Dacl = ExAllocatePoolWithTag(PagedPool, Count, 'lcaD');
500 if (Dacl == NULL)
501 {
502 ExFreePoolWithTag(Buffer, TAG_MM);
503 return STATUS_INSUFFICIENT_RESOURCES;
504 }
505
506 /* Initialize the DACL */
507 Status = RtlCreateAcl(Dacl, Count, ACL_REVISION);
508 if (!NT_SUCCESS(Status))
509 {
510 ExFreePoolWithTag(Dacl, 'lcaD');
511 ExFreePoolWithTag(Buffer, TAG_MM);
512 return Status;
513 }
514
515 /* Grant full access to admins */
516 Status = RtlAddAccessAllowedAce(Dacl, ACL_REVISION, FILE_ALL_ACCESS, SeAliasAdminsSid);
517 if (!NT_SUCCESS(Status))
518 {
519 ExFreePoolWithTag(Dacl, 'lcaD');
520 ExFreePoolWithTag(Buffer, TAG_MM);
521 return Status;
522 }
523
524 /* Grant full access to SYSTEM */
525 Status = RtlAddAccessAllowedAce(Dacl, ACL_REVISION, FILE_ALL_ACCESS, SeLocalSystemSid);
526 if (!NT_SUCCESS(Status))
527 {
528 ExFreePoolWithTag(Dacl, 'lcaD');
529 ExFreePoolWithTag(Buffer, TAG_MM);
530 return Status;
531 }
532
533 /* Attach the DACL to the security descriptor */
534 Status = RtlSetDaclSecurityDescriptor(&SecurityDescriptor, TRUE, Dacl, FALSE);
535 if (!NT_SUCCESS(Status))
536 {
537 ExFreePoolWithTag(Dacl, 'lcaD');
538 ExFreePoolWithTag(Buffer, TAG_MM);
539 return Status;
540 }
541
542 InitializeObjectAttributes(&ObjectAttributes,
543 &PageFileName,
544 OBJ_KERNEL_HANDLE,
545 NULL,
546 &SecurityDescriptor);
547
548 /* Make sure we can at least store a complete page:
549 * If we have 2048 BytesPerAllocationUnit (FAT16 < 128MB) there is
550 * a problem if the paging file is fragmented. Suppose the first cluster
551 * of the paging file is cluster 3042 but cluster 3043 is NOT part of the
552 * paging file but of another file. We can't write a complete page (4096
553 * bytes) to the physical location of cluster 3042 then. */
554 AllocationSize.QuadPart = SafeInitialSize.QuadPart + PAGE_SIZE;
555
556 /* First, attempt to replace the page file, if existing */
557 Status = IoCreateFile(&FileHandle,
558 SYNCHRONIZE | WRITE_DAC | FILE_READ_DATA | FILE_WRITE_DATA,
559 &ObjectAttributes,
560 &IoStatus,
561 &AllocationSize,
562 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
563 FILE_SHARE_WRITE,
564 FILE_SUPERSEDE,
565 FILE_DELETE_ON_CLOSE | FILE_NO_COMPRESSION | FILE_NO_INTERMEDIATE_BUFFERING,
566 NULL,
567 0,
568 CreateFileTypeNone,
569 NULL,
570 SL_OPEN_PAGING_FILE | IO_NO_PARAMETER_CHECKING);
571 /* If we failed, relax a bit constraints, someone may be already holding the
572 * the file, so share write, don't attempt to replace and don't delete on close
573 * (basically, don't do anything conflicting)
574 * This can happen if the caller attempts to extend a page file.
575 */
576 if (!NT_SUCCESS(Status))
577 {
578 ULONG i;
579
580 Status = IoCreateFile(&FileHandle,
581 SYNCHRONIZE | FILE_WRITE_DATA,
582 &ObjectAttributes,
583 &IoStatus,
584 &AllocationSize,
585 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
586 FILE_SHARE_WRITE | FILE_SHARE_READ,
587 FILE_OPEN,
588 FILE_NO_COMPRESSION | FILE_NO_INTERMEDIATE_BUFFERING,
589 NULL,
590 0,
591 CreateFileTypeNone,
592 NULL,
593 SL_OPEN_PAGING_FILE | IO_NO_PARAMETER_CHECKING);
594 if (!NT_SUCCESS(Status))
595 {
596 ExFreePoolWithTag(Dacl, 'lcaD');
597 ExFreePoolWithTag(Buffer, TAG_MM);
598 return Status;
599 }
600
601 /* We opened it! Check we are that "someone" ;-)
602 * First, get the opened file object.
603 */
604 Status = ObReferenceObjectByHandle(FileHandle,
605 FILE_READ_DATA | FILE_WRITE_DATA,
606 IoFileObjectType,
607 KernelMode,
608 (PVOID*)&FileObject,
609 NULL);
610 if (!NT_SUCCESS(Status))
611 {
612 ZwClose(FileHandle);
613 ExFreePoolWithTag(Dacl, 'lcaD');
614 ExFreePoolWithTag(Buffer, TAG_MM);
615 return Status;
616 }
617
618 /* Find if it matches a previous page file */
619 PagingFile = NULL;
620 if (MmNumberOfPagingFiles > 0)
621 {
622 i = 0;
623
624 while (MmPagingFile[i]->FileObject->SectionObjectPointer != FileObject->SectionObjectPointer)
625 {
626 ++i;
627 if (i >= MmNumberOfPagingFiles)
628 {
629 break;
630 }
631 }
632
633 /* This is the matching page file */
634 PagingFile = MmPagingFile[i];
635 }
636
637 /* If we didn't find the page file, fail */
638 if (PagingFile == NULL)
639 {
640 ObDereferenceObject(FileObject);
641 ZwClose(FileHandle);
642 ExFreePoolWithTag(Dacl, 'lcaD');
643 ExFreePoolWithTag(Buffer, TAG_MM);
644 return STATUS_NOT_FOUND;
645 }
646
647 /* FIXME: implement parameters checking and page file extension */
648 UNIMPLEMENTED;
649
650 ObDereferenceObject(FileObject);
651 ZwClose(FileHandle);
652 ExFreePoolWithTag(Dacl, 'lcaD');
653 ExFreePoolWithTag(Buffer, TAG_MM);
654 return STATUS_NOT_IMPLEMENTED;
655 }
656
657 if (!NT_SUCCESS(Status))
658 {
659 DPRINT1("Failed creating page file: %lx\n", Status);
660 ExFreePoolWithTag(Dacl, 'lcaD');
661 ExFreePoolWithTag(Buffer, TAG_MM);
662 return Status;
663 }
664
665 /* Set the security descriptor */
666 if (NT_SUCCESS(IoStatus.Status))
667 {
668 Status = ZwSetSecurityObject(FileHandle, DACL_SECURITY_INFORMATION, &SecurityDescriptor);
669 if (!NT_SUCCESS(Status))
670 {
671 ExFreePoolWithTag(Dacl, 'lcaD');
672 ZwClose(FileHandle);
673 ExFreePoolWithTag(Buffer, TAG_MM);
674 return Status;
675 }
676 }
677
678 /* DACL is no longer needed, free it */
679 ExFreePoolWithTag(Dacl, 'lcaD');
680
681 /* Set its end of file to initial size */
682 Status = ZwSetInformationFile(FileHandle,
683 &IoStatus,
684 &SafeInitialSize,
685 sizeof(LARGE_INTEGER),
686 FileEndOfFileInformation);
687 if (!NT_SUCCESS(Status) || !NT_SUCCESS(IoStatus.Status))
688 {
689 ZwClose(FileHandle);
690 ExFreePoolWithTag(Buffer, TAG_MM);
691 return Status;
692 }
693
694 Status = ObReferenceObjectByHandle(FileHandle,
695 FILE_ALL_ACCESS,
696 IoFileObjectType,
697 KernelMode,
698 (PVOID*)&FileObject,
699 NULL);
700 if (!NT_SUCCESS(Status))
701 {
702 ZwClose(FileHandle);
703 ExFreePoolWithTag(Buffer, TAG_MM);
704 return Status;
705 }
706
707 /* Deny page file creation on a floppy disk */
708 FsDeviceInfo.Characteristics = 0;
709 IoQueryVolumeInformation(FileObject, FileFsDeviceInformation, sizeof(FsDeviceInfo), &FsDeviceInfo, &Count);
710 if (BooleanFlagOn(FsDeviceInfo.Characteristics, FILE_FLOPPY_DISKETTE))
711 {
712 ObDereferenceObject(FileObject);
713 ZwClose(FileHandle);
714 ExFreePoolWithTag(Buffer, TAG_MM);
715 return STATUS_FLOPPY_VOLUME;
716 }
717
718 PagingFile = ExAllocatePoolWithTag(NonPagedPool, sizeof(*PagingFile), TAG_MM);
719 if (PagingFile == NULL)
720 {
721 ObDereferenceObject(FileObject);
722 ZwClose(FileHandle);
723 ExFreePoolWithTag(Buffer, TAG_MM);
724 return STATUS_INSUFFICIENT_RESOURCES;
725 }
726
727 RtlZeroMemory(PagingFile, sizeof(*PagingFile));
728
729 PagingFile->FileHandle = FileHandle;
730 PagingFile->FileObject = FileObject;
731 PagingFile->MaximumSize.QuadPart = SafeMaximumSize.QuadPart;
732 PagingFile->CurrentSize.QuadPart = SafeInitialSize.QuadPart;
733 PagingFile->FreePages = (ULONG)(SafeInitialSize.QuadPart / PAGE_SIZE);
734 PagingFile->UsedPages = 0;
735 KeInitializeSpinLock(&PagingFile->AllocMapLock);
736 PagingFile->PageFileName = PageFileName;
737
738 AllocMapSize = sizeof(RTL_BITMAP) + (((PagingFile->FreePages + 31) / 32) * sizeof(ULONG));
739 PagingFile->AllocMap = ExAllocatePoolWithTag(NonPagedPool,
740 AllocMapSize,
741 TAG_MM);
742 if (PagingFile->AllocMap == NULL)
743 {
744 ExFreePoolWithTag(PagingFile, TAG_MM);
745 ObDereferenceObject(FileObject);
746 ZwClose(FileHandle);
747 ExFreePoolWithTag(Buffer, TAG_MM);
748 return STATUS_INSUFFICIENT_RESOURCES;
749 }
750
751 RtlInitializeBitMap(PagingFile->AllocMap,
752 (PULONG)(PagingFile->AllocMap + 1),
753 (ULONG)(PagingFile->FreePages));
754 RtlClearAllBits(PagingFile->AllocMap);
755
756 KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
757 ASSERT(MmPagingFile[MmNumberOfPagingFiles] == NULL);
758 MmPagingFile[MmNumberOfPagingFiles] = PagingFile;
759 MmNumberOfPagingFiles++;
760 MiFreeSwapPages = MiFreeSwapPages + PagingFile->FreePages;
761 KeReleaseSpinLock(&PagingFileListLock, oldIrql);
762
763 MmSwapSpaceMessage = FALSE;
764
765 return STATUS_SUCCESS;
766 }
767
768 /* EOF */