2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/config/cminit.c
5 * PURPOSE: Configuration Manager - Hive Initialization
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
15 /* FUNCTIONS *****************************************************************/
19 CmpInitializeHive(OUT PCMHIVE
*RegistryHive
,
20 IN ULONG OperationType
,
23 IN PVOID HiveData OPTIONAL
,
27 IN PCUNICODE_STRING FileName OPTIONAL
,
31 FILE_STANDARD_INFORMATION FileInformation
;
32 IO_STATUS_BLOCK IoStatusBlock
;
33 FILE_FS_SIZE_INFORMATION FileSizeInformation
;
41 * The following are invalid:
42 * An external hive that is also internal.
43 * A log hive that's not a primary hive too.
44 * A volatile hive that's linked to permanent storage.
45 * An in-memory initialization without hive data.
46 * A log hive that's not linked to a correct file type.
48 if (((External
) && ((Primary
) || (Log
))) ||
49 ((Log
) && !(Primary
)) ||
50 ((HiveFlags
& HIVE_VOLATILE
) && ((Primary
) || (External
) || (Log
))) ||
51 ((OperationType
== HINIT_MEMORY
) && (!HiveData
)) ||
52 ((Log
) && (FileType
!= HFILE_TYPE_LOG
)))
54 /* Fail the request */
55 return STATUS_INVALID_PARAMETER
;
58 /* Check if this is a primary hive */
61 /* Get the cluster size */
62 Status
= ZwQueryVolumeInformationFile(Primary
,
65 sizeof(FILE_FS_SIZE_INFORMATION
),
66 FileFsSizeInformation
);
67 if (!NT_SUCCESS(Status
)) return Status
;
69 /* Make sure it's not larger then the block size */
70 if (FileSizeInformation
.BytesPerSector
> HBLOCK_SIZE
)
73 return STATUS_REGISTRY_IO_FAILED
;
76 /* Otherwise, calculate the cluster */
77 Cluster
= FileSizeInformation
.BytesPerSector
/ HSECTOR_SIZE
;
78 Cluster
= max(1, Cluster
);
82 /* Otherwise use cluster 1 */
86 /* Allocate the hive */
87 Hive
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(CMHIVE
), TAG_CM
);
88 if (!Hive
) return STATUS_INSUFFICIENT_RESOURCES
;
90 /* Setup null fields */
91 Hive
->UnloadEvent
= NULL
;
94 Hive
->UnloadWorkItem
= NULL
;
95 Hive
->GrowOnlyMode
= FALSE
;
97 Hive
->CellRemapArray
= NULL
;
98 Hive
->UseCountLog
.Next
= 0;
99 Hive
->LockHiveLog
.Next
= 0;
100 Hive
->FileObject
= NULL
;
101 Hive
->NotifyList
.Flink
= NULL
;
102 Hive
->NotifyList
.Blink
= NULL
;
104 /* Set loading flag */
105 Hive
->HiveIsLoading
= TRUE
;
107 /* Set the current thread as creator */
108 Hive
->CreatorOwner
= KeGetCurrentThread();
110 /* Initialize lists */
111 InitializeListHead(&Hive
->KcbConvertListHead
);
112 InitializeListHead(&Hive
->KnodeConvertListHead
);
113 InitializeListHead(&Hive
->TrustClassEntry
);
115 /* Allocate the view log */
116 Hive
->ViewLock
= ExAllocatePoolWithTag(NonPagedPool
,
117 sizeof(KGUARDED_MUTEX
),
121 /* Cleanup allocation and fail */
122 ExFreePoolWithTag(Hive
, TAG_CM
);
123 return STATUS_INSUFFICIENT_RESOURCES
;
126 /* Allocate the flush lock */
127 Hive
->FlusherLock
= ExAllocatePoolWithTag(NonPagedPool
,
130 if (!Hive
->FlusherLock
)
132 /* Cleanup allocations and fail */
133 ExFreePoolWithTag(Hive
->ViewLock
, TAG_CM
);
134 ExFreePoolWithTag(Hive
, TAG_CM
);
135 return STATUS_INSUFFICIENT_RESOURCES
;
138 /* Setup the handles */
139 Hive
->FileHandles
[HFILE_TYPE_PRIMARY
] = Primary
;
140 Hive
->FileHandles
[HFILE_TYPE_LOG
] = Log
;
141 Hive
->FileHandles
[HFILE_TYPE_EXTERNAL
] = External
;
143 /* Initailize the guarded mutex */
144 KeInitializeGuardedMutex(Hive
->ViewLock
);
145 Hive
->ViewLockOwner
= NULL
;
147 /* Initialize the flush lock */
148 ExInitializeResourceLite(Hive
->FlusherLock
);
150 /* Setup hive locks */
151 ExInitializePushLock(&Hive
->HiveLock
);
152 Hive
->HiveLockOwner
= NULL
;
153 ExInitializePushLock(&Hive
->WriterLock
);
154 Hive
->WriterLockOwner
= NULL
;
155 ExInitializePushLock(&Hive
->SecurityLock
);
156 Hive
->HiveSecurityLockOwner
= NULL
;
158 /* Clear file names */
159 RtlInitEmptyUnicodeString(&Hive
->FileUserName
, NULL
, 0);
160 RtlInitEmptyUnicodeString(&Hive
->FileFullPath
, NULL
, 0);
162 /* Initialize the view list */
163 CmpInitHiveViewList(Hive
);
165 /* Initailize the security cache */
166 CmpInitSecurityCache(Hive
);
170 Hive
->FlushCount
= 0;
173 Hive
->Flags
= HiveFlags
;
175 /* Check if this is a primary */
178 /* Check how large the file is */
179 ZwQueryInformationFile(Primary
,
182 sizeof(FileInformation
),
183 FileStandardInformation
);
184 Cluster
= FileInformation
.EndOfFile
.LowPart
;
188 Status
= HvInitialize(&Hive
->Hive
,
201 if (!NT_SUCCESS(Status
))
203 /* Cleanup allocations and fail */
204 ExFreePoolWithTag(Hive
->FlusherLock
, TAG_CM
);
205 ExFreePoolWithTag(Hive
->ViewLock
, TAG_CM
);
206 ExFreePoolWithTag(Hive
, TAG_CM
);
210 /* Check if we should verify the registry */
211 if ((OperationType
== HINIT_FILE
) ||
212 (OperationType
== HINIT_MEMORY
) ||
213 (OperationType
== HINIT_MEMORY_INPLACE
) ||
214 (OperationType
== HINIT_MAPFILE
))
216 /* Verify integrity */
217 ULONG CheckStatus
= CmCheckRegistry(Hive
, CheckFlags
);
218 if (CheckStatus
!= 0)
220 /* Cleanup allocations and fail */
221 ExFreePoolWithTag(Hive
->FlusherLock
, TAG_CM
);
222 ExFreePoolWithTag(Hive
->ViewLock
, TAG_CM
);
223 ExFreePoolWithTag(Hive
, TAG_CM
);
224 return STATUS_REGISTRY_CORRUPT
;
228 /* Lock the hive list */
229 ExAcquirePushLockExclusive(&CmpHiveListHeadLock
);
231 /* Insert this hive */
232 InsertHeadList(&CmpHiveListHead
, &Hive
->HiveList
);
234 /* Release the lock */
235 ExReleasePushLock(&CmpHiveListHeadLock
);
237 /* Return the hive and success */
238 *RegistryHive
= (PCMHIVE
)Hive
;
239 return STATUS_SUCCESS
;
244 CmpDestroyHive(IN PCMHIVE CmHive
)
246 /* Remove the hive from the list */
247 ExAcquirePushLockExclusive(&CmpHiveListHeadLock
);
248 RemoveEntryList(&CmHive
->HiveList
);
249 ExReleasePushLock(&CmpHiveListHeadLock
);
251 /* Delete the flusher lock */
252 ExDeleteResourceLite(CmHive
->FlusherLock
);
253 ExFreePoolWithTag(CmHive
->FlusherLock
, TAG_CM
);
255 /* Delete the view lock */
256 ExFreePoolWithTag(CmHive
->ViewLock
, TAG_CM
);
259 HvFree(&CmHive
->Hive
);
261 return STATUS_SUCCESS
;
266 CmpOpenHiveFiles(IN PCUNICODE_STRING BaseName
,
267 IN PCWSTR Extension OPTIONAL
,
270 OUT PULONG PrimaryDisposition
,
271 OUT PULONG LogDisposition
,
272 IN BOOLEAN CreateAllowed
,
273 IN BOOLEAN MarkAsSystemHive
,
274 IN BOOLEAN NoBuffering
,
275 OUT PULONG ClusterSize OPTIONAL
)
280 UNICODE_STRING FullName
, ExtensionName
;
283 OBJECT_ATTRIBUTES ObjectAttributes
;
284 IO_STATUS_BLOCK IoStatusBlock
;
285 ULONG AttributeFlags
, ShareMode
, DesiredAccess
, CreateDisposition
, IoFlags
;
286 USHORT CompressionState
;
287 FILE_STANDARD_INFORMATION FileInformation
;
288 FILE_FS_SIZE_INFORMATION FsSizeInformation
;
291 Status
= CmpCreateEvent(NotificationEvent
, &EventHandle
, &Event
);
292 if (!NT_SUCCESS(Status
)) return Status
;
294 /* Initialize the full name */
295 RtlInitEmptyUnicodeString(&FullName
, NULL
, 0);
296 Length
= BaseName
->Length
;
298 /* Check if we have an extension */
301 /* Update the name length */
302 Length
+= (USHORT
)wcslen(Extension
) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
304 /* Allocate the buffer for the full name */
305 NameBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
309 ObDereferenceObject(Event
);
310 ZwClose(EventHandle
);
311 return STATUS_NO_MEMORY
;
314 /* Build the full name */
315 FullName
.Buffer
= NameBuffer
;
316 FullName
.MaximumLength
= Length
;
317 RtlAppendUnicodeStringToString(&FullName
, BaseName
);
321 /* The base name is the full name */
322 FullName
= *BaseName
;
326 /* Initialize the attributes */
327 InitializeObjectAttributes(&ObjectAttributes
,
329 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
333 /* Check if we can create the hive */
334 if ((CreateAllowed
) && !(CmpShareSystemHives
))
336 /* Open only or create */
337 CreateDisposition
= FILE_OPEN_IF
;
342 CreateDisposition
= FILE_OPEN
;
345 /* Setup the flags */
346 // FIXME : FILE_OPEN_FOR_BACKUP_INTENT is unimplemented and breaks 3rd stage boot
347 IoFlags
= //FILE_OPEN_FOR_BACKUP_INTENT |
348 FILE_NO_COMPRESSION
|
350 (NoBuffering
? FILE_NO_INTERMEDIATE_BUFFERING
: 0);
352 /* Set share and access modes */
353 if ((CmpMiniNTBoot
) && (CmpShareSystemHives
))
355 /* We're on Live CD or otherwise sharing */
356 DesiredAccess
= FILE_READ_DATA
;
357 ShareMode
= FILE_SHARE_READ
;
361 /* We want to write exclusively */
363 DesiredAccess
= FILE_READ_DATA
| FILE_WRITE_DATA
;
366 /* Default attributes */
367 AttributeFlags
= FILE_ATTRIBUTE_NORMAL
;
369 /* Now create the file */
370 Status
= ZwCreateFile(Primary
,
371 DesiredAccess
| SYNCHRONIZE
,
378 FILE_SYNCHRONOUS_IO_NONALERT
| IoFlags
,
381 /* Check if anything failed until now */
382 if (!NT_SUCCESS(Status
))
384 /* Close handles and free buffers */
385 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
386 ObDereferenceObject(Event
);
387 ZwClose(EventHandle
);
388 DPRINT1("ZwCreateFile failed : %lx.\n", Status
);
393 if (MarkAsSystemHive
)
395 /* We opened it, mark it as a system hive */
396 Status
= ZwFsControlFile(*Primary
,
401 FSCTL_MARK_AS_SYSTEM_HIVE
,
406 if (Status
== STATUS_PENDING
)
408 /* Wait for completion */
409 KeWaitForSingleObject(Event
,
414 Status
= IoStatusBlock
.Status
;
417 /* If we don't support it, ignore the failure */
418 if (Status
== STATUS_INVALID_DEVICE_REQUEST
) Status
= STATUS_SUCCESS
;
420 if (!NT_SUCCESS(Status
))
422 /* Close handles and free buffers */
423 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
424 ObDereferenceObject(Event
);
425 ZwClose(EventHandle
);
432 /* Disable compression */
433 CompressionState
= 0;
434 Status
= ZwFsControlFile(*Primary
,
439 FSCTL_SET_COMPRESSION
,
441 sizeof(CompressionState
),
444 if (Status
== STATUS_PENDING
)
446 /* Wait for completion */
447 KeWaitForSingleObject(Event
,
454 /* Get the disposition */
455 *PrimaryDisposition
= (ULONG
)IoStatusBlock
.Information
;
456 if (IoStatusBlock
.Information
!= FILE_CREATED
)
458 /* Check how large the file is */
459 Status
= ZwQueryInformationFile(*Primary
,
462 sizeof(FileInformation
),
463 FileStandardInformation
);
464 if (NT_SUCCESS(Status
))
466 /* Check if it's 0 bytes */
467 if (!FileInformation
.EndOfFile
.QuadPart
)
469 /* Assume it's a new file */
470 *PrimaryDisposition
= FILE_CREATED
;
475 /* Check if the caller wants cluster size returned */
479 Status
= ZwQueryVolumeInformationFile(*Primary
,
482 sizeof(FsSizeInformation
),
483 FileFsSizeInformation
);
484 if (!NT_SUCCESS(Status
))
486 /* Close handles and free buffers */
487 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
488 ObDereferenceObject(Event
);
489 ZwClose(EventHandle
);
493 /* Check if the sector size is invalid */
494 if (FsSizeInformation
.BytesPerSector
> HBLOCK_SIZE
)
496 /* Close handles and free buffers */
497 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
498 ObDereferenceObject(Event
);
499 ZwClose(EventHandle
);
500 return STATUS_CANNOT_LOAD_REGISTRY_FILE
;
503 /* Return cluster size */
504 *ClusterSize
= max(1, FsSizeInformation
.BytesPerSector
/ HSECTOR_SIZE
);
507 /* Check if we don't need to create a log file */
510 /* We're done, close handles */
511 ObDereferenceObject(Event
);
512 ZwClose(EventHandle
);
513 return STATUS_SUCCESS
;
516 /* Check if we can create the hive */
517 CreateDisposition
= CmpShareSystemHives
? FILE_OPEN
: FILE_OPEN_IF
;
518 if (*PrimaryDisposition
== FILE_CREATED
)
520 /* Over-write the existing log file, since this is a new hive */
521 CreateDisposition
= FILE_SUPERSEDE
;
525 RtlInitUnicodeString(&ExtensionName
, Extension
);
526 RtlAppendUnicodeStringToString(&FullName
, &ExtensionName
);
528 /* Initialize the attributes */
529 InitializeObjectAttributes(&ObjectAttributes
,
531 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
535 /* Setup the flags */
536 IoFlags
= FILE_NO_COMPRESSION
| FILE_NO_INTERMEDIATE_BUFFERING
;
538 /* Check if this is a log file */
539 if (!_wcsnicmp(Extension
, L
".log", 4))
542 AttributeFlags
|= FILE_ATTRIBUTE_HIDDEN
;
545 /* Now create the file */
546 Status
= ZwCreateFile(Log
,
557 if ((NT_SUCCESS(Status
)) && (MarkAsSystemHive
))
559 /* We opened it, mark it as a system hive */
560 Status
= ZwFsControlFile(*Log
,
565 FSCTL_MARK_AS_SYSTEM_HIVE
,
570 if (Status
== STATUS_PENDING
)
572 /* Wait for completion */
573 KeWaitForSingleObject(Event
,
578 Status
= IoStatusBlock
.Status
;
581 /* If we don't support it, ignore the failure */
582 if (Status
== STATUS_INVALID_DEVICE_REQUEST
) Status
= STATUS_SUCCESS
;
584 /* If we failed, close the handle */
585 if (!NT_SUCCESS(Status
)) ZwClose(*Log
);
588 /* Check if anything failed until now */
589 if (!NT_SUCCESS(Status
))
591 /* Clear the handle */
596 /* Disable compression */
597 Status
= ZwFsControlFile(*Log
,
602 FSCTL_SET_COMPRESSION
,
604 sizeof(CompressionState
),
607 if (Status
== STATUS_PENDING
)
609 /* Wait for completion */
610 KeWaitForSingleObject(Event
,
617 /* Return the disposition */
618 *LogDisposition
= (ULONG
)IoStatusBlock
.Information
;
621 /* We're done, close handles and free buffers */
622 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
623 ObDereferenceObject(Event
);
624 ZwClose(EventHandle
);
625 return STATUS_SUCCESS
;