2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/config/cmkcbncb.c
5 * PURPOSE: Routines for handling KCBs, NCBs, as well as key hashes.
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
15 /* GLOBALS *******************************************************************/
17 ULONG CmpHashTableSize
= 2048;
18 PCM_KEY_HASH_TABLE_ENTRY CmpCacheTable
;
19 PCM_NAME_HASH_TABLE_ENTRY CmpNameCacheTable
;
21 BOOLEAN CmpHoldLazyFlush
;
23 /* FUNCTIONS *****************************************************************/
27 CmpInitializeCache(VOID
)
31 /* Calculate length for the table */
32 Length
= CmpHashTableSize
* sizeof(CM_KEY_HASH_TABLE_ENTRY
);
35 CmpCacheTable
= CmpAllocate(Length
, TRUE
, TAG_CM
);
38 /* Take the system down */
39 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 3, 1, 0, 0);
42 /* Zero out the table */
43 RtlZeroMemory(CmpCacheTable
, Length
);
45 /* Initialize the locks */
46 for (i
= 0;i
< CmpHashTableSize
; i
++)
48 /* Setup the pushlock */
49 ExInitializePushLock((PULONG_PTR
)&CmpCacheTable
[i
].Lock
);
52 /* Calculate length for the name cache */
53 Length
= CmpHashTableSize
* sizeof(CM_NAME_HASH_TABLE_ENTRY
);
55 /* Now allocate the name cache table */
56 CmpNameCacheTable
= CmpAllocate(Length
, TRUE
, TAG_CM
);
57 if (!CmpNameCacheTable
)
59 /* Take the system down */
60 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 3, 3, 0, 0);
63 /* Zero out the table */
64 RtlZeroMemory(CmpNameCacheTable
, Length
);
66 /* Initialize the locks */
67 for (i
= 0;i
< CmpHashTableSize
; i
++)
69 /* Setup the pushlock */
70 ExInitializePushLock((PULONG_PTR
)&CmpNameCacheTable
[i
].Lock
);
73 /* Setup the delayed close table */
74 CmpInitializeDelayedCloseTable();
79 CmpRemoveKeyHash(IN PCM_KEY_HASH KeyHash
)
83 ASSERT_VALID_HASH(KeyHash
);
85 /* Lookup all the keys in this index entry */
86 Prev
= &GET_HASH_ENTRY(CmpCacheTable
, KeyHash
->ConvKey
).Entry
;
89 /* Save the current one and make sure it's valid */
91 ASSERT(Current
!= NULL
);
92 ASSERT_VALID_HASH(Current
);
94 /* Check if it matches */
95 if (Current
== KeyHash
)
97 /* Then write the previous one */
98 *Prev
= Current
->NextHash
;
99 if (*Prev
) ASSERT_VALID_HASH(*Prev
);
103 /* Otherwise, keep going */
104 Prev
= &Current
->NextHash
;
108 PCM_KEY_CONTROL_BLOCK
110 CmpInsertKeyHash(IN PCM_KEY_HASH KeyHash
,
115 ASSERT_VALID_HASH(KeyHash
);
117 /* Get the hash index */
118 i
= GET_HASH_INDEX(KeyHash
->ConvKey
);
120 /* If this is a fake key, increase the key cell to use the parent data */
121 if (IsFake
) KeyHash
->KeyCell
++;
123 /* Loop the hash table */
124 Entry
= CmpCacheTable
[i
].Entry
;
127 /* Check if this matches */
128 ASSERT_VALID_HASH(Entry
);
129 if ((KeyHash
->ConvKey
== Entry
->ConvKey
) &&
130 (KeyHash
->KeyCell
== Entry
->KeyCell
) &&
131 (KeyHash
->KeyHive
== Entry
->KeyHive
))
134 return CONTAINING_RECORD(Entry
, CM_KEY_CONTROL_BLOCK
, KeyHash
);
138 Entry
= Entry
->NextHash
;
141 /* No entry found, add this one and return NULL since none existed */
142 KeyHash
->NextHash
= CmpCacheTable
[i
].Entry
;
143 CmpCacheTable
[i
].Entry
= KeyHash
;
147 PCM_NAME_CONTROL_BLOCK
149 CmpGetNameControlBlock(IN PUNICODE_STRING NodeName
)
151 PCM_NAME_CONTROL_BLOCK Ncb
= NULL
;
155 BOOLEAN IsCompressed
= TRUE
, Found
= FALSE
;
156 PCM_NAME_HASH HashEntry
;
157 ULONG Length
, NcbSize
;
160 p
= NodeName
->Buffer
;
161 for (i
= 0; i
< NodeName
->Length
; i
+= sizeof(WCHAR
))
163 /* Make sure it's not a slash */
164 if (*p
!= OBJ_NAME_PATH_SEPARATOR
)
166 /* Add it to the hash */
167 ConvKey
= 37 * ConvKey
+ RtlUpcaseUnicodeChar(*p
);
174 /* Set assumed lengh and loop to check */
175 Length
= NodeName
->Length
/ sizeof(WCHAR
);
176 for (i
= 0; i
< (NodeName
->Length
/ sizeof(WCHAR
)); i
++)
178 /* Check if this is a 16-bit character */
179 if (NodeName
->Buffer
[i
] > (UCHAR
)-1)
181 /* This is the actual size, and we know we're not compressed */
182 Length
= NodeName
->Length
;
183 IsCompressed
= FALSE
;
188 /* Lock the NCB entry */
189 CmpAcquireNcbLockExclusiveByKey(ConvKey
);
191 /* Get the hash entry */
192 HashEntry
= GET_HASH_ENTRY(CmpNameCacheTable
, ConvKey
).Entry
;
195 /* Get the current NCB */
196 Ncb
= CONTAINING_RECORD(HashEntry
, CM_NAME_CONTROL_BLOCK
, NameHash
);
198 /* Check if the hash matches */
199 if ((ConvKey
== HashEntry
->ConvKey
) && (Length
== Ncb
->NameLength
))
204 /* If the NCB is compressed, do a compressed name compare */
208 if (CmpCompareCompressedName(NodeName
, Ncb
->Name
, Length
))
216 /* Do a manual compare */
217 p
= NodeName
->Buffer
;
219 for (i
= 0; i
< Ncb
->NameLength
; i
+= sizeof(WCHAR
))
221 /* Compare the character */
222 if (RtlUpcaseUnicodeChar(*p
) != RtlUpcaseUnicodeChar(*pp
))
235 /* Check if we found a name */
243 /* Go to the next hash */
244 HashEntry
= HashEntry
->NextHash
;
247 /* Check if we didn't find it */
251 NcbSize
= FIELD_OFFSET(CM_NAME_CONTROL_BLOCK
, Name
) + Length
;
252 Ncb
= CmpAllocate(NcbSize
, TRUE
, TAG_CM
);
255 /* Release the lock and fail */
256 CmpReleaseNcbLockByKey(ConvKey
);
261 RtlZeroMemory(Ncb
, NcbSize
);
263 /* Check if the name was compressed */
266 /* Copy the compressed name */
267 for (i
= 0; i
< NodeName
->Length
/ sizeof(WCHAR
); i
++)
269 /* Copy Unicode to ANSI */
270 ((PCHAR
)Ncb
->Name
)[i
] = (CHAR
)RtlUpcaseUnicodeChar(NodeName
->Buffer
[i
]);
275 /* Copy the name directly */
276 for (i
= 0; i
< NodeName
->Length
/ sizeof(WCHAR
); i
++)
278 /* Copy each unicode character */
279 Ncb
->Name
[i
] = RtlUpcaseUnicodeChar(NodeName
->Buffer
[i
]);
283 /* Setup the rest of the NCB */
284 Ncb
->Compressed
= IsCompressed
;
285 Ncb
->ConvKey
= ConvKey
;
287 Ncb
->NameLength
= Length
;
289 /* Insert the name in the hash table */
290 HashEntry
= &Ncb
->NameHash
;
291 HashEntry
->NextHash
= GET_HASH_ENTRY(CmpNameCacheTable
, ConvKey
).Entry
;
292 GET_HASH_ENTRY(CmpNameCacheTable
, ConvKey
).Entry
= HashEntry
;
295 /* Release NCB lock */
296 CmpReleaseNcbLockByKey(ConvKey
);
298 /* Return the NCB found */
304 CmpRemoveKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
306 /* Make sure that the registry and KCB are utterly locked */
307 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
308 (CmpTestRegistryLockExclusive() == TRUE
));
310 /* Remove the key hash */
311 CmpRemoveKeyHash(&Kcb
->KeyHash
);
316 CmpDereferenceNameControlBlockWithLock(IN PCM_NAME_CONTROL_BLOCK Ncb
)
318 PCM_NAME_HASH Current
, *Next
;
319 ULONG ConvKey
= Ncb
->ConvKey
;
322 CmpAcquireNcbLockExclusiveByKey(ConvKey
);
324 /* Decrease the reference count */
325 if (!(--Ncb
->RefCount
))
327 /* Find the NCB in the table */
328 Next
= &GET_HASH_ENTRY(CmpNameCacheTable
, Ncb
->ConvKey
).Entry
;
331 /* Check the current entry */
333 ASSERT(Current
!= NULL
);
334 if (Current
== &Ncb
->NameHash
)
337 *Next
= Current
->NextHash
;
341 /* Get to the next one */
342 Next
= &Current
->NextHash
;
345 /* Found it, now free it */
349 /* Release the lock */
350 CmpReleaseNcbLockByKey(ConvKey
);
355 CmpReferenceKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
357 CMTRACE(CM_REFERENCE_DEBUG
,
358 "%s - Referencing KCB: %p\n", __FUNCTION__
, Kcb
);
360 /* Check if this is the KCB's first reference */
361 if (Kcb
->RefCount
== 0)
363 /* Check if the KCB is locked in shared mode */
364 if (!CmpIsKcbLockedExclusive(Kcb
))
366 /* Convert it to exclusive */
367 if (!CmpTryToConvertKcbSharedToExclusive(Kcb
))
369 /* Set the delayed close index so that we can be ignored */
370 Kcb
->DelayedCloseIndex
= 1;
372 /* Increase the reference count while we release the lock */
373 InterlockedIncrement((PLONG
)&Kcb
->RefCount
);
375 /* Go from shared to exclusive */
376 CmpConvertKcbSharedToExclusive(Kcb
);
378 /* Decrement the reference count; the lock is now held again */
379 InterlockedDecrement((PLONG
)&Kcb
->RefCount
);
381 /* Check if we still control the index */
382 if (Kcb
->DelayedCloseIndex
== 1)
385 Kcb
->DelayedCloseIndex
= 0;
390 ASSERT((Kcb
->DelayedCloseIndex
== CmpDelayedCloseSize
) ||
391 (Kcb
->DelayedCloseIndex
== 0));
397 /* Increase the reference count */
398 if ((InterlockedIncrement((PLONG
)&Kcb
->RefCount
) & 0xFFFF) == 0)
400 /* We've overflown to 64K references, bail out */
401 InterlockedDecrement((PLONG
)&Kcb
->RefCount
);
405 /* Check if this was the last close index */
406 if (!Kcb
->DelayedCloseIndex
)
408 /* Check if the KCB is locked in shared mode */
409 if (!CmpIsKcbLockedExclusive(Kcb
))
411 /* Convert it to exclusive */
412 if (!CmpTryToConvertKcbSharedToExclusive(Kcb
))
414 /* Go from shared to exclusive */
415 CmpConvertKcbSharedToExclusive(Kcb
);
419 /* If we're still the last entry, remove us */
420 if (!Kcb
->DelayedCloseIndex
) CmpRemoveFromDelayedClose(Kcb
);
429 CmpCleanUpKcbValueCache(IN PCM_KEY_CONTROL_BLOCK Kcb
)
431 PULONG_PTR CachedList
;
435 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
436 (CmpTestRegistryLockExclusive() == TRUE
));
438 /* Check if the value list is cached */
439 if (CMP_IS_CELL_CACHED(Kcb
->ValueCache
.ValueList
))
441 /* Get the cache list */
442 CachedList
= (PULONG_PTR
)CMP_GET_CACHED_DATA(Kcb
->ValueCache
.ValueList
);
443 for (i
= 0; i
< Kcb
->ValueCache
.Count
; i
++)
445 /* Check if this cell is cached */
446 if (CMP_IS_CELL_CACHED(CachedList
[i
]))
449 CmpFree((PVOID
)CMP_GET_CACHED_CELL(CachedList
[i
]), 0);
453 /* Now free the list */
454 CmpFree((PVOID
)CMP_GET_CACHED_CELL(Kcb
->ValueCache
.ValueList
), 0);
455 Kcb
->ValueCache
.ValueList
= HCELL_NIL
;
457 else if (Kcb
->ExtFlags
& CM_KCB_SYM_LINK_FOUND
)
459 /* This is a sym link, check if there's only one reference left */
460 if ((Kcb
->ValueCache
.RealKcb
->RefCount
== 1) &&
461 !(Kcb
->ValueCache
.RealKcb
->Delete
))
463 /* Disable delay close for the KCB */
464 Kcb
->ValueCache
.RealKcb
->ExtFlags
|= CM_KCB_NO_DELAY_CLOSE
;
467 /* Dereference the KCB */
468 CmpDelayDerefKeyControlBlock(Kcb
->ValueCache
.RealKcb
);
469 Kcb
->ExtFlags
&= ~CM_KCB_SYM_LINK_FOUND
;
475 CmpCleanUpKcbCacheWithLock(IN PCM_KEY_CONTROL_BLOCK Kcb
,
476 IN BOOLEAN LockHeldExclusively
)
478 PCM_KEY_CONTROL_BLOCK Parent
;
482 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
483 (CmpTestRegistryLockExclusive() == TRUE
));
484 ASSERT(Kcb
->RefCount
== 0);
486 /* Cleanup the value cache */
487 CmpCleanUpKcbValueCache(Kcb
);
489 /* Dereference the NCB */
490 CmpDereferenceNameControlBlockWithLock(Kcb
->NameBlock
);
492 /* Check if we have an index hint block and free it */
493 if (Kcb
->ExtFlags
& CM_KCB_SUBKEY_HINT
) CmpFree(Kcb
->IndexHint
, 0);
495 /* Check if we were already deleted */
496 Parent
= Kcb
->ParentKcb
;
497 if (!Kcb
->Delete
) CmpRemoveKeyControlBlock(Kcb
);
499 /* Set invalid KCB signature */
500 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
502 /* Free the KCB as well */
503 CmpFreeKeyControlBlock(Kcb
);
505 /* Check if we have a parent */
508 /* Dereference the parent */
509 LockHeldExclusively
?
510 CmpDereferenceKeyControlBlockWithLock(Parent
,LockHeldExclusively
) :
511 CmpDelayDerefKeyControlBlock(Parent
);
517 CmpCleanUpSubKeyInfo(IN PCM_KEY_CONTROL_BLOCK Kcb
)
519 PCM_KEY_NODE KeyNode
;
522 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
523 (CmpTestRegistryLockExclusive() == TRUE
));
525 /* Check if there's any cached subkey */
526 if (Kcb
->ExtFlags
& (CM_KCB_NO_SUBKEY
| CM_KCB_SUBKEY_ONE
| CM_KCB_SUBKEY_HINT
))
528 /* Check if there's a hint */
529 if (Kcb
->ExtFlags
& (CM_KCB_SUBKEY_HINT
))
532 CmpFree(Kcb
->IndexHint
, 0);
535 /* Remove subkey flags */
536 Kcb
->ExtFlags
&= ~(CM_KCB_NO_SUBKEY
| CM_KCB_SUBKEY_ONE
| CM_KCB_SUBKEY_HINT
);
539 /* Check if there's no linked cell */
540 if (Kcb
->KeyCell
== HCELL_NIL
)
542 /* Make sure it's a delete */
548 /* Get the key node */
549 KeyNode
= (PCM_KEY_NODE
)HvGetCell(Kcb
->KeyHive
, Kcb
->KeyCell
);
552 /* Check if we got the node */
555 /* We didn't, mark the cached data invalid */
556 Kcb
->ExtFlags
|= CM_KCB_INVALID_CACHED_INFO
;
560 /* We have a keynode, update subkey counts */
561 Kcb
->ExtFlags
&= ~CM_KCB_INVALID_CACHED_INFO
;
562 Kcb
->SubKeyCount
= KeyNode
->SubKeyCounts
[Stable
] +
563 KeyNode
->SubKeyCounts
[Volatile
];
565 /* Release the cell */
566 HvReleaseCell(Kcb
->KeyHive
, Kcb
->KeyCell
);
572 CmpDereferenceKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
574 LONG OldRefCount
, NewRefCount
;
576 CMTRACE(CM_REFERENCE_DEBUG
,
577 "%s - Dereferencing KCB: %p\n", __FUNCTION__
, Kcb
);
579 /* Get the ref count and update it */
580 OldRefCount
= *(PLONG
)&Kcb
->RefCount
;
581 NewRefCount
= OldRefCount
- 1;
583 /* Check if we still have references */
584 if( (NewRefCount
& 0xFFFF) > 0)
586 /* Do the dereference */
587 if (InterlockedCompareExchange((PLONG
)&Kcb
->RefCount
,
589 OldRefCount
) == OldRefCount
)
597 ConvKey
= Kcb
->ConvKey
;
599 /* Do the dereference inside the lock */
600 CmpAcquireKcbLockExclusive(Kcb
);
601 CmpDereferenceKeyControlBlockWithLock(Kcb
, FALSE
);
602 CmpReleaseKcbLockByKey(ConvKey
);
607 CmpDereferenceKeyControlBlockWithLock(IN PCM_KEY_CONTROL_BLOCK Kcb
,
608 IN BOOLEAN LockHeldExclusively
)
610 CMTRACE(CM_REFERENCE_DEBUG
,
611 "%s - Dereferencing KCB: %p\n", __FUNCTION__
, Kcb
);
614 ASSERT_KCB_VALID(Kcb
);
616 /* Check if this is the last reference */
617 if ((InterlockedDecrement((PLONG
)&Kcb
->RefCount
) & 0xFFFF) == 0)
620 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
621 (CmpTestRegistryLockExclusive() == TRUE
));
623 /* Check if we should do a direct delete */
624 if (((CmpHoldLazyFlush
) &&
625 !(Kcb
->ExtFlags
& CM_KCB_SYM_LINK_FOUND
) &&
626 !(Kcb
->Flags
& KEY_SYM_LINK
)) ||
627 (Kcb
->ExtFlags
& CM_KCB_NO_DELAY_CLOSE
) ||
630 /* Clean up the KCB*/
631 CmpCleanUpKcbCacheWithLock(Kcb
, LockHeldExclusively
);
635 /* Otherwise, use delayed close */
636 CmpAddToDelayedClose(Kcb
, LockHeldExclusively
);
643 InitializeKCBKeyBodyList(IN PCM_KEY_CONTROL_BLOCK Kcb
)
645 /* Initialize the list */
646 InitializeListHead(&Kcb
->KeyBodyListHead
);
648 /* Clear the bodies */
649 Kcb
->KeyBodyArray
[0] =
650 Kcb
->KeyBodyArray
[1] =
651 Kcb
->KeyBodyArray
[2] =
652 Kcb
->KeyBodyArray
[3] = NULL
;
655 PCM_KEY_CONTROL_BLOCK
657 CmpCreateKeyControlBlock(IN PHHIVE Hive
,
658 IN HCELL_INDEX Index
,
659 IN PCM_KEY_NODE Node
,
660 IN PCM_KEY_CONTROL_BLOCK Parent
,
662 IN PUNICODE_STRING KeyName
)
664 PCM_KEY_CONTROL_BLOCK Kcb
, FoundKcb
= NULL
;
665 UNICODE_STRING NodeName
;
666 ULONG ConvKey
= 0, i
;
667 BOOLEAN IsFake
, HashLock
;
670 /* Make sure we own this hive in case it's being unloaded */
671 if ((Hive
->HiveFlags
& HIVE_IS_UNLOADING
) &&
672 (((PCMHIVE
)Hive
)->CreatorOwner
!= KeGetCurrentThread()))
678 /* Check if this is a fake KCB */
679 IsFake
= Flags
& CMP_CREATE_FAKE_KCB
? TRUE
: FALSE
;
681 /* If we have a parent, use its ConvKey */
682 if (Parent
) ConvKey
= Parent
->ConvKey
;
684 /* Make a copy of the name */
687 /* Remove leading slash */
688 while ((NodeName
.Length
) && (*NodeName
.Buffer
== OBJ_NAME_PATH_SEPARATOR
))
690 /* Move the buffer by one */
692 NodeName
.Length
-= sizeof(WCHAR
);
695 /* Make sure we didn't get just a slash or something */
696 ASSERT(NodeName
.Length
> 0);
698 /* Now setup the hash */
700 for (i
= 0; i
< NodeName
.Length
; i
+= sizeof(WCHAR
))
702 /* Make sure it's a valid character */
703 if (*p
!= OBJ_NAME_PATH_SEPARATOR
)
705 /* Add this key to the hash */
706 ConvKey
= 37 * ConvKey
+ RtlUpcaseUnicodeChar(*p
);
713 /* Allocate the KCB */
714 Kcb
= CmpAllocateKeyControlBlock();
715 if (!Kcb
) return NULL
;
717 /* Initailize the key list */
718 InitializeKCBKeyBodyList(Kcb
);
721 Kcb
->Signature
= CM_KCB_SIGNATURE
;
725 Kcb
->KeyCell
= Index
;
726 Kcb
->ConvKey
= ConvKey
;
727 Kcb
->DelayedCloseIndex
= CmpDelayedCloseSize
;
728 Kcb
->InDelayClose
= 0;
729 ASSERT_KCB_VALID(Kcb
);
731 /* Check if we have two hash entires */
732 HashLock
= Flags
& CMP_LOCK_HASHES_FOR_KCB
? TRUE
: FALSE
;
735 /* It's not locked, do we have a parent? */
738 /* Lock the parent KCB and ourselves */
739 CmpAcquireTwoKcbLocksExclusiveByKey(ConvKey
, Parent
->ConvKey
);
743 /* Lock only ourselves */
744 CmpAcquireKcbLockExclusive(Kcb
);
748 /* Check if we already have a KCB */
749 FoundKcb
= CmpInsertKeyHash(&Kcb
->KeyHash
, IsFake
);
753 ASSERT(!FoundKcb
->Delete
);
754 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
756 /* Free the one we allocated and reference this one */
757 CmpFreeKeyControlBlock(Kcb
);
758 ASSERT_KCB_VALID(FoundKcb
);
760 if (!CmpReferenceKeyControlBlock(Kcb
))
762 /* We got too many handles */
763 ASSERT(Kcb
->RefCount
+ 1 != 0);
768 /* Check if we're not creating a fake one, but it used to be fake */
769 if ((Kcb
->ExtFlags
& CM_KCB_KEY_NON_EXIST
) && !(IsFake
))
771 /* Set the hive and cell */
773 Kcb
->KeyCell
= Index
;
775 /* This means that our current information is invalid */
776 Kcb
->ExtFlags
= CM_KCB_INVALID_CACHED_INFO
;
779 /* Check if we didn't have any valid data */
780 if (!(Kcb
->ExtFlags
& (CM_KCB_NO_SUBKEY
|
782 CM_KCB_SUBKEY_HINT
)))
784 /* Calculate the index hint */
785 Kcb
->SubKeyCount
= Node
->SubKeyCounts
[Stable
] +
786 Node
->SubKeyCounts
[Volatile
];
788 /* Cached information is now valid */
789 Kcb
->ExtFlags
&= ~CM_KCB_INVALID_CACHED_INFO
;
792 /* Setup the other data */
793 Kcb
->KcbLastWriteTime
= Node
->LastWriteTime
;
794 Kcb
->KcbMaxNameLen
= (USHORT
)Node
->MaxNameLen
;
795 Kcb
->KcbMaxValueNameLen
= (USHORT
)Node
->MaxValueNameLen
;
796 Kcb
->KcbMaxValueDataLen
= Node
->MaxValueDataLen
;
801 /* No KCB, do we have a parent? */
804 /* Reference the parent */
805 if (((Parent
->TotalLevels
+ 1) < 512) &&
806 (CmpReferenceKeyControlBlock(Parent
)))
809 Kcb
->ParentKcb
= Parent
;
810 Kcb
->TotalLevels
= Parent
->TotalLevels
+ 1;
814 /* Remove the KCB and free it */
815 CmpRemoveKeyControlBlock(Kcb
);
816 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
817 CmpFreeKeyControlBlock(Kcb
);
823 /* No parent, this is the root node */
824 Kcb
->ParentKcb
= NULL
;
825 Kcb
->TotalLevels
= 1;
828 /* Check if we have a KCB */
832 Kcb
->NameBlock
= CmpGetNameControlBlock(&NodeName
);
836 Kcb
->ValueCache
.Count
= Node
->ValueList
.Count
;
837 Kcb
->ValueCache
.ValueList
= Node
->ValueList
.List
;
838 Kcb
->Flags
= Node
->Flags
;
840 Kcb
->DelayedCloseIndex
= CmpDelayedCloseSize
;
842 /* Remember if this is a fake key */
843 if (IsFake
) Kcb
->ExtFlags
|= CM_KCB_KEY_NON_EXIST
;
845 /* Setup the other data */
846 Kcb
->SubKeyCount
= Node
->SubKeyCounts
[Stable
] +
847 Node
->SubKeyCounts
[Volatile
];
848 Kcb
->KcbLastWriteTime
= Node
->LastWriteTime
;
849 Kcb
->KcbMaxNameLen
= (USHORT
)Node
->MaxNameLen
;
850 Kcb
->KcbMaxValueNameLen
= (USHORT
)Node
->MaxValueNameLen
;
851 Kcb
->KcbMaxValueDataLen
= (USHORT
)Node
->MaxValueDataLen
;
855 /* Dereference the KCB */
856 CmpDereferenceKeyControlBlockWithLock(Parent
, FALSE
);
858 /* Remove the KCB and free it */
859 CmpRemoveKeyControlBlock(Kcb
);
860 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
861 CmpFreeKeyControlBlock(Kcb
);
867 /* Check if this is a KCB inside a frozen hive */
868 if ((Kcb
) && (((PCMHIVE
)Hive
)->Frozen
) && (!(Kcb
->Flags
& KEY_SYM_LINK
)))
870 /* Don't add these to the delay close */
871 Kcb
->ExtFlags
|= CM_KCB_NO_DELAY_CLOSE
;
875 ASSERT((!Kcb
) || (Kcb
->Delete
== FALSE
));
877 /* Check if we had locked the hashes */
880 /* We locked them manually, do we have a parent? */
883 /* Unlock the parent KCB and ourselves */
884 CmpReleaseTwoKcbLockByKey(ConvKey
, Parent
->ConvKey
);
888 /* Unlock only ourselves */
889 CmpReleaseKcbLockByKey(ConvKey
);
899 CmpConstructName(IN PCM_KEY_CONTROL_BLOCK Kcb
)
901 PUNICODE_STRING KeyName
;
903 PCM_KEY_CONTROL_BLOCK MyKcb
;
904 PCM_KEY_NODE KeyNode
;
905 BOOLEAN DeletedKey
= FALSE
;
906 PWCHAR TargetBuffer
, CurrentNameW
;
909 /* Calculate how much size our key name is going to occupy */
915 /* Add length of the name */
916 if (!MyKcb
->NameBlock
->Compressed
)
918 NameLength
+= MyKcb
->NameBlock
->NameLength
;
922 NameLength
+= CmpCompressedNameSize(MyKcb
->NameBlock
->Name
,
923 MyKcb
->NameBlock
->NameLength
);
926 /* Sum up the separator too */
927 NameLength
+= sizeof(WCHAR
);
929 /* Go to the parent KCB */
930 MyKcb
= MyKcb
->ParentKcb
;
933 /* Allocate the unicode string now */
934 KeyName
= CmpAllocate(NameLength
+ sizeof(UNICODE_STRING
),
938 if (!KeyName
) return NULL
;
941 KeyName
->Buffer
= (PWSTR
)(KeyName
+ 1);
942 KeyName
->Length
= NameLength
;
943 KeyName
->MaximumLength
= NameLength
;
945 /* Loop the keys again, now adding names */
951 /* Sanity checks for deleted and fake keys */
952 if ((!MyKcb
->KeyCell
&& !MyKcb
->Delete
) ||
954 MyKcb
->ExtFlags
& CM_KCB_KEY_NON_EXIST
)
961 /* Try to get the name from the keynode,
962 if the key is not deleted */
963 if (!DeletedKey
&& !MyKcb
->Delete
)
965 KeyNode
= HvGetCell(MyKcb
->KeyHive
, MyKcb
->KeyCell
);
976 /* The key was deleted */
981 /* Get the pointer to the beginning of the current key name */
982 NameLength
+= (MyKcb
->NameBlock
->NameLength
+ 1) * sizeof(WCHAR
);
983 TargetBuffer
= &KeyName
->Buffer
[(KeyName
->Length
- NameLength
) / sizeof(WCHAR
)];
985 /* Add a separator */
986 TargetBuffer
[0] = OBJ_NAME_PATH_SEPARATOR
;
988 /* Add the name, but remember to go from the end to the beginning */
989 if (!MyKcb
->NameBlock
->Compressed
)
991 /* Get the pointer to the name (from the keynode, if possible) */
992 if ((MyKcb
->Flags
& (KEY_HIVE_ENTRY
| KEY_HIVE_EXIT
)) ||
995 CurrentNameW
= MyKcb
->NameBlock
->Name
;
999 CurrentNameW
= KeyNode
->Name
;
1003 for (i
=0; i
< MyKcb
->NameBlock
->NameLength
; i
++)
1005 TargetBuffer
[i
+1] = *CurrentNameW
;
1011 /* Get the pointer to the name (from the keynode, if possible) */
1012 if ((MyKcb
->Flags
& (KEY_HIVE_ENTRY
| KEY_HIVE_EXIT
)) ||
1015 CurrentName
= (PUCHAR
)MyKcb
->NameBlock
->Name
;
1019 CurrentName
= (PUCHAR
)KeyNode
->Name
;
1023 for (i
=0; i
< MyKcb
->NameBlock
->NameLength
; i
++)
1025 TargetBuffer
[i
+1] = (WCHAR
)*CurrentName
;
1030 /* Release the cell, if needed */
1031 if (KeyNode
) HvReleaseCell(MyKcb
->KeyHive
, MyKcb
->KeyCell
);
1033 /* Go to the parent KCB */
1034 MyKcb
= MyKcb
->ParentKcb
;
1037 /* Return resulting buffer (both UNICODE_STRING and
1038 its buffer following it) */
1044 EnlistKeyBodyWithKCB(IN PCM_KEY_BODY KeyBody
,
1050 ASSERT(KeyBody
->KeyControlBlock
!= NULL
);
1052 /* Initialize the list entry */
1053 InitializeListHead(&KeyBody
->KeyBodyList
);
1055 /* Check if we can use the parent KCB array */
1056 for (i
= 0; i
< 4; i
++)
1058 /* Add it into the list */
1059 if (!InterlockedCompareExchangePointer(&KeyBody
->KeyControlBlock
->
1069 /* Array full, check if we need to unlock the KCB */
1070 if (Flags
& CMP_ENLIST_KCB_LOCKED_SHARED
)
1072 /* It's shared, so release the KCB shared lock */
1073 CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1074 ASSERT(!(Flags
& CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
));
1077 /* Check if we need to lock the KCB */
1078 if (!(Flags
& CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
))
1080 /* Acquire the lock */
1081 CmpAcquireKcbLockExclusive(KeyBody
->KeyControlBlock
);
1084 /* Make sure we have the exclusive lock */
1085 ASSERT((CmpIsKcbLockedExclusive(KeyBody
->KeyControlBlock
) == TRUE
) ||
1086 (CmpTestRegistryLockExclusive() == TRUE
));
1089 InsertTailList(&KeyBody
->KeyControlBlock
->KeyBodyListHead
,
1090 &KeyBody
->KeyBodyList
);
1092 /* Check if we did a manual lock */
1093 if (!(Flags
& (CMP_ENLIST_KCB_LOCKED_SHARED
|
1094 CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
)))
1096 /* Release the lock */
1097 CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1103 DelistKeyBodyFromKCB(IN PCM_KEY_BODY KeyBody
,
1104 IN BOOLEAN LockHeld
)
1109 ASSERT(KeyBody
->KeyControlBlock
!= NULL
);
1111 /* Check if we can use the parent KCB array */
1112 for (i
= 0; i
< 4; i
++)
1114 /* Add it into the list */
1115 if (InterlockedCompareExchangePointer(&KeyBody
->KeyControlBlock
->
1118 KeyBody
) == KeyBody
)
1126 ASSERT(IsListEmpty(&KeyBody
->KeyControlBlock
->KeyBodyListHead
) == FALSE
);
1127 ASSERT(IsListEmpty(&KeyBody
->KeyBodyList
) == FALSE
);
1130 if (!LockHeld
) CmpAcquireKcbLockExclusive(KeyBody
->KeyControlBlock
);
1131 ASSERT((CmpIsKcbLockedExclusive(KeyBody
->KeyControlBlock
) == TRUE
) ||
1132 (CmpTestRegistryLockExclusive() == TRUE
));
1134 /* Remove the entry */
1135 RemoveEntryList(&KeyBody
->KeyBodyList
);
1137 /* Unlock it it if we did a manual lock */
1138 if (!LockHeld
) CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);