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 /* FUNCTIONS *****************************************************************/
25 CmpInitializeCache(VOID
)
29 /* Calculate length for the table */
30 Length
= CmpHashTableSize
* sizeof(CM_KEY_HASH_TABLE_ENTRY
);
33 CmpCacheTable
= CmpAllocate(Length
, TRUE
, TAG_CM
);
36 /* Take the system down */
37 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 3, 1, 0, 0);
40 /* Zero out the table */
41 RtlZeroMemory(CmpCacheTable
, Length
);
43 /* Initialize the locks */
44 for (i
= 0;i
< CmpHashTableSize
; i
++)
46 /* Setup the pushlock */
47 ExInitializePushLock((PULONG_PTR
)&CmpCacheTable
[i
].Lock
);
50 /* Calculate length for the name cache */
51 Length
= CmpHashTableSize
* sizeof(CM_NAME_HASH_TABLE_ENTRY
);
53 /* Now allocate the name cache table */
54 CmpNameCacheTable
= CmpAllocate(Length
, TRUE
, TAG_CM
);
55 if (!CmpNameCacheTable
)
57 /* Take the system down */
58 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 3, 3, 0, 0);
61 /* Zero out the table */
62 RtlZeroMemory(CmpNameCacheTable
, Length
);
64 /* Initialize the locks */
65 for (i
= 0;i
< CmpHashTableSize
; i
++)
67 /* Setup the pushlock */
68 ExInitializePushLock((PULONG_PTR
)&CmpNameCacheTable
[i
].Lock
);
71 /* Setup the delayed close table */
72 CmpInitializeDelayedCloseTable();
77 CmpRemoveKeyHash(IN PCM_KEY_HASH KeyHash
)
81 ASSERT_VALID_HASH(KeyHash
);
83 /* Lookup all the keys in this index entry */
84 Prev
= &GET_HASH_ENTRY(CmpCacheTable
, KeyHash
->ConvKey
).Entry
;
87 /* Save the current one and make sure it's valid */
89 ASSERT(Current
!= NULL
);
90 ASSERT_VALID_HASH(Current
);
92 /* Check if it matches */
93 if (Current
== KeyHash
)
95 /* Then write the previous one */
96 *Prev
= Current
->NextHash
;
97 if (*Prev
) ASSERT_VALID_HASH(*Prev
);
101 /* Otherwise, keep going */
102 Prev
= &Current
->NextHash
;
106 PCM_KEY_CONTROL_BLOCK
108 CmpInsertKeyHash(IN PCM_KEY_HASH KeyHash
,
113 ASSERT_VALID_HASH(KeyHash
);
115 /* Get the hash index */
116 i
= GET_HASH_INDEX(KeyHash
->ConvKey
);
118 /* If this is a fake key, increase the key cell to use the parent data */
119 if (IsFake
) KeyHash
->KeyCell
++;
121 /* Loop the hash table */
122 Entry
= CmpCacheTable
[i
].Entry
;
125 /* Check if this matches */
126 ASSERT_VALID_HASH(Entry
);
127 if ((KeyHash
->ConvKey
== Entry
->ConvKey
) &&
128 (KeyHash
->KeyCell
== Entry
->KeyCell
) &&
129 (KeyHash
->KeyHive
== Entry
->KeyHive
))
132 return CONTAINING_RECORD(Entry
, CM_KEY_CONTROL_BLOCK
, KeyHash
);
136 Entry
= Entry
->NextHash
;
139 /* No entry found, add this one and return NULL since none existed */
140 KeyHash
->NextHash
= CmpCacheTable
[i
].Entry
;
141 CmpCacheTable
[i
].Entry
= KeyHash
;
145 PCM_NAME_CONTROL_BLOCK
147 CmpGetNameControlBlock(IN PUNICODE_STRING NodeName
)
149 PCM_NAME_CONTROL_BLOCK Ncb
= NULL
;
153 BOOLEAN IsCompressed
= TRUE
, Found
= FALSE
;
154 PCM_NAME_HASH HashEntry
;
155 ULONG Length
, NcbSize
;
158 p
= NodeName
->Buffer
;
159 for (i
= 0; i
< NodeName
->Length
; i
+= sizeof(WCHAR
))
161 /* Make sure it's not a slash */
162 if (*p
!= OBJ_NAME_PATH_SEPARATOR
)
164 /* Add it to the hash */
165 ConvKey
= 37 * ConvKey
+ RtlUpcaseUnicodeChar(*p
);
172 /* Set assumed lengh and loop to check */
173 Length
= NodeName
->Length
/ sizeof(WCHAR
);
174 for (i
= 0; i
< (NodeName
->Length
/ sizeof(WCHAR
)); i
++)
176 /* Check if this is a 16-bit character */
177 if (NodeName
->Buffer
[i
] > (UCHAR
)-1)
179 /* This is the actual size, and we know we're not compressed */
180 Length
= NodeName
->Length
;
181 IsCompressed
= FALSE
;
186 /* Lock the NCB entry */
187 CmpAcquireNcbLockExclusiveByKey(ConvKey
);
189 /* Get the hash entry */
190 HashEntry
= GET_HASH_ENTRY(CmpNameCacheTable
, ConvKey
).Entry
;
193 /* Get the current NCB */
194 Ncb
= CONTAINING_RECORD(HashEntry
, CM_NAME_CONTROL_BLOCK
, NameHash
);
196 /* Check if the hash matches */
197 if ((ConvKey
== HashEntry
->ConvKey
) && (Length
== Ncb
->NameLength
))
202 /* If the NCB is compressed, do a compressed name compare */
206 if (CmpCompareCompressedName(NodeName
, Ncb
->Name
, Length
))
214 /* Do a manual compare */
215 p
= NodeName
->Buffer
;
217 for (i
= 0; i
< Ncb
->NameLength
; i
+= sizeof(WCHAR
))
219 /* Compare the character */
220 if (RtlUpcaseUnicodeChar(*p
) != RtlUpcaseUnicodeChar(*pp
))
233 /* Check if we found a name */
241 /* Go to the next hash */
242 HashEntry
= HashEntry
->NextHash
;
245 /* Check if we didn't find it */
249 NcbSize
= FIELD_OFFSET(CM_NAME_CONTROL_BLOCK
, Name
) + Length
;
250 Ncb
= CmpAllocate(NcbSize
, TRUE
, TAG_CM
);
253 /* Release the lock and fail */
254 CmpReleaseNcbLockByKey(ConvKey
);
259 RtlZeroMemory(Ncb
, NcbSize
);
261 /* Check if the name was compressed */
264 /* Copy the compressed name */
265 for (i
= 0; i
< NodeName
->Length
/ sizeof(WCHAR
); i
++)
267 /* Copy Unicode to ANSI */
268 ((PCHAR
)Ncb
->Name
)[i
] = (CHAR
)RtlUpcaseUnicodeChar(NodeName
->Buffer
[i
]);
273 /* Copy the name directly */
274 for (i
= 0; i
< NodeName
->Length
/ sizeof(WCHAR
); i
++)
276 /* Copy each unicode character */
277 Ncb
->Name
[i
] = RtlUpcaseUnicodeChar(NodeName
->Buffer
[i
]);
281 /* Setup the rest of the NCB */
282 Ncb
->Compressed
= IsCompressed
;
283 Ncb
->ConvKey
= ConvKey
;
285 Ncb
->NameLength
= Length
;
287 /* Insert the name in the hash table */
288 HashEntry
= &Ncb
->NameHash
;
289 HashEntry
->NextHash
= GET_HASH_ENTRY(CmpNameCacheTable
, ConvKey
).Entry
;
290 GET_HASH_ENTRY(CmpNameCacheTable
, ConvKey
).Entry
= HashEntry
;
293 /* Release NCB lock */
294 CmpReleaseNcbLockByKey(ConvKey
);
296 /* Return the NCB found */
302 CmpRemoveKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
304 /* Make sure that the registry and KCB are utterly locked */
305 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
306 (CmpTestRegistryLockExclusive() == TRUE
));
308 /* Remove the key hash */
309 CmpRemoveKeyHash(&Kcb
->KeyHash
);
314 CmpDereferenceNameControlBlockWithLock(IN PCM_NAME_CONTROL_BLOCK Ncb
)
316 PCM_NAME_HASH Current
, *Next
;
317 ULONG ConvKey
= Ncb
->ConvKey
;
320 CmpAcquireNcbLockExclusiveByKey(ConvKey
);
322 /* Decrease the reference count */
323 if (!(--Ncb
->RefCount
))
325 /* Find the NCB in the table */
326 Next
= &GET_HASH_ENTRY(CmpNameCacheTable
, Ncb
->ConvKey
).Entry
;
329 /* Check the current entry */
331 ASSERT(Current
!= NULL
);
332 if (Current
== &Ncb
->NameHash
)
335 *Next
= Current
->NextHash
;
339 /* Get to the next one */
340 Next
= &Current
->NextHash
;
343 /* Found it, now free it */
347 /* Release the lock */
348 CmpReleaseNcbLockByKey(ConvKey
);
353 CmpReferenceKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
355 CMTRACE(CM_REFERENCE_DEBUG
,
356 "%s - Referencing KCB: %p\n", __FUNCTION__
, Kcb
);
358 /* Check if this is the KCB's first reference */
359 if (Kcb
->RefCount
== 0)
361 /* Check if the KCB is locked in shared mode */
362 if (!CmpIsKcbLockedExclusive(Kcb
))
364 /* Convert it to exclusive */
365 if (!CmpTryToConvertKcbSharedToExclusive(Kcb
))
367 /* Set the delayed close index so that we can be ignored */
368 Kcb
->DelayedCloseIndex
= 1;
370 /* Increase the reference count while we release the lock */
371 InterlockedIncrement((PLONG
)&Kcb
->RefCount
);
373 /* Go from shared to exclusive */
374 CmpConvertKcbSharedToExclusive(Kcb
);
376 /* Decrement the reference count; the lock is now held again */
377 InterlockedDecrement((PLONG
)&Kcb
->RefCount
);
379 /* Check if we still control the index */
380 if (Kcb
->DelayedCloseIndex
== 1)
383 Kcb
->DelayedCloseIndex
= 0;
388 ASSERT((Kcb
->DelayedCloseIndex
== CmpDelayedCloseSize
) ||
389 (Kcb
->DelayedCloseIndex
== 0));
395 /* Increase the reference count */
396 if ((InterlockedIncrement((PLONG
)&Kcb
->RefCount
) & 0xFFFF) == 0)
398 /* We've overflown to 64K references, bail out */
399 InterlockedDecrement((PLONG
)&Kcb
->RefCount
);
403 /* Check if this was the last close index */
404 if (!Kcb
->DelayedCloseIndex
)
406 /* Check if the KCB is locked in shared mode */
407 if (!CmpIsKcbLockedExclusive(Kcb
))
409 /* Convert it to exclusive */
410 if (!CmpTryToConvertKcbSharedToExclusive(Kcb
))
412 /* Go from shared to exclusive */
413 CmpConvertKcbSharedToExclusive(Kcb
);
417 /* If we're still the last entry, remove us */
418 if (!Kcb
->DelayedCloseIndex
) CmpRemoveFromDelayedClose(Kcb
);
427 CmpCleanUpKcbValueCache(IN PCM_KEY_CONTROL_BLOCK Kcb
)
429 PULONG_PTR CachedList
;
433 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
434 (CmpTestRegistryLockExclusive() == TRUE
));
436 /* Check if the value list is cached */
437 if (CMP_IS_CELL_CACHED(Kcb
->ValueCache
.ValueList
))
439 /* Get the cache list */
440 CachedList
= (PULONG_PTR
)CMP_GET_CACHED_DATA(Kcb
->ValueCache
.ValueList
);
441 for (i
= 0; i
< Kcb
->ValueCache
.Count
; i
++)
443 /* Check if this cell is cached */
444 if (CMP_IS_CELL_CACHED(CachedList
[i
]))
447 CmpFree((PVOID
)CMP_GET_CACHED_CELL(CachedList
[i
]), 0);
451 /* Now free the list */
452 CmpFree((PVOID
)CMP_GET_CACHED_CELL(Kcb
->ValueCache
.ValueList
), 0);
453 Kcb
->ValueCache
.ValueList
= HCELL_NIL
;
455 else if (Kcb
->ExtFlags
& CM_KCB_SYM_LINK_FOUND
)
457 /* This is a sym link, check if there's only one reference left */
458 if ((Kcb
->ValueCache
.RealKcb
->RefCount
== 1) &&
459 !(Kcb
->ValueCache
.RealKcb
->Delete
))
461 /* Disable delay close for the KCB */
462 Kcb
->ValueCache
.RealKcb
->ExtFlags
|= CM_KCB_NO_DELAY_CLOSE
;
465 /* Dereference the KCB */
466 CmpDelayDerefKeyControlBlock(Kcb
->ValueCache
.RealKcb
);
467 Kcb
->ExtFlags
&= ~CM_KCB_SYM_LINK_FOUND
;
473 CmpCleanUpKcbCacheWithLock(IN PCM_KEY_CONTROL_BLOCK Kcb
,
474 IN BOOLEAN LockHeldExclusively
)
476 PCM_KEY_CONTROL_BLOCK Parent
;
480 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
481 (CmpTestRegistryLockExclusive() == TRUE
));
482 ASSERT(Kcb
->RefCount
== 0);
484 /* Cleanup the value cache */
485 CmpCleanUpKcbValueCache(Kcb
);
487 /* Dereference the NCB */
488 CmpDereferenceNameControlBlockWithLock(Kcb
->NameBlock
);
490 /* Check if we have an index hint block and free it */
491 if (Kcb
->ExtFlags
& CM_KCB_SUBKEY_HINT
) CmpFree(Kcb
->IndexHint
, 0);
493 /* Check if we were already deleted */
494 Parent
= Kcb
->ParentKcb
;
495 if (!Kcb
->Delete
) CmpRemoveKeyControlBlock(Kcb
);
497 /* Set invalid KCB signature */
498 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
500 /* Free the KCB as well */
501 CmpFreeKeyControlBlock(Kcb
);
503 /* Check if we have a parent */
506 /* Dereference the parent */
507 LockHeldExclusively
?
508 CmpDereferenceKeyControlBlockWithLock(Parent
,LockHeldExclusively
) :
509 CmpDelayDerefKeyControlBlock(Parent
);
515 CmpCleanUpSubKeyInfo(IN PCM_KEY_CONTROL_BLOCK Kcb
)
517 PCM_KEY_NODE KeyNode
;
520 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
521 (CmpTestRegistryLockExclusive() == TRUE
));
523 /* Check if there's any cached subkey */
524 if (Kcb
->ExtFlags
& (CM_KCB_NO_SUBKEY
| CM_KCB_SUBKEY_ONE
| CM_KCB_SUBKEY_HINT
))
526 /* Check if there's a hint */
527 if (Kcb
->ExtFlags
& (CM_KCB_SUBKEY_HINT
))
530 CmpFree(Kcb
->IndexHint
, 0);
533 /* Remove subkey flags */
534 Kcb
->ExtFlags
&= ~(CM_KCB_NO_SUBKEY
| CM_KCB_SUBKEY_ONE
| CM_KCB_SUBKEY_HINT
);
537 /* Check if there's no linked cell */
538 if (Kcb
->KeyCell
== HCELL_NIL
)
540 /* Make sure it's a delete */
546 /* Get the key node */
547 KeyNode
= (PCM_KEY_NODE
)HvGetCell(Kcb
->KeyHive
, Kcb
->KeyCell
);
550 /* Check if we got the node */
553 /* We didn't, mark the cached data invalid */
554 Kcb
->ExtFlags
|= CM_KCB_INVALID_CACHED_INFO
;
558 /* We have a keynode, update subkey counts */
559 Kcb
->ExtFlags
&= ~CM_KCB_INVALID_CACHED_INFO
;
560 Kcb
->SubKeyCount
= KeyNode
->SubKeyCounts
[Stable
] +
561 KeyNode
->SubKeyCounts
[Volatile
];
563 /* Release the cell */
564 HvReleaseCell(Kcb
->KeyHive
, Kcb
->KeyCell
);
570 CmpDereferenceKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
572 LONG OldRefCount
, NewRefCount
;
574 CMTRACE(CM_REFERENCE_DEBUG
,
575 "%s - Dereferencing KCB: %p\n", __FUNCTION__
, Kcb
);
577 /* Get the ref count and update it */
578 OldRefCount
= *(PLONG
)&Kcb
->RefCount
;
579 NewRefCount
= OldRefCount
- 1;
581 /* Check if we still have references */
582 if( (NewRefCount
& 0xFFFF) > 0)
584 /* Do the dereference */
585 if (InterlockedCompareExchange((PLONG
)&Kcb
->RefCount
,
587 OldRefCount
) == OldRefCount
)
595 ConvKey
= Kcb
->ConvKey
;
597 /* Do the dereference inside the lock */
598 CmpAcquireKcbLockExclusive(Kcb
);
599 CmpDereferenceKeyControlBlockWithLock(Kcb
, FALSE
);
600 CmpReleaseKcbLockByKey(ConvKey
);
605 CmpDereferenceKeyControlBlockWithLock(IN PCM_KEY_CONTROL_BLOCK Kcb
,
606 IN BOOLEAN LockHeldExclusively
)
608 CMTRACE(CM_REFERENCE_DEBUG
,
609 "%s - Dereferencing KCB: %p\n", __FUNCTION__
, Kcb
);
612 ASSERT_KCB_VALID(Kcb
);
614 /* Check if this is the last reference */
615 if ((InterlockedDecrement((PLONG
)&Kcb
->RefCount
) & 0xFFFF) == 0)
618 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
619 (CmpTestRegistryLockExclusive() == TRUE
));
621 /* Check if we should do a direct delete */
622 if (((CmpHoldLazyFlush
) &&
623 !(Kcb
->ExtFlags
& CM_KCB_SYM_LINK_FOUND
) &&
624 !(Kcb
->Flags
& KEY_SYM_LINK
)) ||
625 (Kcb
->ExtFlags
& CM_KCB_NO_DELAY_CLOSE
) ||
628 /* Clean up the KCB*/
629 CmpCleanUpKcbCacheWithLock(Kcb
, LockHeldExclusively
);
633 /* Otherwise, use delayed close */
634 CmpAddToDelayedClose(Kcb
, LockHeldExclusively
);
641 InitializeKCBKeyBodyList(IN PCM_KEY_CONTROL_BLOCK Kcb
)
643 /* Initialize the list */
644 InitializeListHead(&Kcb
->KeyBodyListHead
);
646 /* Clear the bodies */
647 Kcb
->KeyBodyArray
[0] =
648 Kcb
->KeyBodyArray
[1] =
649 Kcb
->KeyBodyArray
[2] =
650 Kcb
->KeyBodyArray
[3] = NULL
;
653 PCM_KEY_CONTROL_BLOCK
655 CmpCreateKeyControlBlock(IN PHHIVE Hive
,
656 IN HCELL_INDEX Index
,
657 IN PCM_KEY_NODE Node
,
658 IN PCM_KEY_CONTROL_BLOCK Parent
,
660 IN PUNICODE_STRING KeyName
)
662 PCM_KEY_CONTROL_BLOCK Kcb
, FoundKcb
= NULL
;
663 UNICODE_STRING NodeName
;
664 ULONG ConvKey
= 0, i
;
665 BOOLEAN IsFake
, HashLock
;
668 /* Make sure we own this hive in case it's being unloaded */
669 if ((Hive
->HiveFlags
& HIVE_IS_UNLOADING
) &&
670 (((PCMHIVE
)Hive
)->CreatorOwner
!= KeGetCurrentThread()))
676 /* Check if this is a fake KCB */
677 IsFake
= Flags
& CMP_CREATE_FAKE_KCB
? TRUE
: FALSE
;
679 /* If we have a parent, use its ConvKey */
680 if (Parent
) ConvKey
= Parent
->ConvKey
;
682 /* Make a copy of the name */
685 /* Remove leading slash */
686 while ((NodeName
.Length
) && (*NodeName
.Buffer
== OBJ_NAME_PATH_SEPARATOR
))
688 /* Move the buffer by one */
690 NodeName
.Length
-= sizeof(WCHAR
);
693 /* Make sure we didn't get just a slash or something */
694 ASSERT(NodeName
.Length
> 0);
696 /* Now setup the hash */
698 for (i
= 0; i
< NodeName
.Length
; i
+= sizeof(WCHAR
))
700 /* Make sure it's a valid character */
701 if (*p
!= OBJ_NAME_PATH_SEPARATOR
)
703 /* Add this key to the hash */
704 ConvKey
= 37 * ConvKey
+ RtlUpcaseUnicodeChar(*p
);
711 /* Allocate the KCB */
712 Kcb
= CmpAllocateKeyControlBlock();
713 if (!Kcb
) return NULL
;
715 /* Initailize the key list */
716 InitializeKCBKeyBodyList(Kcb
);
719 Kcb
->Signature
= CM_KCB_SIGNATURE
;
723 Kcb
->KeyCell
= Index
;
724 Kcb
->ConvKey
= ConvKey
;
725 Kcb
->DelayedCloseIndex
= CmpDelayedCloseSize
;
726 Kcb
->InDelayClose
= 0;
727 ASSERT_KCB_VALID(Kcb
);
729 /* Check if we have two hash entires */
730 HashLock
= Flags
& CMP_LOCK_HASHES_FOR_KCB
? TRUE
: FALSE
;
733 /* It's not locked, do we have a parent? */
736 /* Lock the parent KCB and ourselves */
737 CmpAcquireTwoKcbLocksExclusiveByKey(ConvKey
, Parent
->ConvKey
);
741 /* Lock only ourselves */
742 CmpAcquireKcbLockExclusive(Kcb
);
746 /* Check if we already have a KCB */
747 FoundKcb
= CmpInsertKeyHash(&Kcb
->KeyHash
, IsFake
);
751 ASSERT(!FoundKcb
->Delete
);
752 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
754 /* Free the one we allocated and reference this one */
755 CmpFreeKeyControlBlock(Kcb
);
756 ASSERT_KCB_VALID(FoundKcb
);
758 if (!CmpReferenceKeyControlBlock(Kcb
))
760 /* We got too many handles */
761 ASSERT(Kcb
->RefCount
+ 1 != 0);
766 /* Check if we're not creating a fake one, but it used to be fake */
767 if ((Kcb
->ExtFlags
& CM_KCB_KEY_NON_EXIST
) && !(IsFake
))
769 /* Set the hive and cell */
771 Kcb
->KeyCell
= Index
;
773 /* This means that our current information is invalid */
774 Kcb
->ExtFlags
= CM_KCB_INVALID_CACHED_INFO
;
777 /* Check if we didn't have any valid data */
778 if (!(Kcb
->ExtFlags
& (CM_KCB_NO_SUBKEY
|
780 CM_KCB_SUBKEY_HINT
)))
782 /* Calculate the index hint */
783 Kcb
->SubKeyCount
= Node
->SubKeyCounts
[Stable
] +
784 Node
->SubKeyCounts
[Volatile
];
786 /* Cached information is now valid */
787 Kcb
->ExtFlags
&= ~CM_KCB_INVALID_CACHED_INFO
;
790 /* Setup the other data */
791 Kcb
->KcbLastWriteTime
= Node
->LastWriteTime
;
792 Kcb
->KcbMaxNameLen
= (USHORT
)Node
->MaxNameLen
;
793 Kcb
->KcbMaxValueNameLen
= (USHORT
)Node
->MaxValueNameLen
;
794 Kcb
->KcbMaxValueDataLen
= Node
->MaxValueDataLen
;
799 /* No KCB, do we have a parent? */
802 /* Reference the parent */
803 if (((Parent
->TotalLevels
+ 1) < 512) &&
804 (CmpReferenceKeyControlBlock(Parent
)))
807 Kcb
->ParentKcb
= Parent
;
808 Kcb
->TotalLevels
= Parent
->TotalLevels
+ 1;
812 /* Remove the KCB and free it */
813 CmpRemoveKeyControlBlock(Kcb
);
814 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
815 CmpFreeKeyControlBlock(Kcb
);
821 /* No parent, this is the root node */
822 Kcb
->ParentKcb
= NULL
;
823 Kcb
->TotalLevels
= 1;
826 /* Check if we have a KCB */
830 Kcb
->NameBlock
= CmpGetNameControlBlock(&NodeName
);
834 Kcb
->ValueCache
.Count
= Node
->ValueList
.Count
;
835 Kcb
->ValueCache
.ValueList
= Node
->ValueList
.List
;
836 Kcb
->Flags
= Node
->Flags
;
838 Kcb
->DelayedCloseIndex
= CmpDelayedCloseSize
;
840 /* Remember if this is a fake key */
841 if (IsFake
) Kcb
->ExtFlags
|= CM_KCB_KEY_NON_EXIST
;
843 /* Setup the other data */
844 Kcb
->SubKeyCount
= Node
->SubKeyCounts
[Stable
] +
845 Node
->SubKeyCounts
[Volatile
];
846 Kcb
->KcbLastWriteTime
= Node
->LastWriteTime
;
847 Kcb
->KcbMaxNameLen
= (USHORT
)Node
->MaxNameLen
;
848 Kcb
->KcbMaxValueNameLen
= (USHORT
)Node
->MaxValueNameLen
;
849 Kcb
->KcbMaxValueDataLen
= (USHORT
)Node
->MaxValueDataLen
;
853 /* Dereference the KCB */
854 CmpDereferenceKeyControlBlockWithLock(Parent
, FALSE
);
856 /* Remove the KCB and free it */
857 CmpRemoveKeyControlBlock(Kcb
);
858 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
859 CmpFreeKeyControlBlock(Kcb
);
865 /* Check if this is a KCB inside a frozen hive */
866 if ((Kcb
) && (((PCMHIVE
)Hive
)->Frozen
) && (!(Kcb
->Flags
& KEY_SYM_LINK
)))
868 /* Don't add these to the delay close */
869 Kcb
->ExtFlags
|= CM_KCB_NO_DELAY_CLOSE
;
873 ASSERT((!Kcb
) || (Kcb
->Delete
== FALSE
));
875 /* Check if we had locked the hashes */
878 /* We locked them manually, do we have a parent? */
881 /* Unlock the parent KCB and ourselves */
882 CmpReleaseTwoKcbLockByKey(ConvKey
, Parent
->ConvKey
);
886 /* Unlock only ourselves */
887 CmpReleaseKcbLockByKey(ConvKey
);
897 CmpConstructName(IN PCM_KEY_CONTROL_BLOCK Kcb
)
899 PUNICODE_STRING KeyName
;
901 PCM_KEY_CONTROL_BLOCK MyKcb
;
902 PCM_KEY_NODE KeyNode
;
903 BOOLEAN DeletedKey
= FALSE
;
904 PWCHAR TargetBuffer
, CurrentNameW
;
907 /* Calculate how much size our key name is going to occupy */
913 /* Add length of the name */
914 if (!MyKcb
->NameBlock
->Compressed
)
916 NameLength
+= MyKcb
->NameBlock
->NameLength
;
920 NameLength
+= CmpCompressedNameSize(MyKcb
->NameBlock
->Name
,
921 MyKcb
->NameBlock
->NameLength
);
924 /* Sum up the separator too */
925 NameLength
+= sizeof(WCHAR
);
927 /* Go to the parent KCB */
928 MyKcb
= MyKcb
->ParentKcb
;
931 /* Allocate the unicode string now */
932 KeyName
= CmpAllocate(NameLength
+ sizeof(UNICODE_STRING
),
936 if (!KeyName
) return NULL
;
939 KeyName
->Buffer
= (PWSTR
)(KeyName
+ 1);
940 KeyName
->Length
= NameLength
;
941 KeyName
->MaximumLength
= NameLength
;
943 /* Loop the keys again, now adding names */
949 /* Sanity checks for deleted and fake keys */
950 if ((!MyKcb
->KeyCell
&& !MyKcb
->Delete
) ||
952 MyKcb
->ExtFlags
& CM_KCB_KEY_NON_EXIST
)
959 /* Try to get the name from the keynode,
960 if the key is not deleted */
961 if (!DeletedKey
&& !MyKcb
->Delete
)
963 KeyNode
= HvGetCell(MyKcb
->KeyHive
, MyKcb
->KeyCell
);
974 /* The key was deleted */
979 /* Get the pointer to the beginning of the current key name */
980 NameLength
+= (MyKcb
->NameBlock
->NameLength
+ 1) * sizeof(WCHAR
);
981 TargetBuffer
= &KeyName
->Buffer
[(KeyName
->Length
- NameLength
) / sizeof(WCHAR
)];
983 /* Add a separator */
984 TargetBuffer
[0] = OBJ_NAME_PATH_SEPARATOR
;
986 /* Add the name, but remember to go from the end to the beginning */
987 if (!MyKcb
->NameBlock
->Compressed
)
989 /* Get the pointer to the name (from the keynode, if possible) */
990 if ((MyKcb
->Flags
& (KEY_HIVE_ENTRY
| KEY_HIVE_EXIT
)) ||
993 CurrentNameW
= MyKcb
->NameBlock
->Name
;
997 CurrentNameW
= KeyNode
->Name
;
1001 for (i
=0; i
< MyKcb
->NameBlock
->NameLength
; i
++)
1003 TargetBuffer
[i
+1] = *CurrentNameW
;
1009 /* Get the pointer to the name (from the keynode, if possible) */
1010 if ((MyKcb
->Flags
& (KEY_HIVE_ENTRY
| KEY_HIVE_EXIT
)) ||
1013 CurrentName
= (PUCHAR
)MyKcb
->NameBlock
->Name
;
1017 CurrentName
= (PUCHAR
)KeyNode
->Name
;
1021 for (i
=0; i
< MyKcb
->NameBlock
->NameLength
; i
++)
1023 TargetBuffer
[i
+1] = (WCHAR
)*CurrentName
;
1028 /* Release the cell, if needed */
1029 if (KeyNode
) HvReleaseCell(MyKcb
->KeyHive
, MyKcb
->KeyCell
);
1031 /* Go to the parent KCB */
1032 MyKcb
= MyKcb
->ParentKcb
;
1035 /* Return resulting buffer (both UNICODE_STRING and
1036 its buffer following it) */
1042 EnlistKeyBodyWithKCB(IN PCM_KEY_BODY KeyBody
,
1048 ASSERT(KeyBody
->KeyControlBlock
!= NULL
);
1050 /* Initialize the list entry */
1051 InitializeListHead(&KeyBody
->KeyBodyList
);
1053 /* Check if we can use the parent KCB array */
1054 for (i
= 0; i
< 4; i
++)
1056 /* Add it into the list */
1057 if (!InterlockedCompareExchangePointer((PVOID
*)&KeyBody
->KeyControlBlock
->
1067 /* Array full, check if we need to unlock the KCB */
1068 if (Flags
& CMP_ENLIST_KCB_LOCKED_SHARED
)
1070 /* It's shared, so release the KCB shared lock */
1071 CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1072 ASSERT(!(Flags
& CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
));
1075 /* Check if we need to lock the KCB */
1076 if (!(Flags
& CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
))
1078 /* Acquire the lock */
1079 CmpAcquireKcbLockExclusive(KeyBody
->KeyControlBlock
);
1082 /* Make sure we have the exclusive lock */
1083 ASSERT((CmpIsKcbLockedExclusive(KeyBody
->KeyControlBlock
) == TRUE
) ||
1084 (CmpTestRegistryLockExclusive() == TRUE
));
1087 InsertTailList(&KeyBody
->KeyControlBlock
->KeyBodyListHead
,
1088 &KeyBody
->KeyBodyList
);
1090 /* Check if we did a manual lock */
1091 if (!(Flags
& (CMP_ENLIST_KCB_LOCKED_SHARED
|
1092 CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
)))
1094 /* Release the lock */
1095 CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1101 DelistKeyBodyFromKCB(IN PCM_KEY_BODY KeyBody
,
1102 IN BOOLEAN LockHeld
)
1107 ASSERT(KeyBody
->KeyControlBlock
!= NULL
);
1109 /* Check if we can use the parent KCB array */
1110 for (i
= 0; i
< 4; i
++)
1112 /* Add it into the list */
1113 if (InterlockedCompareExchangePointer((VOID
*)&KeyBody
->KeyControlBlock
->
1116 KeyBody
) == KeyBody
)
1124 ASSERT(IsListEmpty(&KeyBody
->KeyControlBlock
->KeyBodyListHead
) == FALSE
);
1125 ASSERT(IsListEmpty(&KeyBody
->KeyBodyList
) == FALSE
);
1128 if (!LockHeld
) CmpAcquireKcbLockExclusive(KeyBody
->KeyControlBlock
);
1129 ASSERT((CmpIsKcbLockedExclusive(KeyBody
->KeyControlBlock
) == TRUE
) ||
1130 (CmpTestRegistryLockExclusive() == TRUE
));
1132 /* Remove the entry */
1133 RemoveEntryList(&KeyBody
->KeyBodyList
);
1135 /* Unlock it it if we did a manual lock */
1136 if (!LockHeld
) CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);