00209 :
00210
00211 This function extends
the size of
the specified section. If
00212
the current size of
the section
is greater than or equal to
the
00213 specified section size,
the size
is not updated.
00214
00215 Arguments:
00216
00217 Section - Supplies a pointer to a referenced section object.
00218
00219 NewSectionSize - Supplies
the new size
for the section object.
00220
00221 IgnoreFileSizeChecking - Supplies
the value
TRUE is file size
00222 checking should be ignored (i.e., it
00223 is being called from a file system which
00224 has already done the checks).
FALSE
00225
if the checks still need to be
made.
00226
00227 Return Value:
00228
00229 Returns
the status
00230
00231 TBS
00232
00233
00234 --*/
00235
00236 {
00237
PMMPTE PointerPte;
00238
PMMPTE LastPte;
00239
PMMPTE ExtendedPtes;
00240
MMPTE TempPte;
00241
PCONTROL_AREA ControlArea;
00242
PSECTION Section;
00243
PSUBSECTION LastSubsection;
00244
PSUBSECTION ExtendedSubsection;
00245 ULONG RequiredPtes;
00246 ULONG NumberOfPtes;
00247 ULONG PtesUsed;
00248 ULONG AllocationSize;
00249 UINT64 EndOfFile;
00250 UINT64 NumberOfPtesForEntireFile;
00251
NTSTATUS Status;
00252 LARGE_INTEGER NumberOf4KsForEntireFile;
00253 LARGE_INTEGER Starting4K;
00254 LARGE_INTEGER Last4KChunk;
00255
00256
PAGED_CODE();
00257
00258 Section = (
PSECTION)SectionToExtend;
00259
00260
00261
00262
00263
00264
00265 ControlArea = Section->
Segment->
ControlArea;
00266
00267
if ((ControlArea->
u.Flags.PhysicalMemory || ControlArea->
u.Flags.Image) ||
00268 (ControlArea->
FilePointer ==
NULL)) {
00269
return STATUS_SECTION_NOT_EXTENDED;
00270 }
00271
00272
00273
00274
00275
00276
00277
KeEnterCriticalRegion ();
00278
ExAcquireResourceExclusive (&MmSectionExtendResource, TRUE);
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 NumberOfPtesForEntireFile = (NewSectionSize->QuadPart +
PAGE_SIZE - 1) >>
PAGE_SHIFT;
00297
00298 NumberOfPtes = (ULONG)NumberOfPtesForEntireFile;
00299
00300
if (NewSectionSize->QuadPart >
MI_MAXIMUM_SECTION_SIZE) {
00301
Status = STATUS_SECTION_TOO_BIG;
00302
goto ReleaseAndReturn;
00303 }
00304
00305
if (NumberOfPtesForEntireFile > (UINT64)((MAXULONG_PTR /
sizeof(
MMPTE)) -
sizeof (
SEGMENT))) {
00306
Status = STATUS_SECTION_TOO_BIG;
00307
goto ReleaseAndReturn;
00308 }
00309
00310
if (NumberOfPtesForEntireFile > (UINT64)NewSectionSize->QuadPart) {
00311
Status = STATUS_SECTION_TOO_BIG;
00312
goto ReleaseAndReturn;
00313 }
00314
00315
if (ControlArea->
u.Flags.WasPurged == 0) {
00316
00317
if ((UINT64)NewSectionSize->QuadPart <= (UINT64)Section->SizeOfSection.QuadPart) {
00318 *NewSectionSize = Section->SizeOfSection;
00319
goto ReleaseAndReturnSuccess;
00320 }
00321 }
00322
00323
00324
00325
00326
00327
if (IgnoreFileSizeChecking ==
FALSE) {
00328
00329
00330
00331
00332
00333
00334
ExReleaseResource (&MmSectionExtendResource);
00335
00336
00337
00338
00339
00340
ExAcquireResourceExclusive (&MmSectionExtendSetResource, TRUE);
00341
00342
00343
00344
00345
00346
00347
00348
00349
Status =
FsRtlGetFileSize (ControlArea->
FilePointer,
00350 (PLARGE_INTEGER)&EndOfFile);
00351
00352
if (!
NT_SUCCESS (Status)) {
00353
ExReleaseResource (&MmSectionExtendSetResource);
00354
KeLeaveCriticalRegion ();
00355
return Status;
00356 }
00357
00358
if ((UINT64)NewSectionSize->QuadPart > EndOfFile) {
00359
00360
00361
00362
00363
00364
00365
00366
00367
if (((Section->InitialPageProtection & PAGE_READWRITE) |
00368 (Section->InitialPageProtection & PAGE_EXECUTE_READWRITE)) == 0) {
00369
#if DBG
00370
DbgPrint(
"Section extension failed %x\n", Section);
00371
#endif
00372
ExReleaseResource (&MmSectionExtendSetResource);
00373
KeLeaveCriticalRegion ();
00374
return STATUS_SECTION_NOT_EXTENDED;
00375 }
00376
00377
00378
00379
00380
00381 EndOfFile = *(PUINT64)NewSectionSize;
00382
00383
Status =
FsRtlSetFileSize (ControlArea->
FilePointer,
00384 (PLARGE_INTEGER)&EndOfFile);
00385
00386
if (!
NT_SUCCESS (Status)) {
00387
ExReleaseResource (&MmSectionExtendSetResource);
00388
KeLeaveCriticalRegion ();
00389
return Status;
00390 }
00391 }
00392
00393
if (ControlArea->
Segment->
ExtendInfo) {
00394 ExAcquireFastMutex (&MmSectionBasedMutex);
00395
if (ControlArea->
Segment->
ExtendInfo) {
00396 ControlArea->
Segment->
ExtendInfo->
CommittedSize = EndOfFile;
00397 }
00398 ExReleaseFastMutex (&MmSectionBasedMutex);
00399 }
00400
00401
00402
00403
00404
00405
00406
ExReleaseResource (&MmSectionExtendSetResource);
00407
ExAcquireResourceExclusive (&MmSectionExtendResource, TRUE);
00408 }
00409
00410
00411
00412
00413
00414
ASSERT (ControlArea->
u.Flags.GlobalOnlyPerSession == 0);
00415
00416 LastSubsection = (
PSUBSECTION)(ControlArea + 1);
00417
00418
while (LastSubsection->
NextSubsection !=
NULL ) {
00419
ASSERT (LastSubsection->
UnusedPtes == 0);
00420 LastSubsection = LastSubsection->
NextSubsection;
00421 }
00422
00423
#if DBG
00424
MiSubsectionConsistent(LastSubsection);
00425
#endif
00426
00427
00428
00429
00430
00431
if (NumberOfPtes <= Section->Segment->TotalNumberOfPtes) {
00432
00433
00434
00435
00436
00437
00438 Section->SizeOfSection = *NewSectionSize;
00439
if (Section->Segment->SizeOfSegment < (UINT64)NewSectionSize->QuadPart) {
00440
00441
00442
00443
00444 Section->Segment->SizeOfSegment = *(PUINT64)NewSectionSize;
00445
00446
Mi4KStartFromSubsection(&Starting4K, LastSubsection);
00447
00448 Last4KChunk.QuadPart = (NewSectionSize->QuadPart >>
MM4K_SHIFT) - Starting4K.QuadPart;
00449
00450
ASSERT (Last4KChunk.HighPart == 0);
00451
00452 LastSubsection->
NumberOfFullSectors = Last4KChunk.LowPart;
00453 LastSubsection->
u.SubsectionFlags.SectorEndOffset =
00454 NewSectionSize->LowPart &
MM4K_MASK;
00455
#if DBG
00456
MiSubsectionConsistent(LastSubsection);
00457
#endif
00458
}
00459
goto ReleaseAndReturnSuccess;
00460 }
00461
00462
00463
00464
00465
00466
00467 RequiredPtes = NumberOfPtes - Section->Segment->TotalNumberOfPtes;
00468 PtesUsed = 0;
00469
00470
if (RequiredPtes < LastSubsection->
UnusedPtes) {
00471
00472
00473
00474
00475
00476 PtesUsed = RequiredPtes;
00477 RequiredPtes = 0;
00478
00479 }
else {
00480 PtesUsed = LastSubsection->
UnusedPtes;
00481 RequiredPtes -= PtesUsed;
00482 }
00483
00484 LastSubsection->
PtesInSubsection += PtesUsed;
00485 LastSubsection->
UnusedPtes -= PtesUsed;
00486 ControlArea->
Segment->
SizeOfSegment += (ULONG_PTR)PtesUsed *
PAGE_SIZE;
00487 ControlArea->
Segment->
TotalNumberOfPtes += PtesUsed;
00488
00489
if (RequiredPtes == 0) {
00490
00491
00492
00493
00494
00495
Mi4KStartFromSubsection(&Starting4K, LastSubsection);
00496
00497 Last4KChunk.QuadPart = (NewSectionSize->QuadPart >>
MM4K_SHIFT) - Starting4K.QuadPart;
00498
00499
ASSERT (Last4KChunk.HighPart == 0);
00500
00501 LastSubsection->
NumberOfFullSectors = Last4KChunk.LowPart;
00502
00503 LastSubsection->
u.SubsectionFlags.SectorEndOffset =
00504 NewSectionSize->LowPart &
MM4K_MASK;
00505
#if DBG
00506
MiSubsectionConsistent(LastSubsection);
00507
#endif
00508
}
else {
00509
00510
00511
00512
00513
00514
00515 AllocationSize = (ULONG)
ROUND_TO_PAGES (RequiredPtes *
sizeof(
MMPTE));
00516
00517 ExtendedPtes = (
PMMPTE)
ExAllocatePoolWithTag (PagedPool,
00518 AllocationSize,
00519 'ppmM');
00520
00521
if (ExtendedPtes ==
NULL) {
00522
00523
00524
00525
00526
00527
00528
00529 LastSubsection->
PtesInSubsection -= PtesUsed;
00530 LastSubsection->
UnusedPtes += PtesUsed;
00531 ControlArea->
Segment->
TotalNumberOfPtes -= PtesUsed;
00532 ControlArea->
Segment->
SizeOfSegment -= ((ULONG_PTR)PtesUsed *
PAGE_SIZE);
00533
Status = STATUS_INSUFFICIENT_RESOURCES;
00534
goto ReleaseAndReturn;
00535 }
00536
00537
00538
00539
00540
00541 ExtendedSubsection = (
PSUBSECTION)
ExAllocatePoolWithTag (NonPagedPool,
00542
sizeof(
SUBSECTION),
00543 'bSmM'
00544 );
00545
if (ExtendedSubsection ==
NULL) {
00546
00547
00548
00549
00550
00551
00552
00553 LastSubsection->
PtesInSubsection -= PtesUsed;
00554 LastSubsection->
UnusedPtes += PtesUsed;
00555 ControlArea->
Segment->
TotalNumberOfPtes -= PtesUsed;
00556 ControlArea->
Segment->
SizeOfSegment -= ((ULONG_PTR)PtesUsed *
PAGE_SIZE);
00557
ExFreePool (ExtendedPtes);
00558
Status = STATUS_INSUFFICIENT_RESOURCES;
00559
goto ReleaseAndReturn;
00560 }
00561
00562 ControlArea->
NonPagedPoolUsage +=
EX_REAL_POOL_USAGE(
sizeof(
SUBSECTION));
00563 ControlArea->
PagedPoolUsage += AllocationSize;
00564
00565
ASSERT (ControlArea->
DereferenceList.Flink == NULL);
00566
00567 NumberOf4KsForEntireFile.QuadPart =
00568 ControlArea->
Segment->
SizeOfSegment >>
MM4K_SHIFT;
00569
00570
Mi4KStartFromSubsection(&Starting4K, LastSubsection);
00571
00572 Last4KChunk.QuadPart = NumberOf4KsForEntireFile.QuadPart -
00573 Starting4K.QuadPart;
00574
00575
if (LastSubsection->
u.SubsectionFlags.SectorEndOffset) {
00576 Last4KChunk.QuadPart += 1;
00577 }
00578
00579
ASSERT(Last4KChunk.HighPart == 0);
00580
00581 LastSubsection->
NumberOfFullSectors = Last4KChunk.LowPart;
00582 LastSubsection->
u.SubsectionFlags.SectorEndOffset = 0;
00583
00584
00585
00586
00587
00588
00589
if (LastSubsection->
NumberOfFullSectors & ((1 << (
PAGE_SHIFT -
MM4K_SHIFT)) - 1)) {
00590 LastSubsection->
NumberOfFullSectors += 1;
00591 }
00592
00593
#if DBG
00594
MiSubsectionConsistent(LastSubsection);
00595
#endif
00596
00597 ExtendedSubsection->
u.LongFlags = 0;
00598 ExtendedSubsection->
NextSubsection =
NULL;
00599 ExtendedSubsection->
UnusedPtes = (AllocationSize /
sizeof(
MMPTE)) -
00600 RequiredPtes;
00601
00602 ExtendedSubsection->
ControlArea = ControlArea;
00603 ExtendedSubsection->
PtesInSubsection = RequiredPtes;
00604
00605 Starting4K.QuadPart += LastSubsection->
NumberOfFullSectors;
00606
00607
Mi4KStartForSubsection(&Starting4K, ExtendedSubsection);
00608
00609 Last4KChunk.QuadPart =
00610 (NewSectionSize->QuadPart >>
MM4K_SHIFT) - Starting4K.QuadPart;
00611
00612
ASSERT(Last4KChunk.HighPart == 0);
00613
00614 ExtendedSubsection->
NumberOfFullSectors = Last4KChunk.LowPart;
00615 ExtendedSubsection->
u.SubsectionFlags.SectorEndOffset =
00616 NewSectionSize->LowPart &
MM4K_MASK;
00617
00618
#if DBG
00619
MiSubsectionConsistent(ExtendedSubsection);
00620
#endif
00621
00622 ExtendedSubsection->
SubsectionBase = ExtendedPtes;
00623
00624 PointerPte = ExtendedPtes;
00625 LastPte = ExtendedPtes + (AllocationSize /
sizeof(
MMPTE));
00626
00627
if (ControlArea->
FilePointer !=
NULL) {
00628 TempPte.
u.Long =
MiGetSubsectionAddressForPte(ExtendedSubsection);
00629 }
00630
#if DBG
00631
else {
00632
DbgPrint(
"MM: Extend with no control area file pointer %x %x\n",
00633 ExtendedSubsection, ControlArea);
00634 DbgBreakPoint();
00635 }
00636
#endif
00637
00638 TempPte.
u.Soft.Protection = ControlArea->
Segment->
SegmentPteTemplate.
u.Soft.Protection;
00639 TempPte.
u.Soft.Prototype = 1;
00640 ExtendedSubsection->
u.SubsectionFlags.Protection =
00641
MI_GET_PROTECTION_FROM_SOFT_PTE(&TempPte);
00642
00643
while (PointerPte < LastPte) {
00644
MI_WRITE_INVALID_PTE (PointerPte, TempPte);
00645 PointerPte += 1;
00646 }
00647
00648
00649
00650
00651
00652 LastSubsection->
NextSubsection = ExtendedSubsection;
00653 ControlArea->
Segment->
TotalNumberOfPtes += RequiredPtes;
00654
00655
#if defined(_ALPHA_) && !defined(NT_UP)
00656
00657
00658
00659
00660
00661
00662
00663 __MB();
00664
#endif
00665
00666 }
00667
00668 ControlArea->
Segment->
SizeOfSegment = *(PUINT64)NewSectionSize;
00669 Section->SizeOfSection = *NewSectionSize;
00670
00671 ReleaseAndReturnSuccess:
00672
00673
Status = STATUS_SUCCESS;
00674
00675 ReleaseAndReturn:
00676
00677
ExReleaseResource (&MmSectionExtendResource);
00678
KeLeaveCriticalRegion ();
00679
00680
return Status;
00681 }