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
*CmHive
,
20 IN ULONG OperationType
,
23 IN PVOID HiveData OPTIONAL
,
27 IN PCUNICODE_STRING FileName OPTIONAL
,
31 IO_STATUS_BLOCK IoStatusBlock
;
32 FILE_FS_SIZE_INFORMATION FileSizeInformation
;
40 * The following are invalid:
41 * An external hive that is also internal.
42 * A log hive that's not a primary hive too.
43 * A volatile hive that's linked to permanent storage.
44 * An in-memory initialization without hive data.
45 * A log hive that's not linked to a correct file type.
47 if (((External
) && ((Primary
) || (Log
))) ||
48 ((Log
) && !(Primary
)) ||
49 ((HiveFlags
& HIVE_VOLATILE
) && ((Primary
) || (External
) || (Log
))) ||
50 ((OperationType
== HINIT_MEMORY
) && (!HiveData
)) ||
51 ((Log
) && (FileType
!= HFILE_TYPE_LOG
)))
53 /* Fail the request */
54 return STATUS_INVALID_PARAMETER
;
57 /* Check if this is a primary hive */
60 /* Get the cluster size */
61 Status
= ZwQueryVolumeInformationFile(Primary
,
64 sizeof(FILE_FS_SIZE_INFORMATION
),
65 FileFsSizeInformation
);
66 if (!NT_SUCCESS(Status
)) return Status
;
68 /* Make sure it's not larger then the block size */
69 if (FileSizeInformation
.BytesPerSector
> HBLOCK_SIZE
)
72 return STATUS_REGISTRY_IO_FAILED
;
75 /* Otherwise, calculate the cluster */
76 Cluster
= FileSizeInformation
.BytesPerSector
/ HSECTOR_SIZE
;
77 Cluster
= max(1, Cluster
);
81 /* Otherwise use cluster 1 */
85 /* Allocate the hive */
86 Hive
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(CMHIVE
), TAG_CMHIVE
);
87 if (!Hive
) return STATUS_INSUFFICIENT_RESOURCES
;
89 /* Setup null fields */
90 Hive
->UnloadEvent
= NULL
;
93 Hive
->UnloadWorkItem
= NULL
;
94 Hive
->GrowOnlyMode
= FALSE
;
96 Hive
->CellRemapArray
= NULL
;
97 Hive
->UseCountLog
.Next
= 0;
98 Hive
->LockHiveLog
.Next
= 0;
99 Hive
->FileObject
= NULL
;
100 Hive
->NotifyList
.Flink
= NULL
;
101 Hive
->NotifyList
.Blink
= NULL
;
103 /* Set loading flag */
104 Hive
->HiveIsLoading
= TRUE
;
106 /* Set the current thread as creator */
107 Hive
->CreatorOwner
= KeGetCurrentThread();
109 /* Initialize lists */
110 InitializeListHead(&Hive
->KcbConvertListHead
);
111 InitializeListHead(&Hive
->KnodeConvertListHead
);
112 InitializeListHead(&Hive
->TrustClassEntry
);
114 /* Allocate the view log */
115 Hive
->ViewLock
= ExAllocatePoolWithTag(NonPagedPool
,
116 sizeof(KGUARDED_MUTEX
),
120 /* Cleanup allocation and fail */
121 ExFreePoolWithTag(Hive
, TAG_CMHIVE
);
122 return STATUS_INSUFFICIENT_RESOURCES
;
125 /* Allocate the flush lock */
126 Hive
->FlusherLock
= ExAllocatePoolWithTag(NonPagedPool
,
129 if (!Hive
->FlusherLock
)
131 /* Cleanup allocations and fail */
132 ExFreePoolWithTag(Hive
->ViewLock
, TAG_CMHIVE
);
133 ExFreePoolWithTag(Hive
, TAG_CMHIVE
);
134 return STATUS_INSUFFICIENT_RESOURCES
;
137 /* Setup the handles */
138 Hive
->FileHandles
[HFILE_TYPE_PRIMARY
] = Primary
;
139 Hive
->FileHandles
[HFILE_TYPE_LOG
] = Log
;
140 Hive
->FileHandles
[HFILE_TYPE_EXTERNAL
] = External
;
142 /* Initailize the guarded mutex */
143 KeInitializeGuardedMutex(Hive
->ViewLock
);
144 Hive
->ViewLockOwner
= NULL
;
146 /* Initialize the flush lock */
147 ExInitializeResourceLite(Hive
->FlusherLock
);
149 /* Setup hive locks */
150 ExInitializePushLock(&Hive
->HiveLock
);
151 Hive
->HiveLockOwner
= NULL
;
152 ExInitializePushLock(&Hive
->WriterLock
);
153 Hive
->WriterLockOwner
= NULL
;
154 ExInitializePushLock(&Hive
->SecurityLock
);
155 Hive
->HiveSecurityLockOwner
= NULL
;
157 /* Clear file names */
158 RtlInitEmptyUnicodeString(&Hive
->FileUserName
, NULL
, 0);
159 RtlInitEmptyUnicodeString(&Hive
->FileFullPath
, NULL
, 0);
161 /* Initialize the view list */
162 CmpInitHiveViewList(Hive
);
164 /* Initailize the security cache */
165 CmpInitSecurityCache(Hive
);
169 Hive
->FlushCount
= 0;
172 Status
= HvInitialize(&Hive
->Hive
,
185 if (!NT_SUCCESS(Status
))
187 /* Cleanup allocations and fail */
188 ExDeleteResourceLite(Hive
->FlusherLock
);
189 ExFreePoolWithTag(Hive
->FlusherLock
, TAG_CMHIVE
);
190 ExFreePoolWithTag(Hive
->ViewLock
, TAG_CMHIVE
);
191 ExFreePoolWithTag(Hive
, TAG_CMHIVE
);
195 /* Check if we should verify the registry */
196 if ((OperationType
== HINIT_FILE
) ||
197 (OperationType
== HINIT_MEMORY
) ||
198 (OperationType
== HINIT_MEMORY_INPLACE
) ||
199 (OperationType
== HINIT_MAPFILE
))
201 /* Verify integrity */
202 ULONG CheckStatus
= CmCheckRegistry(Hive
, CheckFlags
);
203 if (CheckStatus
!= 0)
205 /* Cleanup allocations and fail */
206 ExDeleteResourceLite(Hive
->FlusherLock
);
207 ExFreePoolWithTag(Hive
->FlusherLock
, TAG_CMHIVE
);
208 ExFreePoolWithTag(Hive
->ViewLock
, TAG_CMHIVE
);
209 ExFreePoolWithTag(Hive
, TAG_CMHIVE
);
210 return STATUS_REGISTRY_CORRUPT
;
214 /* Lock the hive list */
215 ExAcquirePushLockExclusive(&CmpHiveListHeadLock
);
217 /* Insert this hive */
218 InsertHeadList(&CmpHiveListHead
, &Hive
->HiveList
);
220 /* Release the lock */
221 ExReleasePushLock(&CmpHiveListHeadLock
);
223 /* Return the hive and success */
225 return STATUS_SUCCESS
;
230 CmpDestroyHive(IN PCMHIVE CmHive
)
232 /* Remove the hive from the list */
233 ExAcquirePushLockExclusive(&CmpHiveListHeadLock
);
234 RemoveEntryList(&CmHive
->HiveList
);
235 ExReleasePushLock(&CmpHiveListHeadLock
);
237 /* Destroy the security descriptor cache */
238 CmpDestroySecurityCache(CmHive
);
240 /* Destroy the view list */
241 CmpDestroyHiveViewList(CmHive
);
243 /* Delete the flusher lock */
244 ExDeleteResourceLite(CmHive
->FlusherLock
);
245 ExFreePoolWithTag(CmHive
->FlusherLock
, TAG_CMHIVE
);
247 /* Delete the view lock */
248 ExFreePoolWithTag(CmHive
->ViewLock
, TAG_CMHIVE
);
250 /* Free the hive storage */
251 HvFree(&CmHive
->Hive
);
254 CmpFree(CmHive
, TAG_CM
);
256 return STATUS_SUCCESS
;
261 CmpOpenHiveFiles(IN PCUNICODE_STRING BaseName
,
262 IN PCWSTR Extension OPTIONAL
,
265 OUT PULONG PrimaryDisposition
,
266 OUT PULONG LogDisposition
,
267 IN BOOLEAN CreateAllowed
,
268 IN BOOLEAN MarkAsSystemHive
,
269 IN BOOLEAN NoBuffering
,
270 OUT PULONG ClusterSize OPTIONAL
)
275 UNICODE_STRING FullName
, ExtensionName
;
278 OBJECT_ATTRIBUTES ObjectAttributes
;
279 IO_STATUS_BLOCK IoStatusBlock
;
280 ULONG AttributeFlags
, ShareMode
, DesiredAccess
, CreateDisposition
, IoFlags
;
281 USHORT CompressionState
;
282 FILE_STANDARD_INFORMATION FileInformation
;
283 FILE_FS_SIZE_INFORMATION FsSizeInformation
;
286 Status
= CmpCreateEvent(NotificationEvent
, &EventHandle
, &Event
);
287 if (!NT_SUCCESS(Status
)) return Status
;
289 /* Initialize the full name */
290 RtlInitEmptyUnicodeString(&FullName
, NULL
, 0);
291 Length
= BaseName
->Length
;
293 /* Check if we have an extension */
296 /* Update the name length */
297 Length
+= (USHORT
)wcslen(Extension
) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
299 /* Allocate the buffer for the full name */
300 NameBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
304 ObDereferenceObject(Event
);
305 ZwClose(EventHandle
);
306 return STATUS_NO_MEMORY
;
309 /* Build the full name */
310 FullName
.Buffer
= NameBuffer
;
311 FullName
.MaximumLength
= Length
;
312 RtlAppendUnicodeStringToString(&FullName
, BaseName
);
316 /* The base name is the full name */
317 FullName
= *BaseName
;
321 /* Initialize the attributes */
322 InitializeObjectAttributes(&ObjectAttributes
,
324 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
328 /* Check if we can create the hive */
329 if ((CreateAllowed
) && !(CmpShareSystemHives
))
331 /* Open only or create */
332 CreateDisposition
= FILE_OPEN_IF
;
337 CreateDisposition
= FILE_OPEN
;
340 /* Setup the flags */
341 // FIXME : FILE_OPEN_FOR_BACKUP_INTENT is unimplemented and breaks 3rd stage boot
342 IoFlags
= //FILE_OPEN_FOR_BACKUP_INTENT |
343 FILE_NO_COMPRESSION
|
345 (NoBuffering
? FILE_NO_INTERMEDIATE_BUFFERING
: 0);
347 /* Set share and access modes */
348 if ((CmpMiniNTBoot
) && (CmpShareSystemHives
))
350 /* We're on Live CD or otherwise sharing */
351 DesiredAccess
= FILE_READ_DATA
;
352 ShareMode
= FILE_SHARE_READ
;
356 /* We want to write exclusively */
358 DesiredAccess
= FILE_READ_DATA
| FILE_WRITE_DATA
;
361 /* Default attributes */
362 AttributeFlags
= FILE_ATTRIBUTE_NORMAL
;
364 /* Now create the file */
365 Status
= ZwCreateFile(Primary
,
366 DesiredAccess
| SYNCHRONIZE
,
373 FILE_SYNCHRONOUS_IO_NONALERT
| IoFlags
,
376 /* Check if anything failed until now */
377 if (!NT_SUCCESS(Status
))
379 /* Close handles and free buffers */
380 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
381 ObDereferenceObject(Event
);
382 ZwClose(EventHandle
);
383 DPRINT1("ZwCreateFile failed : %lx.\n", Status
);
388 if (MarkAsSystemHive
)
390 /* We opened it, mark it as a system hive */
391 Status
= ZwFsControlFile(*Primary
,
396 FSCTL_MARK_AS_SYSTEM_HIVE
,
401 if (Status
== STATUS_PENDING
)
403 /* Wait for completion */
404 KeWaitForSingleObject(Event
,
409 Status
= IoStatusBlock
.Status
;
412 /* If we don't support it, ignore the failure */
413 if (Status
== STATUS_INVALID_DEVICE_REQUEST
) Status
= STATUS_SUCCESS
;
415 if (!NT_SUCCESS(Status
))
417 /* Close handles and free buffers */
418 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
419 ObDereferenceObject(Event
);
420 ZwClose(EventHandle
);
427 /* Disable compression */
428 CompressionState
= 0;
429 Status
= ZwFsControlFile(*Primary
,
434 FSCTL_SET_COMPRESSION
,
436 sizeof(CompressionState
),
439 if (Status
== STATUS_PENDING
)
441 /* Wait for completion */
442 KeWaitForSingleObject(Event
,
449 /* Get the disposition */
450 *PrimaryDisposition
= (ULONG
)IoStatusBlock
.Information
;
451 if (IoStatusBlock
.Information
!= FILE_CREATED
)
453 /* Check how large the file is */
454 Status
= ZwQueryInformationFile(*Primary
,
457 sizeof(FileInformation
),
458 FileStandardInformation
);
459 if (NT_SUCCESS(Status
))
461 /* Check if it's 0 bytes */
462 if (!FileInformation
.EndOfFile
.QuadPart
)
464 /* Assume it's a new file */
465 *PrimaryDisposition
= FILE_CREATED
;
470 /* Check if the caller wants cluster size returned */
474 Status
= ZwQueryVolumeInformationFile(*Primary
,
477 sizeof(FsSizeInformation
),
478 FileFsSizeInformation
);
479 if (!NT_SUCCESS(Status
))
481 /* Close handles and free buffers */
482 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
483 ObDereferenceObject(Event
);
484 ZwClose(EventHandle
);
488 /* Check if the sector size is invalid */
489 if (FsSizeInformation
.BytesPerSector
> HBLOCK_SIZE
)
491 /* Close handles and free buffers */
492 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
493 ObDereferenceObject(Event
);
494 ZwClose(EventHandle
);
495 return STATUS_CANNOT_LOAD_REGISTRY_FILE
;
498 /* Return cluster size */
499 *ClusterSize
= max(1, FsSizeInformation
.BytesPerSector
/ HSECTOR_SIZE
);
502 /* Check if we don't need to create a log file */
505 /* We're done, close handles */
506 ObDereferenceObject(Event
);
507 ZwClose(EventHandle
);
508 return STATUS_SUCCESS
;
511 /* Check if we can create the hive */
512 CreateDisposition
= CmpShareSystemHives
? FILE_OPEN
: FILE_OPEN_IF
;
513 if (*PrimaryDisposition
== FILE_CREATED
)
515 /* Over-write the existing log file, since this is a new hive */
516 CreateDisposition
= FILE_SUPERSEDE
;
520 RtlInitUnicodeString(&ExtensionName
, Extension
);
521 RtlAppendUnicodeStringToString(&FullName
, &ExtensionName
);
523 /* Initialize the attributes */
524 InitializeObjectAttributes(&ObjectAttributes
,
526 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
530 /* Setup the flags */
531 IoFlags
= FILE_NO_COMPRESSION
| FILE_NO_INTERMEDIATE_BUFFERING
;
533 /* Check if this is a log file */
534 if (!_wcsnicmp(Extension
, L
".log", 4))
537 AttributeFlags
|= FILE_ATTRIBUTE_HIDDEN
;
540 /* Now create the file */
541 Status
= ZwCreateFile(Log
,
552 if ((NT_SUCCESS(Status
)) && (MarkAsSystemHive
))
554 /* We opened it, mark it as a system hive */
555 Status
= ZwFsControlFile(*Log
,
560 FSCTL_MARK_AS_SYSTEM_HIVE
,
565 if (Status
== STATUS_PENDING
)
567 /* Wait for completion */
568 KeWaitForSingleObject(Event
,
573 Status
= IoStatusBlock
.Status
;
576 /* If we don't support it, ignore the failure */
577 if (Status
== STATUS_INVALID_DEVICE_REQUEST
) Status
= STATUS_SUCCESS
;
579 /* If we failed, close the handle */
580 if (!NT_SUCCESS(Status
)) ZwClose(*Log
);
583 /* Check if anything failed until now */
584 if (!NT_SUCCESS(Status
))
586 /* Clear the handle */
591 /* Disable compression */
592 Status
= ZwFsControlFile(*Log
,
597 FSCTL_SET_COMPRESSION
,
599 sizeof(CompressionState
),
602 if (Status
== STATUS_PENDING
)
604 /* Wait for completion */
605 KeWaitForSingleObject(Event
,
612 /* Return the disposition */
613 *LogDisposition
= (ULONG
)IoStatusBlock
.Information
;
616 /* We're done, close handles and free buffers */
617 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
618 ObDereferenceObject(Event
);
619 ZwClose(EventHandle
);
620 return STATUS_SUCCESS
;
625 CmpCloseHiveFiles(IN PCMHIVE Hive
)
629 for (i
= 0; i
< HFILE_TYPE_MAX
; i
++)
631 if (Hive
->FileHandles
[i
] != NULL
)
633 ZwClose(Hive
->FileHandles
[i
]);
634 Hive
->FileHandles
[i
] = NULL
;