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
),
119 if (!Hive
->ViewLock
) return STATUS_INSUFFICIENT_RESOURCES
;
121 /* Allocate the flush lock */
122 Hive
->FlusherLock
= ExAllocatePoolWithTag(NonPagedPool
,
125 if (!Hive
->FlusherLock
) return STATUS_INSUFFICIENT_RESOURCES
;
127 /* Setup the handles */
128 Hive
->FileHandles
[HFILE_TYPE_PRIMARY
] = Primary
;
129 Hive
->FileHandles
[HFILE_TYPE_LOG
] = Log
;
130 Hive
->FileHandles
[HFILE_TYPE_EXTERNAL
] = External
;
132 /* Initailize the guarded mutex */
133 KeInitializeGuardedMutex(Hive
->ViewLock
);
134 Hive
->ViewLockOwner
= NULL
;
136 /* Initialize the flush lock */
137 ExInitializeResourceLite(Hive
->FlusherLock
);
139 /* Setup hive locks */
140 ExInitializePushLock(&Hive
->HiveLock
);
141 Hive
->HiveLockOwner
= NULL
;
142 ExInitializePushLock(&Hive
->WriterLock
);
143 Hive
->WriterLockOwner
= NULL
;
144 ExInitializePushLock(&Hive
->SecurityLock
);
145 Hive
->HiveSecurityLockOwner
= NULL
;
147 /* Clear file names */
148 RtlInitEmptyUnicodeString(&Hive
->FileUserName
, NULL
, 0);
149 RtlInitEmptyUnicodeString(&Hive
->FileFullPath
, NULL
, 0);
151 /* Initialize the view list */
152 CmpInitHiveViewList(Hive
);
154 /* Initailize the security cache */
155 CmpInitSecurityCache(Hive
);
159 Hive
->FlushCount
= 0;
162 Hive
->Flags
= HiveFlags
;
164 /* Check if this is a primary */
167 /* Check how large the file is */
168 ZwQueryInformationFile(Primary
,
171 sizeof(FileInformation
),
172 FileStandardInformation
);
173 Cluster
= FileInformation
.EndOfFile
.LowPart
;
177 Status
= HvInitialize(&Hive
->Hive
,
189 (PUNICODE_STRING
)FileName
);
190 if (!NT_SUCCESS(Status
))
192 /* Clear allocations and fail */
193 ExFreePool(Hive
->ViewLock
);
194 ExFreePool(Hive
->FlusherLock
);
199 /* Check if we should verify the registry */
200 if ((OperationType
== HINIT_FILE
) ||
201 (OperationType
== HINIT_MEMORY
) ||
202 (OperationType
== HINIT_MEMORY_INPLACE
) ||
203 (OperationType
== HINIT_MAPFILE
))
205 /* Verify integrity */
206 if (CmCheckRegistry((PCMHIVE
)Hive
, TRUE
))
208 /* Free all alocations */
209 ExFreePool(Hive
->ViewLock
);
210 ExFreePool(Hive
->FlusherLock
);
212 return STATUS_REGISTRY_CORRUPT
;
216 /* Lock the hive list */
217 ExAcquirePushLockExclusive(&CmpHiveListHeadLock
);
219 /* Insert this hive */
220 InsertHeadList(&CmpHiveListHead
, &Hive
->HiveList
);
222 /* Release the lock */
223 ExReleasePushLock(&CmpHiveListHeadLock
);
225 /* Return the hive and success */
226 *RegistryHive
= (PCMHIVE
)Hive
;
227 return STATUS_SUCCESS
;
232 CmpOpenHiveFiles(IN PCUNICODE_STRING BaseName
,
233 IN PCWSTR Extension OPTIONAL
,
236 IN PULONG PrimaryDisposition
,
237 IN PULONG LogDisposition
,
238 IN BOOLEAN CreateAllowed
,
239 IN BOOLEAN MarkAsSystemHive
,
240 IN BOOLEAN NoBuffering
,
241 OUT PULONG ClusterSize OPTIONAL
)
246 UNICODE_STRING FullName
, ExtensionName
;
249 OBJECT_ATTRIBUTES ObjectAttributes
;
250 IO_STATUS_BLOCK IoStatusBlock
;
251 ULONG AttributeFlags
, ShareMode
, DesiredAccess
, CreateDisposition
, IoFlags
;
252 USHORT CompressionState
;
253 FILE_STANDARD_INFORMATION FileInformation
;
254 FILE_FS_SIZE_INFORMATION FsSizeInformation
;
257 Status
= CmpCreateEvent(NotificationEvent
, &EventHandle
, &Event
);
258 if (!NT_SUCCESS(Status
)) return Status
;
260 /* Initialize the full name */
261 RtlInitEmptyUnicodeString(&FullName
, NULL
, 0);
262 Length
= BaseName
->Length
;
264 /* Check if we have an extension */
267 /* Update the name length */
268 Length
+= (USHORT
)wcslen(Extension
) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
270 /* Allocate the buffer for the full name */
271 NameBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
275 ObDereferenceObject(Event
);
276 ZwClose(EventHandle
);
277 return STATUS_NO_MEMORY
;
280 /* Build the full name */
281 FullName
.Buffer
= NameBuffer
;
282 FullName
.MaximumLength
= Length
;
283 RtlAppendUnicodeStringToString(&FullName
, BaseName
);
287 /* The base name is the full name */
288 FullName
= *BaseName
;
292 /* Initialize the attributes */
293 InitializeObjectAttributes(&ObjectAttributes
,
295 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
299 /* Check if we can create the hive */
300 if ((CreateAllowed
) && !(CmpShareSystemHives
))
302 /* Open only or create */
303 CreateDisposition
= FILE_OPEN_IF
;
308 CreateDisposition
= FILE_OPEN
;
311 /* Setup the flags */
312 // FIXME : FILE_OPEN_FOR_BACKUP_INTENT is unimplemented and breaks 3rd stage boot
313 IoFlags
= //FILE_OPEN_FOR_BACKUP_INTENT |
314 FILE_NO_COMPRESSION
|
316 (NoBuffering
? FILE_NO_INTERMEDIATE_BUFFERING
: 0);
318 /* Set share and access modes */
319 if ((CmpMiniNTBoot
) && (CmpShareSystemHives
))
321 /* We're on Live CD or otherwise sharing */
322 DesiredAccess
= FILE_READ_DATA
;
323 ShareMode
= FILE_SHARE_READ
;
327 /* We want to write exclusively */
329 DesiredAccess
= FILE_READ_DATA
| FILE_WRITE_DATA
;
332 /* Default attributes */
333 AttributeFlags
= FILE_ATTRIBUTE_NORMAL
;
335 /* Now create the file */
336 Status
= ZwCreateFile(Primary
,
344 FILE_SYNCHRONOUS_IO_NONALERT
| IoFlags
,
347 /* Check if anything failed until now */
348 if (!NT_SUCCESS(Status
))
350 /* Close handles and free buffers */
351 if (NameBuffer
) ExFreePool(NameBuffer
);
352 ObDereferenceObject(Event
);
353 ZwClose(EventHandle
);
354 DPRINT1("ZwCreateFile failed : %lx.\n", Status
);
359 if (MarkAsSystemHive
)
361 /* We opened it, mark it as a system hive */
362 Status
= ZwFsControlFile(*Primary
,
367 FSCTL_MARK_AS_SYSTEM_HIVE
,
372 if (Status
== STATUS_PENDING
)
374 /* Wait for completion */
375 KeWaitForSingleObject(Event
,
380 Status
= IoStatusBlock
.Status
;
383 /* If we don't support it, ignore the failure */
384 if (Status
== STATUS_INVALID_DEVICE_REQUEST
) Status
= STATUS_SUCCESS
;
386 if (!NT_SUCCESS(Status
))
388 /* Close handles and free buffers */
389 if (NameBuffer
) ExFreePool(NameBuffer
);
390 ObDereferenceObject(Event
);
391 ZwClose(EventHandle
);
398 /* Disable compression */
399 CompressionState
= 0;
400 Status
= ZwFsControlFile(*Primary
,
405 FSCTL_SET_COMPRESSION
,
407 sizeof(CompressionState
),
410 if (Status
== STATUS_PENDING
)
412 /* Wait for completion */
413 KeWaitForSingleObject(Event
,
420 /* Get the disposition */
421 *PrimaryDisposition
= (ULONG
)IoStatusBlock
.Information
;
422 if (IoStatusBlock
.Information
!= FILE_CREATED
)
424 /* Check how large the file is */
425 Status
= ZwQueryInformationFile(*Primary
,
428 sizeof(FileInformation
),
429 FileStandardInformation
);
430 if (NT_SUCCESS(Status
))
432 /* Check if it's 0 bytes */
433 if (!FileInformation
.EndOfFile
.QuadPart
)
435 /* Assume it's a new file */
436 *PrimaryDisposition
= FILE_CREATED
;
441 /* Check if the caller wants cluster size returned */
445 Status
= ZwQueryVolumeInformationFile(*Primary
,
448 sizeof(FsSizeInformation
),
449 FileFsSizeInformation
);
450 if (!NT_SUCCESS(Status
))
452 /* Close handles and free buffers */
453 if (NameBuffer
) ExFreePool(NameBuffer
);
454 ObDereferenceObject(Event
);
455 ZwClose(EventHandle
);
459 /* Check if the sector size is invalid */
460 if (FsSizeInformation
.BytesPerSector
> HBLOCK_SIZE
)
462 /* Close handles and free buffers */
463 if (NameBuffer
) ExFreePool(NameBuffer
);
464 ObDereferenceObject(Event
);
465 ZwClose(EventHandle
);
466 return STATUS_CANNOT_LOAD_REGISTRY_FILE
;
469 /* Return cluster size */
470 *ClusterSize
= max(1, FsSizeInformation
.BytesPerSector
/ HSECTOR_SIZE
);
473 /* Check if we don't need to create a log file */
476 /* We're done, close handles */
477 ObDereferenceObject(Event
);
478 ZwClose(EventHandle
);
479 return STATUS_SUCCESS
;
482 /* Check if we can create the hive */
483 CreateDisposition
= CmpShareSystemHives
? FILE_OPEN
: FILE_OPEN_IF
;
484 if (*PrimaryDisposition
== FILE_CREATED
)
486 /* Over-write the existing log file, since this is a new hive */
487 CreateDisposition
= FILE_SUPERSEDE
;
491 RtlInitUnicodeString(&ExtensionName
, Extension
);
492 RtlAppendUnicodeStringToString(&FullName
, &ExtensionName
);
494 /* Initialize the attributes */
495 InitializeObjectAttributes(&ObjectAttributes
,
497 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
501 /* Setup the flags */
502 IoFlags
= FILE_NO_COMPRESSION
| FILE_NO_INTERMEDIATE_BUFFERING
;
504 /* Check if this is a log file */
505 if (!_wcsnicmp(Extension
, L
".log", 4))
508 AttributeFlags
|= FILE_ATTRIBUTE_HIDDEN
;
511 /* Now create the file */
512 Status
= ZwCreateFile(Log
,
523 if ((NT_SUCCESS(Status
)) && (MarkAsSystemHive
))
525 /* We opened it, mark it as a system hive */
526 Status
= ZwFsControlFile(*Log
,
531 FSCTL_MARK_AS_SYSTEM_HIVE
,
536 if (Status
== STATUS_PENDING
)
538 /* Wait for completion */
539 KeWaitForSingleObject(Event
,
544 Status
= IoStatusBlock
.Status
;
547 /* If we don't support it, ignore the failure */
548 if (Status
== STATUS_INVALID_DEVICE_REQUEST
) Status
= STATUS_SUCCESS
;
550 /* If we failed, close the handle */
551 if (!NT_SUCCESS(Status
)) ZwClose(*Log
);
554 /* Check if anything failed until now */
555 if (!NT_SUCCESS(Status
))
557 /* Clear the handle */
562 /* Disable compression */
563 Status
= ZwFsControlFile(*Log
,
568 FSCTL_SET_COMPRESSION
,
570 sizeof(CompressionState
),
573 if (Status
== STATUS_PENDING
)
575 /* Wait for completion */
576 KeWaitForSingleObject(Event
,
583 /* Return the disposition */
584 *LogDisposition
= (ULONG
)IoStatusBlock
.Information
;
587 /* We're done, close handles and free buffers */
588 if (NameBuffer
) ExFreePool(NameBuffer
);
589 ObDereferenceObject(Event
);
590 ZwClose(EventHandle
);
591 return STATUS_SUCCESS
;