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 if (CmCheckRegistry((PCMHIVE
)Hive
, TRUE
))
219 /* Cleanup allocations and fail */
220 ExFreePoolWithTag(Hive
->FlusherLock
, TAG_CM
);
221 ExFreePoolWithTag(Hive
->ViewLock
, TAG_CM
);
222 ExFreePoolWithTag(Hive
, TAG_CM
);
223 return STATUS_REGISTRY_CORRUPT
;
227 /* Lock the hive list */
228 ExAcquirePushLockExclusive(&CmpHiveListHeadLock
);
230 /* Insert this hive */
231 InsertHeadList(&CmpHiveListHead
, &Hive
->HiveList
);
233 /* Release the lock */
234 ExReleasePushLock(&CmpHiveListHeadLock
);
236 /* Return the hive and success */
237 *RegistryHive
= (PCMHIVE
)Hive
;
238 return STATUS_SUCCESS
;
243 CmpOpenHiveFiles(IN PCUNICODE_STRING BaseName
,
244 IN PCWSTR Extension OPTIONAL
,
247 OUT PULONG PrimaryDisposition
,
248 OUT PULONG LogDisposition
,
249 IN BOOLEAN CreateAllowed
,
250 IN BOOLEAN MarkAsSystemHive
,
251 IN BOOLEAN NoBuffering
,
252 OUT PULONG ClusterSize OPTIONAL
)
257 UNICODE_STRING FullName
, ExtensionName
;
260 OBJECT_ATTRIBUTES ObjectAttributes
;
261 IO_STATUS_BLOCK IoStatusBlock
;
262 ULONG AttributeFlags
, ShareMode
, DesiredAccess
, CreateDisposition
, IoFlags
;
263 USHORT CompressionState
;
264 FILE_STANDARD_INFORMATION FileInformation
;
265 FILE_FS_SIZE_INFORMATION FsSizeInformation
;
268 Status
= CmpCreateEvent(NotificationEvent
, &EventHandle
, &Event
);
269 if (!NT_SUCCESS(Status
)) return Status
;
271 /* Initialize the full name */
272 RtlInitEmptyUnicodeString(&FullName
, NULL
, 0);
273 Length
= BaseName
->Length
;
275 /* Check if we have an extension */
278 /* Update the name length */
279 Length
+= (USHORT
)wcslen(Extension
) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
281 /* Allocate the buffer for the full name */
282 NameBuffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
286 ObDereferenceObject(Event
);
287 ZwClose(EventHandle
);
288 return STATUS_NO_MEMORY
;
291 /* Build the full name */
292 FullName
.Buffer
= NameBuffer
;
293 FullName
.MaximumLength
= Length
;
294 RtlAppendUnicodeStringToString(&FullName
, BaseName
);
298 /* The base name is the full name */
299 FullName
= *BaseName
;
303 /* Initialize the attributes */
304 InitializeObjectAttributes(&ObjectAttributes
,
306 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
310 /* Check if we can create the hive */
311 if ((CreateAllowed
) && !(CmpShareSystemHives
))
313 /* Open only or create */
314 CreateDisposition
= FILE_OPEN_IF
;
319 CreateDisposition
= FILE_OPEN
;
322 /* Setup the flags */
323 // FIXME : FILE_OPEN_FOR_BACKUP_INTENT is unimplemented and breaks 3rd stage boot
324 IoFlags
= //FILE_OPEN_FOR_BACKUP_INTENT |
325 FILE_NO_COMPRESSION
|
327 (NoBuffering
? FILE_NO_INTERMEDIATE_BUFFERING
: 0);
329 /* Set share and access modes */
330 if ((CmpMiniNTBoot
) && (CmpShareSystemHives
))
332 /* We're on Live CD or otherwise sharing */
333 DesiredAccess
= FILE_READ_DATA
;
334 ShareMode
= FILE_SHARE_READ
;
338 /* We want to write exclusively */
340 DesiredAccess
= FILE_READ_DATA
| FILE_WRITE_DATA
;
343 /* Default attributes */
344 AttributeFlags
= FILE_ATTRIBUTE_NORMAL
;
346 /* Now create the file */
347 Status
= ZwCreateFile(Primary
,
348 DesiredAccess
| SYNCHRONIZE
,
355 FILE_SYNCHRONOUS_IO_NONALERT
| IoFlags
,
358 /* Check if anything failed until now */
359 if (!NT_SUCCESS(Status
))
361 /* Close handles and free buffers */
362 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
363 ObDereferenceObject(Event
);
364 ZwClose(EventHandle
);
365 DPRINT1("ZwCreateFile failed : %lx.\n", Status
);
370 if (MarkAsSystemHive
)
372 /* We opened it, mark it as a system hive */
373 Status
= ZwFsControlFile(*Primary
,
378 FSCTL_MARK_AS_SYSTEM_HIVE
,
383 if (Status
== STATUS_PENDING
)
385 /* Wait for completion */
386 KeWaitForSingleObject(Event
,
391 Status
= IoStatusBlock
.Status
;
394 /* If we don't support it, ignore the failure */
395 if (Status
== STATUS_INVALID_DEVICE_REQUEST
) Status
= STATUS_SUCCESS
;
397 if (!NT_SUCCESS(Status
))
399 /* Close handles and free buffers */
400 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
401 ObDereferenceObject(Event
);
402 ZwClose(EventHandle
);
409 /* Disable compression */
410 CompressionState
= 0;
411 Status
= ZwFsControlFile(*Primary
,
416 FSCTL_SET_COMPRESSION
,
418 sizeof(CompressionState
),
421 if (Status
== STATUS_PENDING
)
423 /* Wait for completion */
424 KeWaitForSingleObject(Event
,
431 /* Get the disposition */
432 *PrimaryDisposition
= (ULONG
)IoStatusBlock
.Information
;
433 if (IoStatusBlock
.Information
!= FILE_CREATED
)
435 /* Check how large the file is */
436 Status
= ZwQueryInformationFile(*Primary
,
439 sizeof(FileInformation
),
440 FileStandardInformation
);
441 if (NT_SUCCESS(Status
))
443 /* Check if it's 0 bytes */
444 if (!FileInformation
.EndOfFile
.QuadPart
)
446 /* Assume it's a new file */
447 *PrimaryDisposition
= FILE_CREATED
;
452 /* Check if the caller wants cluster size returned */
456 Status
= ZwQueryVolumeInformationFile(*Primary
,
459 sizeof(FsSizeInformation
),
460 FileFsSizeInformation
);
461 if (!NT_SUCCESS(Status
))
463 /* Close handles and free buffers */
464 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
465 ObDereferenceObject(Event
);
466 ZwClose(EventHandle
);
470 /* Check if the sector size is invalid */
471 if (FsSizeInformation
.BytesPerSector
> HBLOCK_SIZE
)
473 /* Close handles and free buffers */
474 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
475 ObDereferenceObject(Event
);
476 ZwClose(EventHandle
);
477 return STATUS_CANNOT_LOAD_REGISTRY_FILE
;
480 /* Return cluster size */
481 *ClusterSize
= max(1, FsSizeInformation
.BytesPerSector
/ HSECTOR_SIZE
);
484 /* Check if we don't need to create a log file */
487 /* We're done, close handles */
488 ObDereferenceObject(Event
);
489 ZwClose(EventHandle
);
490 return STATUS_SUCCESS
;
493 /* Check if we can create the hive */
494 CreateDisposition
= CmpShareSystemHives
? FILE_OPEN
: FILE_OPEN_IF
;
495 if (*PrimaryDisposition
== FILE_CREATED
)
497 /* Over-write the existing log file, since this is a new hive */
498 CreateDisposition
= FILE_SUPERSEDE
;
502 RtlInitUnicodeString(&ExtensionName
, Extension
);
503 RtlAppendUnicodeStringToString(&FullName
, &ExtensionName
);
505 /* Initialize the attributes */
506 InitializeObjectAttributes(&ObjectAttributes
,
508 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
512 /* Setup the flags */
513 IoFlags
= FILE_NO_COMPRESSION
| FILE_NO_INTERMEDIATE_BUFFERING
;
515 /* Check if this is a log file */
516 if (!_wcsnicmp(Extension
, L
".log", 4))
519 AttributeFlags
|= FILE_ATTRIBUTE_HIDDEN
;
522 /* Now create the file */
523 Status
= ZwCreateFile(Log
,
534 if ((NT_SUCCESS(Status
)) && (MarkAsSystemHive
))
536 /* We opened it, mark it as a system hive */
537 Status
= ZwFsControlFile(*Log
,
542 FSCTL_MARK_AS_SYSTEM_HIVE
,
547 if (Status
== STATUS_PENDING
)
549 /* Wait for completion */
550 KeWaitForSingleObject(Event
,
555 Status
= IoStatusBlock
.Status
;
558 /* If we don't support it, ignore the failure */
559 if (Status
== STATUS_INVALID_DEVICE_REQUEST
) Status
= STATUS_SUCCESS
;
561 /* If we failed, close the handle */
562 if (!NT_SUCCESS(Status
)) ZwClose(*Log
);
565 /* Check if anything failed until now */
566 if (!NT_SUCCESS(Status
))
568 /* Clear the handle */
573 /* Disable compression */
574 Status
= ZwFsControlFile(*Log
,
579 FSCTL_SET_COMPRESSION
,
581 sizeof(CompressionState
),
584 if (Status
== STATUS_PENDING
)
586 /* Wait for completion */
587 KeWaitForSingleObject(Event
,
594 /* Return the disposition */
595 *LogDisposition
= (ULONG
)IoStatusBlock
.Information
;
598 /* We're done, close handles and free buffers */
599 if (NameBuffer
) ExFreePoolWithTag(NameBuffer
, TAG_CM
);
600 ObDereferenceObject(Event
);
601 ZwClose(EventHandle
);
602 return STATUS_SUCCESS
;