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 *****************************************************************/
26 CmpInitializeCache(VOID
)
30 /* Calculate length for the table */
31 Length
= CmpHashTableSize
* sizeof(CM_KEY_HASH_TABLE_ENTRY
);
34 CmpCacheTable
= CmpAllocate(Length
, TRUE
, TAG_CM
);
37 /* Take the system down */
38 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 3, 1, 0, 0);
41 /* Zero out the table */
42 RtlZeroMemory(CmpCacheTable
, Length
);
44 /* Initialize the locks */
45 for (i
= 0;i
< CmpHashTableSize
; i
++)
47 /* Setup the pushlock */
48 ExInitializePushLock(&CmpCacheTable
[i
].Lock
);
51 /* Calculate length for the name cache */
52 Length
= CmpHashTableSize
* sizeof(CM_NAME_HASH_TABLE_ENTRY
);
54 /* Now allocate the name cache table */
55 CmpNameCacheTable
= CmpAllocate(Length
, TRUE
, TAG_CM
);
56 if (!CmpNameCacheTable
)
58 /* Take the system down */
59 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 3, 3, 0, 0);
62 /* Zero out the table */
63 RtlZeroMemory(CmpNameCacheTable
, Length
);
65 /* Initialize the locks */
66 for (i
= 0;i
< CmpHashTableSize
; i
++)
68 /* Setup the pushlock */
69 ExInitializePushLock(&CmpNameCacheTable
[i
].Lock
);
72 /* Setup the delayed close table */
73 CmpInitializeDelayedCloseTable();
78 CmpRemoveKeyHash(IN PCM_KEY_HASH KeyHash
)
82 ASSERT_VALID_HASH(KeyHash
);
84 /* Lookup all the keys in this index entry */
85 Prev
= &GET_HASH_ENTRY(CmpCacheTable
, KeyHash
->ConvKey
)->Entry
;
88 /* Save the current one and make sure it's valid */
90 ASSERT(Current
!= NULL
);
91 ASSERT_VALID_HASH(Current
);
93 /* Check if it matches */
94 if (Current
== KeyHash
)
96 /* Then write the previous one */
97 *Prev
= Current
->NextHash
;
98 if (*Prev
) ASSERT_VALID_HASH(*Prev
);
102 /* Otherwise, keep going */
103 Prev
= &Current
->NextHash
;
107 PCM_KEY_CONTROL_BLOCK
109 CmpInsertKeyHash(IN PCM_KEY_HASH KeyHash
,
114 ASSERT_VALID_HASH(KeyHash
);
116 /* Get the hash index */
117 i
= GET_HASH_INDEX(KeyHash
->ConvKey
);
119 /* If this is a fake key, increase the key cell to use the parent data */
120 if (IsFake
) KeyHash
->KeyCell
++;
122 /* Loop the hash table */
123 Entry
= CmpCacheTable
[i
].Entry
;
126 /* Check if this matches */
127 ASSERT_VALID_HASH(Entry
);
128 if ((KeyHash
->ConvKey
== Entry
->ConvKey
) &&
129 (KeyHash
->KeyCell
== Entry
->KeyCell
) &&
130 (KeyHash
->KeyHive
== Entry
->KeyHive
))
133 return CONTAINING_RECORD(Entry
, CM_KEY_CONTROL_BLOCK
, KeyHash
);
137 Entry
= Entry
->NextHash
;
140 /* No entry found, add this one and return NULL since none existed */
141 KeyHash
->NextHash
= CmpCacheTable
[i
].Entry
;
142 CmpCacheTable
[i
].Entry
= KeyHash
;
146 PCM_NAME_CONTROL_BLOCK
148 CmpGetNameControlBlock(IN PUNICODE_STRING NodeName
)
150 PCM_NAME_CONTROL_BLOCK Ncb
= NULL
;
154 BOOLEAN IsCompressed
= TRUE
, Found
= FALSE
;
155 PCM_NAME_HASH HashEntry
;
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 */
239 ASSERT(Ncb
->RefCount
!= 0xFFFF);
245 /* Go to the next hash */
246 HashEntry
= HashEntry
->NextHash
;
249 /* Check if we didn't find it */
253 NcbSize
= FIELD_OFFSET(CM_NAME_CONTROL_BLOCK
, Name
) + Length
;
254 Ncb
= CmpAllocate(NcbSize
, TRUE
, TAG_CM
);
257 /* Release the lock and fail */
258 CmpReleaseNcbLockByKey(ConvKey
);
263 RtlZeroMemory(Ncb
, NcbSize
);
265 /* Check if the name was compressed */
268 /* Copy the compressed name */
269 for (i
= 0; i
< NodeName
->Length
/ sizeof(WCHAR
); i
++)
271 /* Copy Unicode to ANSI */
272 ((PCHAR
)Ncb
->Name
)[i
] = (CHAR
)RtlUpcaseUnicodeChar(NodeName
->Buffer
[i
]);
277 /* Copy the name directly */
278 for (i
= 0; i
< NodeName
->Length
/ sizeof(WCHAR
); i
++)
280 /* Copy each unicode character */
281 Ncb
->Name
[i
] = RtlUpcaseUnicodeChar(NodeName
->Buffer
[i
]);
285 /* Setup the rest of the NCB */
286 Ncb
->Compressed
= IsCompressed
;
287 Ncb
->ConvKey
= ConvKey
;
289 Ncb
->NameLength
= Length
;
291 /* Insert the name in the hash table */
292 HashEntry
= &Ncb
->NameHash
;
293 HashEntry
->NextHash
= GET_HASH_ENTRY(CmpNameCacheTable
, ConvKey
)->Entry
;
294 GET_HASH_ENTRY(CmpNameCacheTable
, ConvKey
)->Entry
= HashEntry
;
297 /* Release NCB lock */
298 CmpReleaseNcbLockByKey(ConvKey
);
300 /* Return the NCB found */
306 CmpRemoveKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
308 /* Make sure we have the exclusive lock */
309 CMP_ASSERT_KCB_LOCK(Kcb
);
311 /* Remove the key hash */
312 CmpRemoveKeyHash(&Kcb
->KeyHash
);
317 CmpDereferenceNameControlBlockWithLock(IN PCM_NAME_CONTROL_BLOCK Ncb
)
319 PCM_NAME_HASH Current
, *Next
;
320 ULONG ConvKey
= Ncb
->ConvKey
;
323 CmpAcquireNcbLockExclusiveByKey(ConvKey
);
325 /* Decrease the reference count */
326 ASSERT(Ncb
->RefCount
>= 1);
327 if (!(--Ncb
->RefCount
))
329 /* Find the NCB in the table */
330 Next
= &GET_HASH_ENTRY(CmpNameCacheTable
, Ncb
->ConvKey
)->Entry
;
333 /* Check the current entry */
335 ASSERT(Current
!= NULL
);
336 if (Current
== &Ncb
->NameHash
)
339 *Next
= Current
->NextHash
;
343 /* Get to the next one */
344 Next
= &Current
->NextHash
;
347 /* Found it, now free it */
351 /* Release the lock */
352 CmpReleaseNcbLockByKey(ConvKey
);
357 CmpReferenceKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
359 CMTRACE(CM_REFERENCE_DEBUG
,
360 "%s - Referencing KCB: %p\n", __FUNCTION__
, Kcb
);
362 /* Check if this is the KCB's first reference */
363 if (Kcb
->RefCount
== 0)
365 /* Check if the KCB is locked in shared mode */
366 if (!CmpIsKcbLockedExclusive(Kcb
))
368 /* Convert it to exclusive */
369 if (!CmpTryToConvertKcbSharedToExclusive(Kcb
))
371 /* Set the delayed close index so that we can be ignored */
372 Kcb
->DelayedCloseIndex
= 1;
374 /* Increase the reference count while we release the lock */
375 InterlockedIncrement((PLONG
)&Kcb
->RefCount
);
377 /* Go from shared to exclusive */
378 CmpConvertKcbSharedToExclusive(Kcb
);
380 /* Decrement the reference count; the lock is now held again */
381 InterlockedDecrement((PLONG
)&Kcb
->RefCount
);
383 /* Check if we still control the index */
384 if (Kcb
->DelayedCloseIndex
== 1)
387 Kcb
->DelayedCloseIndex
= 0;
392 ASSERT((Kcb
->DelayedCloseIndex
== CmpDelayedCloseSize
) ||
393 (Kcb
->DelayedCloseIndex
== 0));
399 /* Increase the reference count */
400 if ((InterlockedIncrement((PLONG
)&Kcb
->RefCount
) & 0xFFFF) == 0)
402 /* We've overflown to 64K references, bail out */
403 InterlockedDecrement((PLONG
)&Kcb
->RefCount
);
407 /* Check if this was the last close index */
408 if (!Kcb
->DelayedCloseIndex
)
410 /* Check if the KCB is locked in shared mode */
411 if (!CmpIsKcbLockedExclusive(Kcb
))
413 /* Convert it to exclusive */
414 if (!CmpTryToConvertKcbSharedToExclusive(Kcb
))
416 /* Go from shared to exclusive */
417 CmpConvertKcbSharedToExclusive(Kcb
);
421 /* If we're still the last entry, remove us */
422 if (!Kcb
->DelayedCloseIndex
) CmpRemoveFromDelayedClose(Kcb
);
431 CmpCleanUpKcbValueCache(IN PCM_KEY_CONTROL_BLOCK Kcb
)
433 PULONG_PTR CachedList
;
436 /* Make sure we have the exclusive lock */
437 CMP_ASSERT_KCB_LOCK(Kcb
);
439 /* Check if the value list is cached */
440 if (CMP_IS_CELL_CACHED(Kcb
->ValueCache
.ValueList
))
442 /* Get the cache list */
443 CachedList
= (PULONG_PTR
)CMP_GET_CACHED_DATA(Kcb
->ValueCache
.ValueList
);
444 for (i
= 0; i
< Kcb
->ValueCache
.Count
; i
++)
446 /* Check if this cell is cached */
447 if (CMP_IS_CELL_CACHED(CachedList
[i
]))
450 CmpFree((PVOID
)CMP_GET_CACHED_CELL(CachedList
[i
]), 0);
454 /* Now free the list */
455 CmpFree((PVOID
)CMP_GET_CACHED_CELL(Kcb
->ValueCache
.ValueList
), 0);
456 Kcb
->ValueCache
.ValueList
= HCELL_NIL
;
458 else if (Kcb
->ExtFlags
& CM_KCB_SYM_LINK_FOUND
)
460 /* This is a sym link, check if there's only one reference left */
461 if ((Kcb
->ValueCache
.RealKcb
->RefCount
== 1) &&
462 !(Kcb
->ValueCache
.RealKcb
->Delete
))
464 /* Disable delay close for the KCB */
465 Kcb
->ValueCache
.RealKcb
->ExtFlags
|= CM_KCB_NO_DELAY_CLOSE
;
468 /* Dereference the KCB */
469 CmpDelayDerefKeyControlBlock(Kcb
->ValueCache
.RealKcb
);
470 Kcb
->ExtFlags
&= ~CM_KCB_SYM_LINK_FOUND
;
476 CmpCleanUpKcbCacheWithLock(IN PCM_KEY_CONTROL_BLOCK Kcb
,
477 IN BOOLEAN LockHeldExclusively
)
479 PCM_KEY_CONTROL_BLOCK Parent
;
483 CMP_ASSERT_KCB_LOCK(Kcb
);
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
;
521 /* Make sure we have the exclusive lock */
522 CMP_ASSERT_KCB_LOCK(Kcb
);
524 /* Check if there's any cached subkey */
525 if (Kcb
->ExtFlags
& (CM_KCB_NO_SUBKEY
| CM_KCB_SUBKEY_ONE
| CM_KCB_SUBKEY_HINT
))
527 /* Check if there's a hint */
528 if (Kcb
->ExtFlags
& (CM_KCB_SUBKEY_HINT
))
531 CmpFree(Kcb
->IndexHint
, 0);
534 /* Remove subkey flags */
535 Kcb
->ExtFlags
&= ~(CM_KCB_NO_SUBKEY
| CM_KCB_SUBKEY_ONE
| CM_KCB_SUBKEY_HINT
);
538 /* Check if there's no linked cell */
539 if (Kcb
->KeyCell
== HCELL_NIL
)
541 /* Make sure it's a delete */
547 /* Get the key node */
548 KeyNode
= (PCM_KEY_NODE
)HvGetCell(Kcb
->KeyHive
, Kcb
->KeyCell
);
551 /* Check if we got the node */
554 /* We didn't, mark the cached data invalid */
555 Kcb
->ExtFlags
|= CM_KCB_INVALID_CACHED_INFO
;
559 /* We have a keynode, update subkey counts */
560 Kcb
->ExtFlags
&= ~CM_KCB_INVALID_CACHED_INFO
;
561 Kcb
->SubKeyCount
= KeyNode
->SubKeyCounts
[Stable
] +
562 KeyNode
->SubKeyCounts
[Volatile
];
564 /* Release the cell */
565 HvReleaseCell(Kcb
->KeyHive
, Kcb
->KeyCell
);
571 CmpDereferenceKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
573 LONG OldRefCount
, NewRefCount
;
575 CMTRACE(CM_REFERENCE_DEBUG
,
576 "%s - Dereferencing KCB: %p\n", __FUNCTION__
, Kcb
);
578 /* Get the ref count and update it */
579 OldRefCount
= *(PLONG
)&Kcb
->RefCount
;
580 NewRefCount
= OldRefCount
- 1;
582 /* Check if we still have references */
583 if ((NewRefCount
& 0xFFFF) > 0)
585 /* Do the dereference */
586 if (InterlockedCompareExchange((PLONG
)&Kcb
->RefCount
,
588 OldRefCount
) == OldRefCount
)
596 ConvKey
= Kcb
->ConvKey
;
598 /* Do the dereference inside the lock */
599 CmpAcquireKcbLockExclusive(Kcb
);
600 CmpDereferenceKeyControlBlockWithLock(Kcb
, FALSE
);
601 CmpReleaseKcbLockByKey(ConvKey
);
606 CmpDereferenceKeyControlBlockWithLock(IN PCM_KEY_CONTROL_BLOCK Kcb
,
607 IN BOOLEAN LockHeldExclusively
)
609 CMTRACE(CM_REFERENCE_DEBUG
,
610 "%s - Dereferencing KCB: %p\n", __FUNCTION__
, Kcb
);
613 ASSERT_KCB_VALID(Kcb
);
615 /* Check if this is the last reference */
616 if ((InterlockedDecrement((PLONG
)&Kcb
->RefCount
) & 0xFFFF) == 0)
618 /* Make sure we have the exclusive lock */
619 CMP_ASSERT_KCB_LOCK(Kcb
);
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
;
902 PCM_KEY_CONTROL_BLOCK MyKcb
;
903 PCM_KEY_NODE KeyNode
;
904 BOOLEAN DeletedKey
= FALSE
;
905 PWCHAR TargetBuffer
, CurrentNameW
;
908 /* Calculate how much size our key name is going to occupy */
914 /* Add length of the name */
915 if (!MyKcb
->NameBlock
->Compressed
)
917 NameLength
+= MyKcb
->NameBlock
->NameLength
;
921 NameLength
+= CmpCompressedNameSize(MyKcb
->NameBlock
->Name
,
922 MyKcb
->NameBlock
->NameLength
);
925 /* Sum up the separator too */
926 NameLength
+= sizeof(WCHAR
);
928 /* Go to the parent KCB */
929 MyKcb
= MyKcb
->ParentKcb
;
932 /* Allocate the unicode string now */
933 KeyName
= CmpAllocate(NameLength
+ sizeof(UNICODE_STRING
),
937 if (!KeyName
) return NULL
;
940 KeyName
->Buffer
= (PWSTR
)(KeyName
+ 1);
941 KeyName
->Length
= NameLength
;
942 KeyName
->MaximumLength
= NameLength
;
944 /* Loop the keys again, now adding names */
950 /* Sanity checks for deleted and fake keys */
951 if ((!MyKcb
->KeyCell
&& !MyKcb
->Delete
) ||
953 MyKcb
->ExtFlags
& CM_KCB_KEY_NON_EXIST
)
960 /* Try to get the name from the keynode,
961 if the key is not deleted */
962 if (!DeletedKey
&& !MyKcb
->Delete
)
964 KeyNode
= HvGetCell(MyKcb
->KeyHive
, MyKcb
->KeyCell
);
975 /* The key was deleted */
980 /* Get the pointer to the beginning of the current key name */
981 NameLength
+= (MyKcb
->NameBlock
->NameLength
+ 1) * sizeof(WCHAR
);
982 TargetBuffer
= &KeyName
->Buffer
[(KeyName
->Length
- NameLength
) / sizeof(WCHAR
)];
984 /* Add a separator */
985 TargetBuffer
[0] = OBJ_NAME_PATH_SEPARATOR
;
987 /* Add the name, but remember to go from the end to the beginning */
988 if (!MyKcb
->NameBlock
->Compressed
)
990 /* Get the pointer to the name (from the keynode, if possible) */
991 if ((MyKcb
->Flags
& (KEY_HIVE_ENTRY
| KEY_HIVE_EXIT
)) ||
994 CurrentNameW
= MyKcb
->NameBlock
->Name
;
998 CurrentNameW
= KeyNode
->Name
;
1002 for (i
=0; i
< MyKcb
->NameBlock
->NameLength
; i
++)
1004 TargetBuffer
[i
+1] = *CurrentNameW
;
1010 /* Get the pointer to the name (from the keynode, if possible) */
1011 if ((MyKcb
->Flags
& (KEY_HIVE_ENTRY
| KEY_HIVE_EXIT
)) ||
1014 CurrentName
= (PUCHAR
)MyKcb
->NameBlock
->Name
;
1018 CurrentName
= (PUCHAR
)KeyNode
->Name
;
1022 for (i
=0; i
< MyKcb
->NameBlock
->NameLength
; i
++)
1024 TargetBuffer
[i
+1] = (WCHAR
)*CurrentName
;
1029 /* Release the cell, if needed */
1030 if (KeyNode
) HvReleaseCell(MyKcb
->KeyHive
, MyKcb
->KeyCell
);
1032 /* Go to the parent KCB */
1033 MyKcb
= MyKcb
->ParentKcb
;
1036 /* Return resulting buffer (both UNICODE_STRING and
1037 its buffer following it) */
1043 EnlistKeyBodyWithKCB(IN PCM_KEY_BODY KeyBody
,
1049 ASSERT(KeyBody
->KeyControlBlock
!= NULL
);
1051 /* Initialize the list entry */
1052 InitializeListHead(&KeyBody
->KeyBodyList
);
1054 /* Check if we can use the parent KCB array */
1055 for (i
= 0; i
< 4; i
++)
1057 /* Add it into the list */
1058 if (!InterlockedCompareExchangePointer((PVOID
*)&KeyBody
->KeyControlBlock
->
1068 /* Array full, check if we need to unlock the KCB */
1069 if (Flags
& CMP_ENLIST_KCB_LOCKED_SHARED
)
1071 /* It's shared, so release the KCB shared lock */
1072 CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1073 ASSERT(!(Flags
& CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
));
1076 /* Check if we need to lock the KCB */
1077 if (!(Flags
& CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
))
1079 /* Acquire the lock */
1080 CmpAcquireKcbLockExclusive(KeyBody
->KeyControlBlock
);
1083 /* Make sure we have the exclusive lock */
1084 CMP_ASSERT_KCB_LOCK(KeyBody
->KeyControlBlock
);
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 CMP_ASSERT_KCB_LOCK(KeyBody
->KeyControlBlock
);
1131 /* Remove the entry */
1132 RemoveEntryList(&KeyBody
->KeyBodyList
);
1134 /* Unlock it it if we did a manual lock */
1135 if (!LockHeld
) CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1140 CmpFlushNotifiesOnKeyBodyList(IN PCM_KEY_CONTROL_BLOCK Kcb
,
1141 IN BOOLEAN LockHeld
)
1143 PLIST_ENTRY NextEntry
, ListHead
;
1144 PCM_KEY_BODY KeyBody
;
1147 LockHeld
? CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK() : CmpIsKcbLockedExclusive(Kcb
);
1150 /* Is the list empty? */
1151 ListHead
= &Kcb
->KeyBodyListHead
;
1152 if (!IsListEmpty(ListHead
))
1155 NextEntry
= ListHead
->Flink
;
1156 while (NextEntry
!= ListHead
)
1158 /* Get the key body */
1159 KeyBody
= CONTAINING_RECORD(NextEntry
, CM_KEY_BODY
, KeyBodyList
);
1160 ASSERT(KeyBody
->Type
== CM_KEY_BODY_TYPE
);
1162 /* Check for notifications */
1163 if (KeyBody
->NotifyBlock
)
1165 /* Is the lock held? */
1169 CmpFlushNotify(KeyBody
, LockHeld
);
1170 ASSERT(KeyBody
->NotifyBlock
== NULL
);
1174 /* Lock isn't held, so we need to take a reference */
1175 if (ObReferenceObjectSafe(KeyBody
))
1177 /* Now we can flush */
1178 CmpFlushNotify(KeyBody
, LockHeld
);
1179 ASSERT(KeyBody
->NotifyBlock
== NULL
);
1181 /* Release the reference we took */
1182 ObDereferenceObjectDeferDelete(KeyBody
);
1187 /* Try the next entry */
1188 NextEntry
= NextEntry
->Flink
;
1192 /* List has been parsed, exit */