00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
#include "cmp.h"
00026
00027 extern BOOLEAN
HvShutdownComplete;
00028
00029
00030
#if DBG
00031
#define DumpDirtyVector(Hive) \
00032
{ \
00033
PRTL_BITMAP BitMap; \
00034
ULONG BitMapSize; \
00035
PUCHAR BitBuffer; \
00036
ULONG i; \
00037
UCHAR Byte; \
00038
\
00039
BitMap = &(Hive->DirtyVector); \
00040
BitMapSize = (BitMap->SizeOfBitMap) / 8; \
00041
BitBuffer = (PUCHAR)(BitMap->Buffer); \
00042
for (i = 0; i < BitMapSize; i++) { \
00043
if ((i % 8) == 0) { \
00044
KdPrint(("\n\t")); \
00045
} \
00046
Byte = BitBuffer[i]; \
00047
KdPrint(("%02x ", Byte)); \
00048
} \
00049
KdPrint(("\n")); \
00050
}
00051
#else
00052 #define DumpDirtyVector(Hive)
00053
#endif
00054
00055
00056
00057
00058 BOOLEAN
00059
HvpWriteLog(
00060
PHHIVE Hive
00061 );
00062
00063 BOOLEAN
00064
HvpFindNextDirtyBlock(
00065
PHHIVE Hive,
00066 PRTL_BITMAP
BitMap,
00067 PULONG Current,
00068 PUCHAR *Address,
00069 PULONG Length,
00070 PULONG Offset
00071 );
00072
00073
VOID
00074
HvpDiscardBins(
00075
PHHIVE Hive
00076 );
00077
00078
VOID
00079
HvpTruncateBins(
00080
PHHIVE Hive
00081 );
00082
00083
VOID
00084
HvRefreshHive(
00085
PHHIVE Hive
00086 );
00087
00088
#ifdef ALLOC_PRAGMA
00089
#pragma alloc_text(PAGE,HvMarkCellDirty)
00090
#pragma alloc_text(PAGE,HvIsBinDirty)
00091
#pragma alloc_text(PAGE,HvMarkDirty)
00092
#pragma alloc_text(PAGE,HvMarkClean)
00093
#pragma alloc_text(PAGE,HvpGrowLog1)
00094
#pragma alloc_text(PAGE,HvpGrowLog2)
00095
#pragma alloc_text(PAGE,HvSyncHive)
00096
#pragma alloc_text(PAGE,HvpDoWriteHive)
00097
#pragma alloc_text(PAGE,HvpWriteLog)
00098
#pragma alloc_text(PAGE,HvpFindNextDirtyBlock)
00099
#pragma alloc_text(PAGE,HvWriteHive)
00100
#pragma alloc_text(PAGE,HvRefreshHive)
00101
#pragma alloc_text(PAGE,HvpDiscardBins)
00102
#pragma alloc_text(PAGE,HvpTruncateBins)
00103
00104
#ifdef _WRITE_PROTECTED_REGISTRY_POOL
00105
#pragma alloc_text(PAGE,HvpChangeBinAllocation)
00106
#pragma alloc_text(PAGE,HvpMarkBinReadWrite)
00107
#endif
00108
00109
#endif
00110
00111
00112
00113
00114
00115
00116 BOOLEAN
00117 HvMarkCellDirty(
00118
PHHIVE Hive,
00119 HCELL_INDEX Cell
00120 )
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 {
00142 ULONG Type;
00143 ULONG
Size;
00144
PHCELL pCell;
00145
PHMAP_ENTRY Me;
00146
HCELL_INDEX Base;
00147
PHBIN Bin;
00148
00149
CMLOG(
CML_MINOR,
CMS_IO) {
00150 KdPrint((
"HvMarkCellDirty:\n\t"));
00151 KdPrint((
"Hive:%08lx Cell:%08lx\n",
Hive,
Cell));
00152 }
00153
00154
ASSERT(
Hive->
Signature ==
HHIVE_SIGNATURE);
00155
ASSERT(
Hive->
ReadOnly ==
FALSE);
00156
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00157
00158 Type =
HvGetCellType(
Cell);
00159
00160
if ( (
Hive->
HiveFlags &
HIVE_VOLATILE) ||
00161 (Type ==
Volatile) )
00162 {
00163
return TRUE;
00164 }
00165
00166 pCell =
HvpGetHCell(
Hive,
Cell);
00167
#if DBG
00168
Me =
HvpGetCellMap(
Hive,
Cell);
00169
VALIDATE_CELL_MAP(__LINE__,Me,
Hive,
Cell);
00170
Bin = (
PHBIN)(Me->
BinAddress &
HMAP_BASE);
00171
ASSERT(
Bin->
Signature ==
HBIN_SIGNATURE);
00172
#endif
00173
00174
00175
00176
00177
00178
00179
00180
if (
USE_OLD_CELL(
Hive)) {
00181 Me =
HvpGetCellMap(
Hive,
Cell);
00182
VALIDATE_CELL_MAP(__LINE__,Me,
Hive,
Cell);
00183
Bin = (
PHBIN)(Me->
BinAddress &
HMAP_BASE);
00184 Base =
Bin->
FileOffset;
00185
Size =
Bin->
Size;
00186
return HvMarkDirty(
Hive, Base,
Size);
00187 }
else {
00188
if (pCell->
Size < 0) {
00189
Size = -pCell->
Size;
00190 }
else {
00191
Size = pCell->
Size;
00192 }
00193
ASSERT(Size < Bin->
Size);
00194
return HvMarkDirty(
Hive,
Cell-FIELD_OFFSET(
HCELL,u.NewCell),
Size);
00195 }
00196 }
00197
00198
00199 BOOLEAN
00200 HvIsBinDirty(
00201 IN
PHHIVE Hive,
00202 IN HCELL_INDEX Cell
00203 )
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 {
00227 ULONG Type;
00228
PHCELL pcell;
00229 PRTL_BITMAP Bitmap;
00230 ULONG First;
00231 ULONG Last;
00232 ULONG i;
00233
PHMAP_ENTRY Map;
00234
PHBIN Bin;
00235
00236
CMLOG(
CML_MINOR,
CMS_IO) {
00237 KdPrint((
"HvIsBinDirty:\n\t"));
00238 KdPrint((
"Hive:%08lx Cell:%08lx\n",
Hive,
Cell));
00239 }
00240
00241
ASSERT(
Hive->
Signature ==
HHIVE_SIGNATURE);
00242
ASSERT(
Hive->
ReadOnly ==
FALSE);
00243
00244 Type =
HvGetCellType(
Cell);
00245
00246
if ( (
Hive->
HiveFlags &
HIVE_VOLATILE) ||
00247 (Type ==
Volatile) )
00248 {
00249
return FALSE;
00250 }
00251
00252 Bitmap = &(
Hive->
DirtyVector);
00253
00254 Map =
HvpGetCellMap(
Hive,
Cell);
00255
VALIDATE_CELL_MAP(__LINE__,Map,
Hive,
Cell);
00256
Bin = (
PHBIN)(Map->
BinAddress &
HMAP_BASE);
00257 First =
Bin->
FileOffset /
HSECTOR_SIZE;
00258 Last = (
Bin->
FileOffset +
Bin->
Size - 1) /
HSECTOR_SIZE;
00259
00260
for (i=First; i<=Last; i++) {
00261
if (RtlCheckBit(Bitmap, i)==1) {
00262
return(
TRUE);
00263 }
00264 }
00265
return(
FALSE);
00266 }
00267
00268
00269 BOOLEAN
00270 HvMarkDirty(
00271
PHHIVE Hive,
00272 HCELL_INDEX Start,
00273 ULONG Length
00274 )
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 {
00309 ULONG Type;
00310 PRTL_BITMAP
BitMap;
00311 ULONG First;
00312 ULONG Last;
00313 ULONG i;
00314 ULONG Cluster;
00315 ULONG OriginalDirtyCount;
00316 ULONG DirtySectors;
00317 BOOLEAN Result =
TRUE;
00318
00319
CMLOG(
CML_MINOR,
CMS_IO) {
00320 KdPrint((
"HvMarkDirty:\n\t"));
00321 KdPrint((
"Hive:%08lx Start:%08lx Length:%08lx\n",
Hive,
Start, Length));
00322 }
00323
00324
00325
ASSERT(
Hive->
Signature ==
HHIVE_SIGNATURE);
00326
ASSERT(
Hive->
ReadOnly ==
FALSE);
00327
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00328
00329 Type =
HvGetCellType(
Start);
00330
00331
if ( (
Hive->
HiveFlags &
HIVE_VOLATILE) ||
00332 (Type ==
Volatile) )
00333 {
00334
return TRUE;
00335 }
00336
00337
00338
BitMap = &(
Hive->
DirtyVector);
00339 OriginalDirtyCount =
Hive->
DirtyCount;
00340
00341 First =
Start /
HSECTOR_SIZE;
00342 Last = (
Start + Length - 1) /
HSECTOR_SIZE;
00343
00344 Cluster =
Hive->
Cluster;
00345
if (Cluster > 1) {
00346
00347
00348
00349
00350
00351 First = First & ~(Cluster - 1);
00352 Last =
ROUND_UP(Last+1, Cluster) - 1;
00353 }
00354
00355
if (Last >=
BitMap->SizeOfBitMap) {
00356 Last =
BitMap->SizeOfBitMap-1;
00357 }
00358
00359
00360
00361
00362 DirtySectors = 0;
00363
for (i = First; i <= Last; i++) {
00364
if (RtlCheckBit(
BitMap, i)==0) {
00365 ++DirtySectors;
00366 }
00367 }
00368
if (DirtySectors != 0) {
00369
if (
HvpGrowLog1(
Hive, DirtySectors) ==
FALSE) {
00370
return(
FALSE);
00371 }
00372
00373
if ((OriginalDirtyCount == 0) && (First != 0)) {
00374 Result =
HvMarkDirty(
Hive, 0,
sizeof(
HBIN));
00375
if (Result==
FALSE) {
00376
return(
FALSE);
00377 }
00378 }
00379
00380
00381
00382
00383
00384
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00385
Hive->
DirtyCount += DirtySectors;
00386
RtlSetBits(
BitMap, First, Last-First+1);
00387 }
00388
00389
00390
HvpMarkBinReadWrite(
Hive,
Start);
00391
00392
if (!(
Hive->
HiveFlags &
HIVE_NOLAZYFLUSH)) {
00393
CmpLazyFlush();
00394 }
00395
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00396
return(
TRUE);
00397 }
00398
00399
00400 BOOLEAN
00401 HvMarkClean(
00402
PHHIVE Hive,
00403 HCELL_INDEX Start,
00404 ULONG Length
00405 )
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 {
00432 ULONG Type;
00433 PRTL_BITMAP
BitMap;
00434 ULONG First;
00435 ULONG Last;
00436 ULONG i;
00437 ULONG Cluster;
00438
00439
CMLOG(
CML_MINOR,
CMS_IO) {
00440 KdPrint((
"HvMarkClean:\n\t"));
00441 KdPrint((
"Hive:%08lx Start:%08lx Length:%08lx\n",
Hive,
Start, Length));
00442 }
00443
00444
00445
ASSERT(
Hive->
Signature ==
HHIVE_SIGNATURE);
00446
ASSERT(
Hive->
ReadOnly ==
FALSE);
00447
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00448
00449 Type =
HvGetCellType(
Start);
00450
00451
if ( (
Hive->
HiveFlags &
HIVE_VOLATILE) ||
00452 (Type ==
Volatile) )
00453 {
00454
return TRUE;
00455 }
00456
00457
BitMap = &(
Hive->
DirtyVector);
00458
00459 First =
Start /
HSECTOR_SIZE;
00460 Last = (
Start + Length - 1) /
HSECTOR_SIZE;
00461
00462 Cluster =
Hive->
Cluster;
00463
if (Cluster > 1) {
00464
00465
00466
00467
00468
00469 First = First & ~(Cluster - 1);
00470 Last =
ROUND_UP(Last+1, Cluster) - 1;
00471 }
00472
00473
if (Last >=
BitMap->SizeOfBitMap) {
00474 Last =
BitMap->SizeOfBitMap-1;
00475 }
00476
00477
00478
00479
00480
00481
for (i=First; i<=Last; i++) {
00482
if (RtlCheckBit(
BitMap,i)==1) {
00483 --
Hive->
DirtyCount;
00484
RtlClearBits(
BitMap, i, 1);
00485 }
00486 }
00487
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00488
00489
return(
TRUE);
00490 }
00491
00492
00493
00494 BOOLEAN
00495 HvpGrowLog1(
00496
PHHIVE Hive,
00497 ULONG Count
00498 )
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 {
00521 ULONG ClusterSize;
00522 ULONG RequiredSize;
00523 ULONG tmp;
00524
00525
CMLOG(
CML_MINOR,
CMS_IO) {
00526 KdPrint((
"HvpGrowLog1:\n\t"));
00527 KdPrint((
"Hive:%08lx Count:%08lx\n",
Hive,
Count));
00528 }
00529
00530
ASSERT(
Hive->
ReadOnly ==
FALSE);
00531
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00532
00533
00534
00535
00536
if (
Hive->
Log ==
FALSE) {
00537
return TRUE;
00538 }
00539
00540 ClusterSize =
Hive->
Cluster *
HSECTOR_SIZE;
00541
00542 tmp =
Hive->
DirtyVector.SizeOfBitMap / 8;
00543 tmp +=
sizeof(ULONG);
00544
00545 RequiredSize =
00546 ClusterSize +
00547
ROUND_UP(tmp, ClusterSize) +
00548 ((
Hive->
DirtyCount +
Count) *
HSECTOR_SIZE);
00549
00550 RequiredSize =
ROUND_UP(RequiredSize,
HLOG_GROW);
00551
00552
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00553
00554
if ( ! (
Hive->
FileSetSize)(
Hive,
HFILE_TYPE_LOG, RequiredSize)) {
00555
return FALSE;
00556 }
00557
00558
Hive->
LogSize = RequiredSize;
00559
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00560
return TRUE;
00561 }
00562
00563
00564 BOOLEAN
00565 HvpGrowLog2(
00566
PHHIVE Hive,
00567 ULONG Size
00568 )
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 {
00591 ULONG ClusterSize;
00592 ULONG RequiredSize;
00593 ULONG DirtyBytes;
00594
00595
CMLOG(
CML_MINOR,
CMS_IO) {
00596 KdPrint((
"HvpGrowLog2:\n\t"));
00597 KdPrint((
"Hive:%08lx Size:%08lx\n",
Hive,
Size));
00598 }
00599
00600
ASSERT(
Hive->
ReadOnly ==
FALSE);
00601
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00602
00603
00604
00605
00606
00607
if (
Hive->
Log ==
FALSE) {
00608
return TRUE;
00609 }
00610
00611
ASSERT( (
Size %
HSECTOR_SIZE) == 0 );
00612
00613 ClusterSize =
Hive->
Cluster *
HSECTOR_SIZE;
00614
00615
ASSERT( (((
Hive->Storage[
Stable].Length +
Size) /
HSECTOR_SIZE) % 8) == 0);
00616
00617 DirtyBytes = (
Hive->
DirtyVector.SizeOfBitMap / 8) +
00618 ((
Size /
HSECTOR_SIZE) / 8) +
00619
sizeof(ULONG);
00620 DirtyBytes =
ROUND_UP(DirtyBytes, ClusterSize);
00621
00622 RequiredSize =
00623 ClusterSize +
00624 (
Hive->
DirtyCount *
HSECTOR_SIZE) +
00625 DirtyBytes;
00626
00627 RequiredSize =
ROUND_UP(RequiredSize,
HLOG_GROW);
00628
00629
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00630
00631
if ( ! (
Hive->
FileSetSize)(
Hive,
HFILE_TYPE_LOG, RequiredSize)) {
00632
return FALSE;
00633 }
00634
00635
Hive->
LogSize = RequiredSize;
00636
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
00637
return TRUE;
00638 }
00639
00640
00641
00642
00643
00644
00645
00646 BOOLEAN
00647 HvSyncHive(
00648
PHHIVE Hive
00649 )
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677 {
00678 BOOLEAN oldFlag;
00679
00680
CMLOG(
CML_WORKER,
CMS_IO) {
00681 KdPrint((
"HvSyncHive:\n\t"));
00682 KdPrint((
"Hive:%08lx\n",
Hive));
00683 }
00684
00685
ASSERT(
Hive->
Signature ==
HHIVE_SIGNATURE);
00686
ASSERT(
Hive->
ReadOnly ==
FALSE);
00687
00688
00689
00690
00691
if (
HvShutdownComplete) {
00692
CMLOG(
CML_BUGCHECK,
CMS_IO) {
00693 KdPrint((
"HvSyncHive: Attempt to sync AFTER SHUTDOWN\n"));
00694 }
00695
return FALSE;
00696 }
00697
00698
00699
00700
00701
if (
Hive->
DirtyCount == 0) {
00702
return TRUE;
00703 }
00704
00705
HvpTruncateBins(
Hive);
00706
00707
00708
00709
00710
if (
Hive->
HiveFlags &
HIVE_VOLATILE) {
00711
return TRUE;
00712 }
00713
00714
CMLOG(
CML_FLOW,
CMS_IO) {
00715 KdPrint((
"\tDirtyCount:%08lx\n",
Hive->
DirtyCount));
00716 KdPrint((
"\tDirtyVector:"));
00717
DumpDirtyVector(
Hive);
00718 }
00719
00720
00721
00722
00723 oldFlag =
IoSetThreadHardErrorMode(
FALSE);
00724
00725
00726
00727
00728
if (
Hive->
Log ==
TRUE) {
00729
if (
HvpWriteLog(
Hive) ==
FALSE) {
00730
IoSetThreadHardErrorMode(oldFlag);
00731
return FALSE;
00732 }
00733 }
00734
00735
00736
00737
00738
if (
HvpDoWriteHive(
Hive,
HFILE_TYPE_PRIMARY) ==
FALSE) {
00739
IoSetThreadHardErrorMode(oldFlag);
00740
return FALSE;
00741 }
00742
00743
00744
00745
00746
if (
Hive->
Alternate ==
TRUE) {
00747
if (
HvpDoWriteHive(
Hive,
HFILE_TYPE_ALTERNATE) ==
FALSE) {
00748
IoSetThreadHardErrorMode(oldFlag);
00749
return FALSE;
00750 }
00751 }
00752
00753
00754
00755
00756
IoSetThreadHardErrorMode(oldFlag);
00757
00758
00759
00760
00761
00762
HvpDiscardBins(
Hive);
00763
00764
00765
00766
00767
RtlClearAllBits(&(
Hive->
DirtyVector));
00768
Hive->
DirtyCount = 0;
00769
00770
return TRUE;
00771 }
00772
00773
00774 BOOLEAN
00775 HvpDoWriteHive(
00776
PHHIVE Hive,
00777 ULONG FileType
00778 )
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803 {
00804
PHBASE_BLOCK BaseBlock;
00805 ULONG
Offset;
00806 PUCHAR Address;
00807 ULONG Length;
00808 BOOLEAN rc;
00809 ULONG Current;
00810 PRTL_BITMAP
BitMap;
00811
PHMAP_ENTRY Me;
00812
PHBIN Bin;
00813 BOOLEAN ShrinkHive;
00814
PCMP_OFFSET_ARRAY offsetArray;
00815
CMP_OFFSET_ARRAY offsetElement;
00816 ULONG
Count;
00817 ULONG SetBitCount;
00818
00819
CMLOG(
CML_MINOR,
CMS_IO) {
00820 KdPrint((
"HvpDoWriteHive:\n\t"));
00821 KdPrint((
"Hive:%08lx FileType:%08lx\n",
Hive, FileType));
00822 }
00823
00824
00825
00826
00827
00828
if (!(
Hive->
FileFlush)(
Hive, FileType)) {
00829
return(
FALSE);
00830 }
00831
00832 BaseBlock =
Hive->
BaseBlock;
00833
00834
if (BaseBlock->
Length >
Hive->Storage[
Stable].Length) {
00835 ShrinkHive =
TRUE;
00836 }
else {
00837 ShrinkHive =
FALSE;
00838 }
00839
00840
00841
00842
00843
ASSERT(BaseBlock->
Signature ==
HBASE_BLOCK_SIGNATURE);
00844
ASSERT(BaseBlock->
Major ==
HSYS_MAJOR);
00845
ASSERT(BaseBlock->
Format ==
HBASE_FORMAT_MEMORY);
00846
ASSERT(
Hive->
ReadOnly ==
FALSE);
00847
00848
00849
if (BaseBlock->
Sequence1 != BaseBlock->
Sequence2) {
00850
00851
00852
00853
00854
00855
return FALSE;
00856 }
00857
00858 BaseBlock->
Length =
Hive->Storage[
Stable].Length;
00859
00860 BaseBlock->
Sequence1++;
00861 BaseBlock->
Type =
HFILE_TYPE_PRIMARY;
00862 BaseBlock->
Cluster =
Hive->
Cluster;
00863 BaseBlock->
CheckSum =
HvpHeaderCheckSum(BaseBlock);
00864
00865
Offset = 0;
00866 offsetElement.
FileOffset =
Offset;
00867 offsetElement.
DataBuffer = (PVOID) BaseBlock;
00868 offsetElement.
DataLength =
HSECTOR_SIZE *
Hive->
Cluster;
00869 rc = (
Hive->
FileWrite)(
00870
Hive,
00871 FileType,
00872 &offsetElement,
00873 1,
00874 &
Offset
00875 );
00876
00877
if (rc ==
FALSE) {
00878
return FALSE;
00879 }
00880
if ( ! (
Hive->
FileFlush)(
Hive, FileType)) {
00881
return FALSE;
00882 }
00883
Offset =
ROUND_UP(
Offset,
HBLOCK_SIZE);
00884
00885
00886
00887
00888
00889
if (
Hive->
DirtyVector.Buffer !=
NULL) {
00890
00891
00892
00893
00894
BitMap = &(
Hive->
DirtyVector);
00895
00896
ASSERT(RtlCheckBit(
BitMap, 0) == 1);
00897
ASSERT(RtlCheckBit(
BitMap, (
Hive->
Cluster - 1)) == 1);
00898
ASSERT(
sizeof(LIST_ENTRY) >=
sizeof(LARGE_INTEGER));
00899
00900 Me =
HvpGetCellMap(
Hive, 0);
00901
VALIDATE_CELL_MAP(__LINE__,Me,
Hive,0);
00902 Address = (PUCHAR)Me->
BlockAddress;
00903 Length =
Hive->
Cluster *
HSECTOR_SIZE;
00904
Bin = (
PHBIN)Address;
00905
Bin->
TimeStamp = BaseBlock->
TimeStamp;
00906
00907 offsetElement.FileOffset =
Offset;
00908 offsetElement.DataBuffer = (PVOID) Address;
00909 offsetElement.DataLength = Length;
00910 rc = (
Hive->
FileWrite)(
00911
Hive,
00912 FileType,
00913 &offsetElement,
00914 1,
00915 &
Offset
00916 );
00917
ASSERT((
Offset % (
Hive->
Cluster *
HSECTOR_SIZE)) == 0);
00918
if (rc ==
FALSE) {
00919
return FALSE;
00920 }
00921
00922
00923
00924
00925
00926 Current =
Hive->
Cluster;
00927
00928 SetBitCount =
RtlNumberOfSetBits(
BitMap);
00929 offsetArray =
00930 (
PCMP_OFFSET_ARRAY)
00931
ExAllocatePool(
PagedPool,
00932
sizeof(
CMP_OFFSET_ARRAY) * SetBitCount);
00933
if (offsetArray ==
NULL) {
00934
return FALSE;
00935 }
00936
Count = 0;
00937
00938
while (
HvpFindNextDirtyBlock(
00939
Hive,
00940
BitMap,
00941 &Current,
00942 &Address,
00943 &Length,
00944 &
Offset
00945 ) ==
TRUE)
00946 {
00947
00948
ASSERT(
Count < SetBitCount);
00949 offsetArray[
Count].FileOffset =
Offset;
00950 offsetArray[
Count].DataBuffer = Address;
00951 offsetArray[
Count].DataLength = Length;
00952
Offset += Length;
00953
Count++;
00954
ASSERT((
Offset % (
Hive->
Cluster *
HSECTOR_SIZE)) == 0);
00955 }
00956
00957 rc = (
Hive->
FileWrite)(
00958
Hive,
00959 FileType,
00960 offsetArray,
00961
Count,
00962 &
Offset
00963
00964 );
00965
ExFreePool(offsetArray);
00966
if (rc ==
FALSE) {
00967
return FALSE;
00968 }
00969 }
00970
00971
if ( ! (
Hive->
FileFlush)(
Hive, FileType)) {
00972
return FALSE;
00973 }
00974
00975
00976
00977
00978 BaseBlock->
Sequence2++;
00979 BaseBlock->
CheckSum =
HvpHeaderCheckSum(BaseBlock);
00980
Offset = 0;
00981
00982 offsetElement.FileOffset =
Offset;
00983 offsetElement.DataBuffer = (PVOID) BaseBlock;
00984 offsetElement.DataLength =
HSECTOR_SIZE *
Hive->
Cluster;
00985 rc = (
Hive->
FileWrite)(
00986
Hive,
00987 FileType,
00988 &offsetElement,
00989 1,
00990 &
Offset
00991 );
00992
if (rc ==
FALSE) {
00993
return FALSE;
00994 }
00995
00996
if (ShrinkHive) {
00997
00998
00999
01000
CmpDoFileSetSize(
Hive, FileType,
Hive->Storage[
Stable].Length +
HBLOCK_SIZE);
01001 }
01002
01003
if ( ! (
Hive->
FileFlush)(
Hive, FileType)) {
01004
return FALSE;
01005 }
01006
01007
if ((
Hive->
Log) &&
01008 (
Hive->
LogSize >
HLOG_MINSIZE(
Hive))) {
01009
01010
01011
01012
01013
01014
01015
01016
CmpDoFileSetSize(
Hive,
HFILE_TYPE_LOG,
HLOG_MINSIZE(
Hive));
01017
Hive->
LogSize =
HLOG_MINSIZE(
Hive);
01018 }
01019
01020
return TRUE;
01021 }
01022
01023
01024 BOOLEAN
01025 HvpWriteLog(
01026
PHHIVE Hive
01027 )
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046 {
01047
PHBASE_BLOCK BaseBlock;
01048 ULONG
Offset;
01049 PUCHAR Address;
01050 ULONG Length;
01051 BOOLEAN rc;
01052 ULONG Current;
01053 ULONG junk;
01054 ULONG ClusterSize;
01055 ULONG HeaderLength;
01056 PRTL_BITMAP
BitMap;
01057 ULONG DirtyVectorSignature =
HLOG_DV_SIGNATURE;
01058 LARGE_INTEGER systemtime;
01059
PCMP_OFFSET_ARRAY offsetArray;
01060
CMP_OFFSET_ARRAY offsetElement;
01061 ULONG
Count;
01062 ULONG SetBitCount;
01063
01064
CMLOG(
CML_MINOR,
CMS_IO) {
01065 KdPrint((
"HvpWriteLog:\n\t"));
01066 KdPrint((
"Hive:%08lx\n",
Hive));
01067 }
01068
01069
BitMap = &
Hive->
DirtyVector;
01070
01071
01072
01073 BaseBlock =
Hive->
BaseBlock;
01074
ASSERT(BaseBlock->
Signature ==
HBASE_BLOCK_SIGNATURE);
01075
ASSERT(BaseBlock->
Major ==
HSYS_MAJOR);
01076
ASSERT(BaseBlock->
Format ==
HBASE_FORMAT_MEMORY);
01077
ASSERT(
Hive->
ReadOnly ==
FALSE);
01078
01079
01080
if (BaseBlock->
Sequence1 != BaseBlock->
Sequence2) {
01081
01082
01083
01084
01085
01086
return FALSE;
01087 }
01088
01089 BaseBlock->
Sequence1++;
01090
KeQuerySystemTime(&systemtime);
01091 BaseBlock->
TimeStamp = systemtime;
01092
01093 BaseBlock->
Type =
HFILE_TYPE_LOG;
01094
01095 ClusterSize =
Hive->
Cluster *
HSECTOR_SIZE;
01096 HeaderLength =
ROUND_UP(
HLOG_HEADER_SIZE, ClusterSize);
01097 BaseBlock->
Cluster =
Hive->
Cluster;
01098
01099 BaseBlock->
CheckSum =
HvpHeaderCheckSum(BaseBlock);
01100
01101
Offset = 0;
01102 offsetElement.
FileOffset =
Offset;
01103 offsetElement.
DataBuffer = (PVOID) BaseBlock;
01104 offsetElement.
DataLength =
HSECTOR_SIZE *
Hive->
Cluster;
01105 rc = (
Hive->
FileWrite)(
01106
Hive,
01107
HFILE_TYPE_LOG,
01108 &offsetElement,
01109 1,
01110 &
Offset
01111 );
01112
if (rc ==
FALSE) {
01113
return FALSE;
01114 }
01115
Offset =
ROUND_UP(
Offset, HeaderLength);
01116
if ( ! (
Hive->
FileFlush)(
Hive,
HFILE_TYPE_LOG)) {
01117
return FALSE;
01118 }
01119
01120
01121
01122
01123
ASSERT(
sizeof(ULONG) ==
sizeof(DirtyVectorSignature));
01124 offsetElement.FileOffset =
Offset;
01125 offsetElement.DataBuffer = (PVOID) &DirtyVectorSignature;
01126 offsetElement.DataLength =
sizeof(DirtyVectorSignature);
01127 rc = (
Hive->
FileWrite)(
01128
Hive,
01129
HFILE_TYPE_LOG,
01130 &offsetElement,
01131 1,
01132 &
Offset
01133 );
01134
if (rc ==
FALSE) {
01135
return FALSE;
01136 }
01137
01138 Length =
Hive->
DirtyVector.SizeOfBitMap / 8;
01139 Address = (PUCHAR)(
Hive->
DirtyVector.Buffer);
01140 offsetElement.FileOffset =
Offset;
01141 offsetElement.DataBuffer = (PVOID) Address;
01142 offsetElement.DataLength = Length;
01143 rc = (
Hive->
FileWrite)(
01144
Hive,
01145
HFILE_TYPE_LOG,
01146 &offsetElement,
01147 1,
01148 &
Offset
01149 );
01150
if (rc ==
FALSE) {
01151
return FALSE;
01152 }
01153
Offset =
ROUND_UP(
Offset, ClusterSize);
01154
01155
01156
01157
01158 SetBitCount =
RtlNumberOfSetBits(
BitMap);
01159 offsetArray =
01160 (
PCMP_OFFSET_ARRAY)
01161
ExAllocatePool(
PagedPool,
01162
sizeof(
CMP_OFFSET_ARRAY) * SetBitCount);
01163
if (offsetArray ==
NULL) {
01164
return FALSE;
01165 }
01166
Count = 0;
01167
01168 Current = 0;
01169
while (
HvpFindNextDirtyBlock(
01170
Hive,
01171
BitMap,
01172 &Current,
01173 &Address,
01174 &Length,
01175 &junk
01176 ) ==
TRUE)
01177 {
01178
01179
ASSERT(
Count < SetBitCount);
01180 offsetArray[
Count].FileOffset =
Offset;
01181 offsetArray[
Count].DataBuffer = Address;
01182 offsetArray[
Count].DataLength = Length;
01183
Offset += Length;
01184
Count++;
01185
ASSERT((
Offset % ClusterSize) == 0);
01186 }
01187
01188 rc = (
Hive->
FileWrite)(
01189
Hive,
01190
HFILE_TYPE_LOG,
01191 offsetArray,
01192
Count,
01193 &
Offset
01194
01195 );
01196
ExFreePool(offsetArray);
01197
if (rc ==
FALSE) {
01198
return FALSE;
01199 }
01200
01201
if ( ! (
Hive->
FileFlush)(
Hive,
HFILE_TYPE_LOG)) {
01202
return FALSE;
01203 }
01204
01205
01206
01207
01208 BaseBlock->
Sequence2++;
01209 BaseBlock->
CheckSum =
HvpHeaderCheckSum(BaseBlock);
01210
Offset = 0;
01211 offsetElement.FileOffset =
Offset;
01212 offsetElement.DataBuffer = (PVOID) BaseBlock;
01213 offsetElement.DataLength =
HSECTOR_SIZE *
Hive->
Cluster;
01214 rc = (
Hive->
FileWrite)(
01215
Hive,
01216
HFILE_TYPE_LOG,
01217 &offsetElement,
01218 1,
01219 &
Offset
01220 );
01221
if (rc ==
FALSE) {
01222
return FALSE;
01223 }
01224
if ( ! (
Hive->
FileFlush)(
Hive,
HFILE_TYPE_LOG)) {
01225
return FALSE;
01226 }
01227
01228
return TRUE;
01229 }
01230
01231
01232 BOOLEAN
01233 HvpFindNextDirtyBlock(
01234
PHHIVE Hive,
01235 PRTL_BITMAP
BitMap,
01236 PULONG Current,
01237 PUCHAR *Address,
01238 PULONG Length,
01239 PULONG Offset
01240 )
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276 {
01277 ULONG i;
01278 ULONG EndOfBitMap;
01279 ULONG
Start;
01280 ULONG
End;
01281
HCELL_INDEX FileBaseAddress;
01282
HCELL_INDEX FileEndAddress;
01283
PHMAP_ENTRY Me;
01284 PUCHAR Block;
01285 PUCHAR StartBlock;
01286 PUCHAR NextBlock;
01287 ULONG RunSpan;
01288 ULONG RunLength;
01289 ULONG FileLength;
01290
PFREE_HBIN FreeBin;
01291
01292
CMLOG(
CML_FLOW,
CMS_IO) {
01293 KdPrint((
"HvpFindNextDirtyBlock:\n\t"));
01294 KdPrint((
"Hive:%08lx Current:%08lx\n",
Hive, *Current));
01295 }
01296
01297
01298 EndOfBitMap =
BitMap->SizeOfBitMap;
01299
01300
if (*Current >= EndOfBitMap) {
01301
return FALSE;
01302 }
01303
01304
01305
01306
01307
for (i = *Current; i < EndOfBitMap; i++) {
01308
if (RtlCheckBit(
BitMap, i) == 1) {
01309
break;
01310 }
01311 }
01312
Start = i;
01313
01314
for ( ; i < EndOfBitMap; i++) {
01315
if (RtlCheckBit(
BitMap, i) == 0) {
01316
break;
01317 }
01318 }
01319
End = i;
01320
01321
01322
01323
01324
01325 FileBaseAddress =
Start *
HSECTOR_SIZE;
01326 FileEndAddress =
End *
HSECTOR_SIZE;
01327 FileLength = FileEndAddress - FileBaseAddress;
01328
if (FileLength == 0) {
01329 *Address =
NULL;
01330 *Current = 0xffffffff;
01331 *Length = 0;
01332
return FALSE;
01333 }
01334 Me =
HvpGetCellMap(
Hive, FileBaseAddress);
01335
VALIDATE_CELL_MAP(__LINE__,Me,
Hive,FileBaseAddress);
01336
01337
if (Me->
BinAddress &
HMAP_DISCARDABLE) {
01338 FreeBin = (
PFREE_HBIN)Me->
BlockAddress;
01339 StartBlock = (PUCHAR)((Me->
BinAddress &
HMAP_BASE) + FileBaseAddress - FreeBin->
FileOffset );
01340 }
else {
01341 StartBlock = (PUCHAR)Me->
BlockAddress;
01342 }
01343
01344 Block = StartBlock;
01345
ASSERT(((
PHBIN)(Me->
BinAddress &
HMAP_BASE))->Signature ==
HBIN_SIGNATURE);
01346 *Address = Block + (FileBaseAddress &
HCELL_OFFSET_MASK);
01347
01348 *
Offset = FileBaseAddress +
HBLOCK_SIZE;
01349
01350
01351
01352
01353 RunSpan =
HSECTOR_COUNT - (
Start %
HSECTOR_COUNT);
01354
01355
if ((
End -
Start) <= RunSpan) {
01356
01357
01358
01359
01360 *Length = FileLength;
01361 *Current =
End;
01362
return TRUE;
01363
01364 }
else {
01365
01366 RunLength = RunSpan *
HSECTOR_SIZE;
01367 FileBaseAddress =
ROUND_UP(FileBaseAddress+1,
HBLOCK_SIZE);
01368
01369 }
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
while (RunLength < FileLength) {
01380
01381 Me =
HvpGetCellMap(
Hive, FileBaseAddress);
01382
VALIDATE_CELL_MAP(__LINE__,Me,
Hive,FileBaseAddress);
01383
ASSERT(((
PHBIN)(Me->
BinAddress &
HMAP_BASE))->Signature ==
HBIN_SIGNATURE);
01384
01385
if (Me->
BinAddress &
HMAP_DISCARDABLE) {
01386 FreeBin = (
PFREE_HBIN)Me->
BlockAddress;
01387 NextBlock = (PUCHAR)((Me->
BinAddress &
HMAP_BASE) + FileBaseAddress - FreeBin->
FileOffset );
01388 }
else {
01389 NextBlock = (PUCHAR)Me->
BlockAddress;
01390 }
01391
01392
if ( (NextBlock - Block) !=
HBLOCK_SIZE) {
01393
01394
01395
01396
01397
01398
break;
01399 }
01400
01401
01402
if ((FileEndAddress - FileBaseAddress) <=
HBLOCK_SIZE) {
01403
01404
01405
01406
01407
01408 *Length = FileLength;
01409 *Current =
End;
01410
return TRUE;
01411 }
01412
01413
01414
01415
01416 RunLength +=
HBLOCK_SIZE;
01417 RunSpan +=
HSECTOR_COUNT;
01418 FileBaseAddress +=
HBLOCK_SIZE;
01419 Block = NextBlock;
01420 }
01421
01422
01423
01424
01425
01426 *Length = RunLength;
01427 *Current =
Start + RunSpan;
01428
return TRUE;
01429 }
01430
01431
01432
NTSTATUS
01433 HvWriteHive(
01434
PHHIVE Hive
01435 )
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477 {
01478 PULONG SaveDirtyVector;
01479 ULONG SaveDirtyVectorSize;
01480 PULONG AltDirtyVector;
01481 ULONG AltDirtyVectorSize;
01482
PHBASE_BLOCK SaveBaseBlock;
01483
PHBASE_BLOCK AltBaseBlock;
01484 ULONG Alignment;
01485
01486
NTSTATUS status;
01487
01488
01489
CMLOG(
CML_MAJOR,
CMS_IO) {
01490 KdPrint((
"HvWriteHive: \n"));
01491 KdPrint((
"\tHive = %08lx\n"));
01492 }
01493
ASSERT(
Hive->
Signature ==
HHIVE_SIGNATURE);
01494
ASSERT(
Hive->
ReadOnly ==
FALSE);
01495
01496
01497
01498
01499
01500
if (
HvShutdownComplete) {
01501
CMLOG(
CML_BUGCHECK,
CMS_IO) {
01502 KdPrint((
"HvWriteHive: Attempt to write hive AFTER SHUTDOWN\n"));
01503 }
01504
return STATUS_REGISTRY_IO_FAILED;
01505 }
01506
01507
01508
01509
01510 SaveDirtyVector =
Hive->
DirtyVector.Buffer;
01511 SaveDirtyVectorSize =
Hive->
DirtyVector.SizeOfBitMap;
01512 SaveBaseBlock =
Hive->
BaseBlock;
01513
01514 AltDirtyVectorSize = (
Hive->Storage[
Stable].Length /
HSECTOR_SIZE) / 8;
01515 AltDirtyVector = (
Hive->
Allocate)(
ROUND_UP(AltDirtyVectorSize,
sizeof(ULONG)),
FALSE);
01516
if (AltDirtyVector ==
NULL) {
01517 status = STATUS_INSUFFICIENT_RESOURCES;
01518
goto Exit1;
01519 }
01520
Hive->
DirtyVector.Buffer = AltDirtyVector;
01521
Hive->
DirtyVector.SizeOfBitMap = AltDirtyVectorSize * 8;
01522
RtlSetAllBits(&(
Hive->
DirtyVector));
01523
01524
01525
01526
01527 AltBaseBlock = (
Hive->
Allocate)(
sizeof(
HBASE_BLOCK),
TRUE);
01528
if (AltBaseBlock ==
NULL) {
01529 status = STATUS_INSUFFICIENT_RESOURCES;
01530
goto Exit2;
01531 }
01532
01533
01534
01535
01536 Alignment =
Hive->
Cluster *
HSECTOR_SIZE - 1;
01537
if (((ULONG_PTR)AltBaseBlock & Alignment) != 0) {
01538 (
Hive->
Free)(AltBaseBlock,
sizeof(
HBASE_BLOCK));
01539 AltBaseBlock = (
PHBASE_BLOCK)((
Hive->
Allocate)(
PAGE_SIZE,
TRUE));
01540
if (AltBaseBlock ==
NULL) {
01541 status = STATUS_INSUFFICIENT_RESOURCES;
01542
goto Exit2;
01543 }
01544
01545
01546
01547
01548
CmpReleaseGlobalQuota(
PAGE_SIZE -
sizeof(
HBASE_BLOCK));
01549 }
01550
01551 RtlMoveMemory(AltBaseBlock, SaveBaseBlock,
HSECTOR_SIZE);
01552
Hive->
BaseBlock = AltBaseBlock;
01553
01554
01555
01556
01557 status =
CmpDoFileSetSize(
Hive,
01558
HFILE_TYPE_EXTERNAL,
01559
Hive->Storage[
Stable].Length);
01560
01561
if (
NT_SUCCESS(status)) {
01562
if (!
HvpDoWriteHive(
Hive,
HFILE_TYPE_EXTERNAL)) {
01563 status = STATUS_REGISTRY_IO_FAILED;
01564 }
01565 }
01566
01567
01568
01569
01570
CmpFree(AltBaseBlock,
sizeof(
HBASE_BLOCK));
01571
01572 Exit2:
01573
CmpFree(AltDirtyVector,
ROUND_UP(AltDirtyVectorSize,
sizeof(ULONG)));
01574
01575 Exit1:
01576
Hive->
DirtyVector.Buffer = SaveDirtyVector;
01577
Hive->
DirtyVector.SizeOfBitMap = SaveDirtyVectorSize;
01578
Hive->
BaseBlock = SaveBaseBlock;
01579
return status;
01580 }
01581
01582
01583
VOID
01584 HvRefreshHive(
01585
PHHIVE Hive
01586 )
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613 {
01614 ULONG
Offset;
01615 ULONG ReadLength;
01616 ULONG checkstatus;
01617 PUCHAR Address;
01618 ULONG Current;
01619 PRTL_BITMAP
BitMap;
01620 BOOLEAN rc;
01621 ULONG
Start;
01622 ULONG
End;
01623 ULONG BitLength;
01624
HCELL_INDEX TailStart;
01625
HCELL_INDEX p;
01626
PHMAP_ENTRY t;
01627 ULONG i;
01628
PHBIN Bin;
01629 PLIST_ENTRY
List;
01630
PFREE_HBIN FreeBin;
01631
PHMAP_ENTRY Map;
01632
HCELL_INDEX RootCell;
01633
PCM_KEY_NODE RootNode;
01634
HCELL_INDEX LinkCell;
01635
01636
01637
01638
01639
HCELL_INDEX TailDisplay[
HHIVE_FREE_DISPLAY_SIZE];
01640
01641
01642
01643
01644
if (
Hive->
DirtyCount == 0) {
01645
return;
01646 }
01647
ASSERT(
Hive->
HiveFlags &
HIVE_NOLAZYFLUSH);
01648
ASSERT(
Hive->Storage[
Volatile].Length == 0);
01649
01650
01651
01652
01653 checkstatus =
HvCheckHive(
Hive,
NULL);
01654
if (checkstatus != 0) {
01655
KeBugCheckEx(REGISTRY_ERROR,7,0,(ULONG_PTR)
Hive,checkstatus);
01656 }
01657
01658
Hive->
RefreshCount++;
01659
01660
01661
01662
01663
01664 RootCell =
Hive->
BaseBlock->
RootCell;
01665 RootNode = (
PCM_KEY_NODE)
HvGetCell(
Hive, RootCell);
01666 LinkCell = RootNode->
Parent;
01667
01668
01669
01670
01671
01672
01673
List =
Hive->Storage[
Stable].FreeBins.Flink;
01674
while (
List != &
Hive->Storage[
Stable].FreeBins) {
01675
01676 FreeBin = CONTAINING_RECORD(
List,
FREE_HBIN, ListEntry);
01677
List =
List->Flink;
01678
01679
if (FreeBin->
Flags &
FREE_HBIN_DISCARDABLE) {
01680
for (i=0; i<FreeBin->
Size; i+=
HBLOCK_SIZE) {
01681 Map =
HvpGetCellMap(
Hive, FreeBin->
FileOffset+i);
01682
VALIDATE_CELL_MAP(__LINE__,Map,
Hive,FreeBin->
FileOffset+i);
01683 Map->
BlockAddress = (Map->
BinAddress &
HMAP_BASE)+i;
01684 Map->
BinAddress &= ~
HMAP_DISCARDABLE;
01685 }
01686 RemoveEntryList(&FreeBin->
ListEntry);
01687 (
Hive->
Free)(FreeBin,
sizeof(
FREE_HBIN));
01688 }
01689 }
01690
01691
01692
01693
01694
Offset = 0;
01695
if ( (
Hive->
FileRead)(
01696
Hive,
01697
HFILE_TYPE_PRIMARY,
01698 &
Offset,
01699
Hive->
BaseBlock,
01700
HBLOCK_SIZE
01701 ) !=
TRUE)
01702 {
01703
KeBugCheckEx(REGISTRY_ERROR,7,1,0,0);
01704 }
01705 TailStart = (
HCELL_INDEX)(
Hive->
BaseBlock->
Length);
01706
01707
01708
01709
01710
HvFreeHivePartial(
Hive, TailStart,
Stable);
01711
01712
01713
01714
01715
Start =
Hive->
BaseBlock->
Length /
HSECTOR_SIZE;
01716
End =
Hive->
DirtyVector.SizeOfBitMap;
01717 BitLength =
End -
Start;
01718
01719
RtlClearBits(&(
Hive->
DirtyVector),
Start, BitLength);
01720
01721
01722
01723
01724
01725
BitMap = &(
Hive->
DirtyVector);
01726 Current = 0;
01727
while (
HvpFindNextDirtyBlock(
01728
Hive,
01729 &
Hive->
DirtyVector,
01730 &Current, &Address,
01731 &ReadLength,
01732 &
Offset
01733 ))
01734 {
01735
ASSERT(Offset < (Hive->BaseBlock->Length +
sizeof(
HBASE_BLOCK)));
01736 rc = (
Hive->
FileRead)(
01737
Hive,
01738
HFILE_TYPE_PRIMARY,
01739 &
Offset,
01740 (PVOID)Address,
01741 ReadLength
01742 );
01743
if (rc ==
FALSE) {
01744
KeBugCheckEx(REGISTRY_ERROR,7,2,(ULONG_PTR)&
Offset,rc);
01745 }
01746 }
01747
01748
01749
01750
01751
01752
01753
01754 p=0;
01755
while (p <
Hive->Storage[
Stable].Length) {
01756
t =
HvpGetCellMap(
Hive, p);
01757
VALIDATE_CELL_MAP(__LINE__,
t,
Hive,p);
01758
Bin = (
PHBIN)(
t->BlockAddress &
HMAP_BASE);
01759
01760
if ((
t->BinAddress &
HMAP_DISCARDABLE)==0) {
01761
if (RtlCheckBit(&
Hive->
DirtyVector, p /
HSECTOR_SIZE)==1) {
01762
01763
01764
01765
01766
01767
01768
01769
t->BinAddress = (
t->BinAddress & ~
HMAP_BASE) |
t->BlockAddress;
01770
01771
01772
01773
01774
01775
if (
t->BinAddress &
HMAP_NEWALLOC) {
01776
01777
01778
01779
01780
Bin->
MemAlloc = 0;
01781
do {
01782
t =
HvpGetCellMap(
Hive, p+
Bin->
MemAlloc+
HBLOCK_SIZE);
01783
Bin->
MemAlloc +=
HBLOCK_SIZE;
01784
if (p+
Bin->
MemAlloc ==
Hive->Storage[
Stable].Length) {
01785
01786
01787
01788
break;
01789 }
01790
VALIDATE_CELL_MAP(__LINE__,
t,
Hive,p+
Bin->
MemAlloc);
01791 }
while ( (
t->BinAddress &
HMAP_NEWALLOC) == 0);
01792
01793 }
else {
01794
Bin->
MemAlloc = 0;
01795 }
01796 }
01797
01798 p +=
Bin->
Size;
01799
01800 }
else {
01801 FreeBin = (
PFREE_HBIN)
t->BlockAddress;
01802 p += FreeBin->
Size;
01803 }
01804 }
01805
01806
01807
01808
01809 checkstatus =
HvCheckHive(
Hive,
NULL);
01810
if (checkstatus != 0) {
01811
KeBugCheckEx(REGISTRY_ERROR,7,3,(ULONG_PTR)
Hive,checkstatus);
01812 }
01813
01814
01815
01816
01817
for (i = 0; i <
HHIVE_FREE_DISPLAY_SIZE; i++) {
01818
Hive->Storage[
Stable].FreeDisplay[i] =
HCELL_NIL;
01819 TailDisplay[i] =
HCELL_NIL;
01820 }
01821
Hive->Storage[
Stable].FreeSummary = 0;
01822
01823
01824
01825
01826 p = 0;
01827
while (p <
Hive->Storage[
Stable].Length) {
01828
t =
HvpGetCellMap(
Hive, p);
01829
VALIDATE_CELL_MAP(__LINE__,
t,
Hive,p);
01830
01831
if ((
t->BinAddress &
HMAP_DISCARDABLE) == 0) {
01832
Bin = (
PHBIN)((
t->BinAddress) &
HMAP_BASE);
01833
01834
if ( !
HvpEnlistFreeCells(
Hive,
Bin,
Bin->
FileOffset,TailDisplay)) {
01835
KeBugCheckEx(REGISTRY_ERROR,7,5,(ULONG_PTR)
Bin,
Bin->
FileOffset);
01836 }
01837
01838 p = (ULONG)p +
Bin->
Size;
01839 }
else {
01840 FreeBin = (
PFREE_HBIN)
t->BlockAddress;
01841 p = (ULONG)p + FreeBin->
Size;
01842 }
01843 }
01844
01845
01846
01847
01848
01849
01850 RootNode = (
PCM_KEY_NODE)
HvGetCell(
Hive, RootCell);
01851 RootNode->
Parent = LinkCell;
01852 RootNode->
Flags |=
KEY_HIVE_ENTRY |
KEY_NO_DELETE;
01853
01854
01855
01856
01857
01858 checkstatus =
CmCheckRegistry((
PCMHIVE)
Hive,
FALSE);
01859
if (checkstatus != 0) {
01860
KeBugCheckEx(REGISTRY_ERROR,7,6,(ULONG_PTR)
Hive,checkstatus);
01861 }
01862
01863
01864
01865
01866
RtlClearAllBits(&(
Hive->
DirtyVector));
01867
Hive->
DirtyCount = 0;
01868
01869
01870
01871
01872
01873
01874 (
Hive->
FileSetSize)(
01875
Hive,
01876
HFILE_TYPE_PRIMARY,
01877 (
Hive->
BaseBlock->
Length +
HBLOCK_SIZE)
01878 );
01879
01880
return;
01881 }
01882
01883
01884
VOID
01885 HvpDiscardBins(
01886 IN
PHHIVE Hive
01887 )
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907 {
01908
PHBIN Bin;
01909
PHMAP_ENTRY Map;
01910
PHMAP_ENTRY PreviousMap;
01911
PHMAP_ENTRY NextMap;
01912
PFREE_HBIN FreeBin;
01913
PFREE_HBIN PreviousFreeBin;
01914
PFREE_HBIN NextFreeBin;
01915 PLIST_ENTRY
List;
01916
01917
List =
Hive->Storage[
Stable].FreeBins.Flink;
01918
01919
while (
List != &
Hive->Storage[
Stable].FreeBins) {
01920
ASSERT_LISTENTRY(
List);
01921 FreeBin = CONTAINING_RECORD(
List,
FREE_HBIN, ListEntry);
01922
01923
if (FreeBin->
Flags &
FREE_HBIN_DISCARDABLE) {
01924 Map =
HvpGetCellMap(
Hive, FreeBin->
FileOffset);
01925
VALIDATE_CELL_MAP(__LINE__,Map,
Hive,FreeBin->
FileOffset);
01926
Bin = (
PHBIN)(Map->
BinAddress &
HMAP_BASE);
01927
ASSERT(Map->
BinAddress &
HMAP_DISCARDABLE);
01928
01929
01930
01931
01932
01933
01934
01935
ExFreePool(
Bin);
01936 FreeBin->
Flags &= ~
FREE_HBIN_DISCARDABLE;
01937 }
01938
List=
List->Flink;
01939 }
01940
01941 }
01942
01943
01944
01945
VOID
01946 HvpTruncateBins(
01947 IN
PHHIVE Hive
01948 )
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967 {
01968
HSTORAGE_TYPE i;
01969
PHMAP_ENTRY Map;
01970 ULONG NewLength;
01971
PFREE_HBIN FreeBin;
01972
01973
01974
01975
01976
for (i=0;i<
HTYPE_COUNT;i++) {
01977
01978
01979
01980
01981 NewLength =
Hive->Storage[i].Length;
01982
01983
while (NewLength > 0) {
01984 Map =
HvpGetCellMap(
Hive, (NewLength -
HBLOCK_SIZE) + (i*
HCELL_TYPE_MASK));
01985
VALIDATE_CELL_MAP(__LINE__,Map,
Hive,(NewLength -
HBLOCK_SIZE) + (i*
HCELL_TYPE_MASK));
01986
if (Map->
BinAddress &
HMAP_DISCARDABLE) {
01987 FreeBin = (
PFREE_HBIN)Map->
BlockAddress;
01988 NewLength = FreeBin->
FileOffset;
01989 }
else {
01990
break;
01991 }
01992 }
01993
01994
if (NewLength <
Hive->Storage[i].Length) {
01995
01996
01997
01998
HvFreeHivePartial(
Hive, NewLength, i);
01999 }
02000 }
02001 }
02002
02003
#ifdef _WRITE_PROTECTED_REGISTRY_POOL
02004
02005
VOID
02006
HvpChangeBinAllocation(
02007
PHBIN Bin,
02008 BOOLEAN ReadOnly
02009 )
02010 {
02011
ASSERT(
Bin->
Signature == HBIN_SIGNATURE);
02012
02013
02014
02015 }
02016
02017
VOID
02018
HvpMarkBinReadWrite(
02019
PHHIVE Hive,
02020 HCELL_INDEX Cell
02021 )
02022
02023
02024
02025
02026
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040 {
02041 ULONG Type;
02042
PHMAP_ENTRY Me;
02043
PHBIN Bin;
02044
02045
ASSERT(
Hive->
Signature == HHIVE_SIGNATURE);
02046
ASSERT(
Hive->
DirtyCount ==
RtlNumberOfSetBits(&
Hive->
DirtyVector));
02047
02048 Type =
HvGetCellType(Cell);
02049
02050
if ( (
Hive->
HiveFlags &
HIVE_VOLATILE) ||
02051 (Type ==
Volatile) )
02052 {
02053
02054
return;
02055 }
02056
02057 Me =
HvpGetCellMap(Hive, Cell);
02058
VALIDATE_CELL_MAP(__LINE__,Me,Hive,Cell);
02059
Bin = (
PHBIN)(Me->
BinAddress &
HMAP_BASE);
02060
02061
HvpChangeBinAllocation(Bin,FALSE);
02062
02063 }
02064
02065
#endif
02066