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 that the registry and KCB are utterly locked */
309 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
310 (CmpTestRegistryLockExclusive() == TRUE
));
312 /* Remove the key hash */
313 CmpRemoveKeyHash(&Kcb
->KeyHash
);
318 CmpDereferenceNameControlBlockWithLock(IN PCM_NAME_CONTROL_BLOCK Ncb
)
320 PCM_NAME_HASH Current
, *Next
;
321 ULONG ConvKey
= Ncb
->ConvKey
;
324 CmpAcquireNcbLockExclusiveByKey(ConvKey
);
326 /* Decrease the reference count */
327 ASSERT(Ncb
->RefCount
>= 1);
328 if (!(--Ncb
->RefCount
))
330 /* Find the NCB in the table */
331 Next
= &GET_HASH_ENTRY(CmpNameCacheTable
, Ncb
->ConvKey
).Entry
;
334 /* Check the current entry */
336 ASSERT(Current
!= NULL
);
337 if (Current
== &Ncb
->NameHash
)
340 *Next
= Current
->NextHash
;
344 /* Get to the next one */
345 Next
= &Current
->NextHash
;
348 /* Found it, now free it */
352 /* Release the lock */
353 CmpReleaseNcbLockByKey(ConvKey
);
358 CmpReferenceKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
360 CMTRACE(CM_REFERENCE_DEBUG
,
361 "%s - Referencing KCB: %p\n", __FUNCTION__
, Kcb
);
363 /* Check if this is the KCB's first reference */
364 if (Kcb
->RefCount
== 0)
366 /* Check if the KCB is locked in shared mode */
367 if (!CmpIsKcbLockedExclusive(Kcb
))
369 /* Convert it to exclusive */
370 if (!CmpTryToConvertKcbSharedToExclusive(Kcb
))
372 /* Set the delayed close index so that we can be ignored */
373 Kcb
->DelayedCloseIndex
= 1;
375 /* Increase the reference count while we release the lock */
376 InterlockedIncrement((PLONG
)&Kcb
->RefCount
);
378 /* Go from shared to exclusive */
379 CmpConvertKcbSharedToExclusive(Kcb
);
381 /* Decrement the reference count; the lock is now held again */
382 InterlockedDecrement((PLONG
)&Kcb
->RefCount
);
384 /* Check if we still control the index */
385 if (Kcb
->DelayedCloseIndex
== 1)
388 Kcb
->DelayedCloseIndex
= 0;
393 ASSERT((Kcb
->DelayedCloseIndex
== CmpDelayedCloseSize
) ||
394 (Kcb
->DelayedCloseIndex
== 0));
400 /* Increase the reference count */
401 if ((InterlockedIncrement((PLONG
)&Kcb
->RefCount
) & 0xFFFF) == 0)
403 /* We've overflown to 64K references, bail out */
404 InterlockedDecrement((PLONG
)&Kcb
->RefCount
);
408 /* Check if this was the last close index */
409 if (!Kcb
->DelayedCloseIndex
)
411 /* Check if the KCB is locked in shared mode */
412 if (!CmpIsKcbLockedExclusive(Kcb
))
414 /* Convert it to exclusive */
415 if (!CmpTryToConvertKcbSharedToExclusive(Kcb
))
417 /* Go from shared to exclusive */
418 CmpConvertKcbSharedToExclusive(Kcb
);
422 /* If we're still the last entry, remove us */
423 if (!Kcb
->DelayedCloseIndex
) CmpRemoveFromDelayedClose(Kcb
);
432 CmpCleanUpKcbValueCache(IN PCM_KEY_CONTROL_BLOCK Kcb
)
434 PULONG_PTR CachedList
;
438 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
439 (CmpTestRegistryLockExclusive() == TRUE
));
441 /* Check if the value list is cached */
442 if (CMP_IS_CELL_CACHED(Kcb
->ValueCache
.ValueList
))
444 /* Get the cache list */
445 CachedList
= (PULONG_PTR
)CMP_GET_CACHED_DATA(Kcb
->ValueCache
.ValueList
);
446 for (i
= 0; i
< Kcb
->ValueCache
.Count
; i
++)
448 /* Check if this cell is cached */
449 if (CMP_IS_CELL_CACHED(CachedList
[i
]))
452 CmpFree((PVOID
)CMP_GET_CACHED_CELL(CachedList
[i
]), 0);
456 /* Now free the list */
457 CmpFree((PVOID
)CMP_GET_CACHED_CELL(Kcb
->ValueCache
.ValueList
), 0);
458 Kcb
->ValueCache
.ValueList
= HCELL_NIL
;
460 else if (Kcb
->ExtFlags
& CM_KCB_SYM_LINK_FOUND
)
462 /* This is a sym link, check if there's only one reference left */
463 if ((Kcb
->ValueCache
.RealKcb
->RefCount
== 1) &&
464 !(Kcb
->ValueCache
.RealKcb
->Delete
))
466 /* Disable delay close for the KCB */
467 Kcb
->ValueCache
.RealKcb
->ExtFlags
|= CM_KCB_NO_DELAY_CLOSE
;
470 /* Dereference the KCB */
471 CmpDelayDerefKeyControlBlock(Kcb
->ValueCache
.RealKcb
);
472 Kcb
->ExtFlags
&= ~CM_KCB_SYM_LINK_FOUND
;
478 CmpCleanUpKcbCacheWithLock(IN PCM_KEY_CONTROL_BLOCK Kcb
,
479 IN BOOLEAN LockHeldExclusively
)
481 PCM_KEY_CONTROL_BLOCK Parent
;
485 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
486 (CmpTestRegistryLockExclusive() == TRUE
));
487 ASSERT(Kcb
->RefCount
== 0);
489 /* Cleanup the value cache */
490 CmpCleanUpKcbValueCache(Kcb
);
492 /* Dereference the NCB */
493 CmpDereferenceNameControlBlockWithLock(Kcb
->NameBlock
);
495 /* Check if we have an index hint block and free it */
496 if (Kcb
->ExtFlags
& CM_KCB_SUBKEY_HINT
) CmpFree(Kcb
->IndexHint
, 0);
498 /* Check if we were already deleted */
499 Parent
= Kcb
->ParentKcb
;
500 if (!Kcb
->Delete
) CmpRemoveKeyControlBlock(Kcb
);
502 /* Set invalid KCB signature */
503 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
505 /* Free the KCB as well */
506 CmpFreeKeyControlBlock(Kcb
);
508 /* Check if we have a parent */
511 /* Dereference the parent */
512 LockHeldExclusively
?
513 CmpDereferenceKeyControlBlockWithLock(Parent
,LockHeldExclusively
) :
514 CmpDelayDerefKeyControlBlock(Parent
);
520 CmpCleanUpSubKeyInfo(IN PCM_KEY_CONTROL_BLOCK Kcb
)
522 PCM_KEY_NODE KeyNode
;
525 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
526 (CmpTestRegistryLockExclusive() == TRUE
));
528 /* Check if there's any cached subkey */
529 if (Kcb
->ExtFlags
& (CM_KCB_NO_SUBKEY
| CM_KCB_SUBKEY_ONE
| CM_KCB_SUBKEY_HINT
))
531 /* Check if there's a hint */
532 if (Kcb
->ExtFlags
& (CM_KCB_SUBKEY_HINT
))
535 CmpFree(Kcb
->IndexHint
, 0);
538 /* Remove subkey flags */
539 Kcb
->ExtFlags
&= ~(CM_KCB_NO_SUBKEY
| CM_KCB_SUBKEY_ONE
| CM_KCB_SUBKEY_HINT
);
542 /* Check if there's no linked cell */
543 if (Kcb
->KeyCell
== HCELL_NIL
)
545 /* Make sure it's a delete */
551 /* Get the key node */
552 KeyNode
= (PCM_KEY_NODE
)HvGetCell(Kcb
->KeyHive
, Kcb
->KeyCell
);
555 /* Check if we got the node */
558 /* We didn't, mark the cached data invalid */
559 Kcb
->ExtFlags
|= CM_KCB_INVALID_CACHED_INFO
;
563 /* We have a keynode, update subkey counts */
564 Kcb
->ExtFlags
&= ~CM_KCB_INVALID_CACHED_INFO
;
565 Kcb
->SubKeyCount
= KeyNode
->SubKeyCounts
[Stable
] +
566 KeyNode
->SubKeyCounts
[Volatile
];
568 /* Release the cell */
569 HvReleaseCell(Kcb
->KeyHive
, Kcb
->KeyCell
);
575 CmpDereferenceKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb
)
577 LONG OldRefCount
, NewRefCount
;
579 CMTRACE(CM_REFERENCE_DEBUG
,
580 "%s - Dereferencing KCB: %p\n", __FUNCTION__
, Kcb
);
582 /* Get the ref count and update it */
583 OldRefCount
= *(PLONG
)&Kcb
->RefCount
;
584 NewRefCount
= OldRefCount
- 1;
586 /* Check if we still have references */
587 if ((NewRefCount
& 0xFFFF) > 0)
589 /* Do the dereference */
590 if (InterlockedCompareExchange((PLONG
)&Kcb
->RefCount
,
592 OldRefCount
) == OldRefCount
)
600 ConvKey
= Kcb
->ConvKey
;
602 /* Do the dereference inside the lock */
603 CmpAcquireKcbLockExclusive(Kcb
);
604 CmpDereferenceKeyControlBlockWithLock(Kcb
, FALSE
);
605 CmpReleaseKcbLockByKey(ConvKey
);
610 CmpDereferenceKeyControlBlockWithLock(IN PCM_KEY_CONTROL_BLOCK Kcb
,
611 IN BOOLEAN LockHeldExclusively
)
613 CMTRACE(CM_REFERENCE_DEBUG
,
614 "%s - Dereferencing KCB: %p\n", __FUNCTION__
, Kcb
);
617 ASSERT_KCB_VALID(Kcb
);
619 /* Check if this is the last reference */
620 if ((InterlockedDecrement((PLONG
)&Kcb
->RefCount
) & 0xFFFF) == 0)
623 ASSERT((CmpIsKcbLockedExclusive(Kcb
) == TRUE
) ||
624 (CmpTestRegistryLockExclusive() == TRUE
));
626 /* Check if we should do a direct delete */
627 if (((CmpHoldLazyFlush
) &&
628 !(Kcb
->ExtFlags
& CM_KCB_SYM_LINK_FOUND
) &&
629 !(Kcb
->Flags
& KEY_SYM_LINK
)) ||
630 (Kcb
->ExtFlags
& CM_KCB_NO_DELAY_CLOSE
) ||
633 /* Clean up the KCB*/
634 CmpCleanUpKcbCacheWithLock(Kcb
, LockHeldExclusively
);
638 /* Otherwise, use delayed close */
639 CmpAddToDelayedClose(Kcb
, LockHeldExclusively
);
646 InitializeKCBKeyBodyList(IN PCM_KEY_CONTROL_BLOCK Kcb
)
648 /* Initialize the list */
649 InitializeListHead(&Kcb
->KeyBodyListHead
);
651 /* Clear the bodies */
652 Kcb
->KeyBodyArray
[0] =
653 Kcb
->KeyBodyArray
[1] =
654 Kcb
->KeyBodyArray
[2] =
655 Kcb
->KeyBodyArray
[3] = NULL
;
658 PCM_KEY_CONTROL_BLOCK
660 CmpCreateKeyControlBlock(IN PHHIVE Hive
,
661 IN HCELL_INDEX Index
,
662 IN PCM_KEY_NODE Node
,
663 IN PCM_KEY_CONTROL_BLOCK Parent
,
665 IN PUNICODE_STRING KeyName
)
667 PCM_KEY_CONTROL_BLOCK Kcb
, FoundKcb
= NULL
;
668 UNICODE_STRING NodeName
;
669 ULONG ConvKey
= 0, i
;
670 BOOLEAN IsFake
, HashLock
;
673 /* Make sure we own this hive in case it's being unloaded */
674 if ((Hive
->HiveFlags
& HIVE_IS_UNLOADING
) &&
675 (((PCMHIVE
)Hive
)->CreatorOwner
!= KeGetCurrentThread()))
681 /* Check if this is a fake KCB */
682 IsFake
= Flags
& CMP_CREATE_FAKE_KCB
? TRUE
: FALSE
;
684 /* If we have a parent, use its ConvKey */
685 if (Parent
) ConvKey
= Parent
->ConvKey
;
687 /* Make a copy of the name */
690 /* Remove leading slash */
691 while ((NodeName
.Length
) && (*NodeName
.Buffer
== OBJ_NAME_PATH_SEPARATOR
))
693 /* Move the buffer by one */
695 NodeName
.Length
-= sizeof(WCHAR
);
698 /* Make sure we didn't get just a slash or something */
699 ASSERT(NodeName
.Length
> 0);
701 /* Now setup the hash */
703 for (i
= 0; i
< NodeName
.Length
; i
+= sizeof(WCHAR
))
705 /* Make sure it's a valid character */
706 if (*p
!= OBJ_NAME_PATH_SEPARATOR
)
708 /* Add this key to the hash */
709 ConvKey
= 37 * ConvKey
+ RtlUpcaseUnicodeChar(*p
);
716 /* Allocate the KCB */
717 Kcb
= CmpAllocateKeyControlBlock();
718 if (!Kcb
) return NULL
;
720 /* Initailize the key list */
721 InitializeKCBKeyBodyList(Kcb
);
724 Kcb
->Signature
= CM_KCB_SIGNATURE
;
728 Kcb
->KeyCell
= Index
;
729 Kcb
->ConvKey
= ConvKey
;
730 Kcb
->DelayedCloseIndex
= CmpDelayedCloseSize
;
731 Kcb
->InDelayClose
= 0;
732 ASSERT_KCB_VALID(Kcb
);
734 /* Check if we have two hash entires */
735 HashLock
= Flags
& CMP_LOCK_HASHES_FOR_KCB
? TRUE
: FALSE
;
738 /* It's not locked, do we have a parent? */
741 /* Lock the parent KCB and ourselves */
742 CmpAcquireTwoKcbLocksExclusiveByKey(ConvKey
, Parent
->ConvKey
);
746 /* Lock only ourselves */
747 CmpAcquireKcbLockExclusive(Kcb
);
751 /* Check if we already have a KCB */
752 FoundKcb
= CmpInsertKeyHash(&Kcb
->KeyHash
, IsFake
);
756 ASSERT(!FoundKcb
->Delete
);
757 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
759 /* Free the one we allocated and reference this one */
760 CmpFreeKeyControlBlock(Kcb
);
761 ASSERT_KCB_VALID(FoundKcb
);
763 if (!CmpReferenceKeyControlBlock(Kcb
))
765 /* We got too many handles */
766 ASSERT(Kcb
->RefCount
+ 1 != 0);
771 /* Check if we're not creating a fake one, but it used to be fake */
772 if ((Kcb
->ExtFlags
& CM_KCB_KEY_NON_EXIST
) && !(IsFake
))
774 /* Set the hive and cell */
776 Kcb
->KeyCell
= Index
;
778 /* This means that our current information is invalid */
779 Kcb
->ExtFlags
= CM_KCB_INVALID_CACHED_INFO
;
782 /* Check if we didn't have any valid data */
783 if (!(Kcb
->ExtFlags
& (CM_KCB_NO_SUBKEY
|
785 CM_KCB_SUBKEY_HINT
)))
787 /* Calculate the index hint */
788 Kcb
->SubKeyCount
= Node
->SubKeyCounts
[Stable
] +
789 Node
->SubKeyCounts
[Volatile
];
791 /* Cached information is now valid */
792 Kcb
->ExtFlags
&= ~CM_KCB_INVALID_CACHED_INFO
;
795 /* Setup the other data */
796 Kcb
->KcbLastWriteTime
= Node
->LastWriteTime
;
797 Kcb
->KcbMaxNameLen
= (USHORT
)Node
->MaxNameLen
;
798 Kcb
->KcbMaxValueNameLen
= (USHORT
)Node
->MaxValueNameLen
;
799 Kcb
->KcbMaxValueDataLen
= Node
->MaxValueDataLen
;
804 /* No KCB, do we have a parent? */
807 /* Reference the parent */
808 if (((Parent
->TotalLevels
+ 1) < 512) &&
809 (CmpReferenceKeyControlBlock(Parent
)))
812 Kcb
->ParentKcb
= Parent
;
813 Kcb
->TotalLevels
= Parent
->TotalLevels
+ 1;
817 /* Remove the KCB and free it */
818 CmpRemoveKeyControlBlock(Kcb
);
819 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
820 CmpFreeKeyControlBlock(Kcb
);
826 /* No parent, this is the root node */
827 Kcb
->ParentKcb
= NULL
;
828 Kcb
->TotalLevels
= 1;
831 /* Check if we have a KCB */
835 Kcb
->NameBlock
= CmpGetNameControlBlock(&NodeName
);
839 Kcb
->ValueCache
.Count
= Node
->ValueList
.Count
;
840 Kcb
->ValueCache
.ValueList
= Node
->ValueList
.List
;
841 Kcb
->Flags
= Node
->Flags
;
843 Kcb
->DelayedCloseIndex
= CmpDelayedCloseSize
;
845 /* Remember if this is a fake key */
846 if (IsFake
) Kcb
->ExtFlags
|= CM_KCB_KEY_NON_EXIST
;
848 /* Setup the other data */
849 Kcb
->SubKeyCount
= Node
->SubKeyCounts
[Stable
] +
850 Node
->SubKeyCounts
[Volatile
];
851 Kcb
->KcbLastWriteTime
= Node
->LastWriteTime
;
852 Kcb
->KcbMaxNameLen
= (USHORT
)Node
->MaxNameLen
;
853 Kcb
->KcbMaxValueNameLen
= (USHORT
)Node
->MaxValueNameLen
;
854 Kcb
->KcbMaxValueDataLen
= (USHORT
)Node
->MaxValueDataLen
;
858 /* Dereference the KCB */
859 CmpDereferenceKeyControlBlockWithLock(Parent
, FALSE
);
861 /* Remove the KCB and free it */
862 CmpRemoveKeyControlBlock(Kcb
);
863 Kcb
->Signature
= CM_KCB_INVALID_SIGNATURE
;
864 CmpFreeKeyControlBlock(Kcb
);
870 /* Check if this is a KCB inside a frozen hive */
871 if ((Kcb
) && (((PCMHIVE
)Hive
)->Frozen
) && (!(Kcb
->Flags
& KEY_SYM_LINK
)))
873 /* Don't add these to the delay close */
874 Kcb
->ExtFlags
|= CM_KCB_NO_DELAY_CLOSE
;
878 ASSERT((!Kcb
) || (Kcb
->Delete
== FALSE
));
880 /* Check if we had locked the hashes */
883 /* We locked them manually, do we have a parent? */
886 /* Unlock the parent KCB and ourselves */
887 CmpReleaseTwoKcbLockByKey(ConvKey
, Parent
->ConvKey
);
891 /* Unlock only ourselves */
892 CmpReleaseKcbLockByKey(ConvKey
);
902 CmpConstructName(IN PCM_KEY_CONTROL_BLOCK Kcb
)
904 PUNICODE_STRING KeyName
;
907 PCM_KEY_CONTROL_BLOCK MyKcb
;
908 PCM_KEY_NODE KeyNode
;
909 BOOLEAN DeletedKey
= FALSE
;
910 PWCHAR TargetBuffer
, CurrentNameW
;
913 /* Calculate how much size our key name is going to occupy */
919 /* Add length of the name */
920 if (!MyKcb
->NameBlock
->Compressed
)
922 NameLength
+= MyKcb
->NameBlock
->NameLength
;
926 NameLength
+= CmpCompressedNameSize(MyKcb
->NameBlock
->Name
,
927 MyKcb
->NameBlock
->NameLength
);
930 /* Sum up the separator too */
931 NameLength
+= sizeof(WCHAR
);
933 /* Go to the parent KCB */
934 MyKcb
= MyKcb
->ParentKcb
;
937 /* Allocate the unicode string now */
938 KeyName
= CmpAllocate(NameLength
+ sizeof(UNICODE_STRING
),
942 if (!KeyName
) return NULL
;
945 KeyName
->Buffer
= (PWSTR
)(KeyName
+ 1);
946 KeyName
->Length
= NameLength
;
947 KeyName
->MaximumLength
= NameLength
;
949 /* Loop the keys again, now adding names */
955 /* Sanity checks for deleted and fake keys */
956 if ((!MyKcb
->KeyCell
&& !MyKcb
->Delete
) ||
958 MyKcb
->ExtFlags
& CM_KCB_KEY_NON_EXIST
)
965 /* Try to get the name from the keynode,
966 if the key is not deleted */
967 if (!DeletedKey
&& !MyKcb
->Delete
)
969 KeyNode
= HvGetCell(MyKcb
->KeyHive
, MyKcb
->KeyCell
);
980 /* The key was deleted */
985 /* Get the pointer to the beginning of the current key name */
986 NameLength
+= (MyKcb
->NameBlock
->NameLength
+ 1) * sizeof(WCHAR
);
987 TargetBuffer
= &KeyName
->Buffer
[(KeyName
->Length
- NameLength
) / sizeof(WCHAR
)];
989 /* Add a separator */
990 TargetBuffer
[0] = OBJ_NAME_PATH_SEPARATOR
;
992 /* Add the name, but remember to go from the end to the beginning */
993 if (!MyKcb
->NameBlock
->Compressed
)
995 /* Get the pointer to the name (from the keynode, if possible) */
996 if ((MyKcb
->Flags
& (KEY_HIVE_ENTRY
| KEY_HIVE_EXIT
)) ||
999 CurrentNameW
= MyKcb
->NameBlock
->Name
;
1003 CurrentNameW
= KeyNode
->Name
;
1007 for (i
=0; i
< MyKcb
->NameBlock
->NameLength
; i
++)
1009 TargetBuffer
[i
+1] = *CurrentNameW
;
1015 /* Get the pointer to the name (from the keynode, if possible) */
1016 if ((MyKcb
->Flags
& (KEY_HIVE_ENTRY
| KEY_HIVE_EXIT
)) ||
1019 CurrentName
= (PUCHAR
)MyKcb
->NameBlock
->Name
;
1023 CurrentName
= (PUCHAR
)KeyNode
->Name
;
1027 for (i
=0; i
< MyKcb
->NameBlock
->NameLength
; i
++)
1029 TargetBuffer
[i
+1] = (WCHAR
)*CurrentName
;
1034 /* Release the cell, if needed */
1035 if (KeyNode
) HvReleaseCell(MyKcb
->KeyHive
, MyKcb
->KeyCell
);
1037 /* Go to the parent KCB */
1038 MyKcb
= MyKcb
->ParentKcb
;
1041 /* Return resulting buffer (both UNICODE_STRING and
1042 its buffer following it) */
1048 EnlistKeyBodyWithKCB(IN PCM_KEY_BODY KeyBody
,
1054 ASSERT(KeyBody
->KeyControlBlock
!= NULL
);
1056 /* Initialize the list entry */
1057 InitializeListHead(&KeyBody
->KeyBodyList
);
1059 /* Check if we can use the parent KCB array */
1060 for (i
= 0; i
< 4; i
++)
1062 /* Add it into the list */
1063 if (!InterlockedCompareExchangePointer((PVOID
*)&KeyBody
->KeyControlBlock
->
1073 /* Array full, check if we need to unlock the KCB */
1074 if (Flags
& CMP_ENLIST_KCB_LOCKED_SHARED
)
1076 /* It's shared, so release the KCB shared lock */
1077 CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1078 ASSERT(!(Flags
& CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
));
1081 /* Check if we need to lock the KCB */
1082 if (!(Flags
& CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
))
1084 /* Acquire the lock */
1085 CmpAcquireKcbLockExclusive(KeyBody
->KeyControlBlock
);
1088 /* Make sure we have the exclusive lock */
1089 ASSERT((CmpIsKcbLockedExclusive(KeyBody
->KeyControlBlock
) == TRUE
) ||
1090 (CmpTestRegistryLockExclusive() == TRUE
));
1093 InsertTailList(&KeyBody
->KeyControlBlock
->KeyBodyListHead
,
1094 &KeyBody
->KeyBodyList
);
1096 /* Check if we did a manual lock */
1097 if (!(Flags
& (CMP_ENLIST_KCB_LOCKED_SHARED
|
1098 CMP_ENLIST_KCB_LOCKED_EXCLUSIVE
)))
1100 /* Release the lock */
1101 CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1107 DelistKeyBodyFromKCB(IN PCM_KEY_BODY KeyBody
,
1108 IN BOOLEAN LockHeld
)
1113 ASSERT(KeyBody
->KeyControlBlock
!= NULL
);
1115 /* Check if we can use the parent KCB array */
1116 for (i
= 0; i
< 4; i
++)
1118 /* Add it into the list */
1119 if (InterlockedCompareExchangePointer((VOID
*)&KeyBody
->KeyControlBlock
->
1122 KeyBody
) == KeyBody
)
1130 ASSERT(IsListEmpty(&KeyBody
->KeyControlBlock
->KeyBodyListHead
) == FALSE
);
1131 ASSERT(IsListEmpty(&KeyBody
->KeyBodyList
) == FALSE
);
1134 if (!LockHeld
) CmpAcquireKcbLockExclusive(KeyBody
->KeyControlBlock
);
1135 ASSERT((CmpIsKcbLockedExclusive(KeyBody
->KeyControlBlock
) == TRUE
) ||
1136 (CmpTestRegistryLockExclusive() == TRUE
));
1138 /* Remove the entry */
1139 RemoveEntryList(&KeyBody
->KeyBodyList
);
1141 /* Unlock it it if we did a manual lock */
1142 if (!LockHeld
) CmpReleaseKcbLock(KeyBody
->KeyControlBlock
);
1147 CmpFlushNotifiesOnKeyBodyList(IN PCM_KEY_CONTROL_BLOCK Kcb
,
1148 IN BOOLEAN LockHeld
)
1150 PLIST_ENTRY NextEntry
, ListHead
;
1151 PCM_KEY_BODY KeyBody
;
1154 LockHeld
? CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK() : CmpIsKcbLockedExclusive(Kcb
);
1157 /* Is the list empty? */
1158 ListHead
= &Kcb
->KeyBodyListHead
;
1159 if (!IsListEmpty(ListHead
))
1162 NextEntry
= ListHead
->Flink
;
1163 while (NextEntry
!= ListHead
)
1165 /* Get the key body */
1166 KeyBody
= CONTAINING_RECORD(NextEntry
, CM_KEY_BODY
, KeyBodyList
);
1167 ASSERT(KeyBody
->Type
== '20yk');
1169 /* Check for notifications */
1170 if (KeyBody
->NotifyBlock
)
1172 /* Is the lock held? */
1176 CmpFlushNotify(KeyBody
, LockHeld
);
1177 ASSERT(KeyBody
->NotifyBlock
== NULL
);
1181 /* Lock isn't held, so we need to take a reference */
1182 if (ObReferenceObjectSafe(KeyBody
))
1184 /* Now we can flush */
1185 CmpFlushNotify(KeyBody
, LockHeld
);
1186 ASSERT(KeyBody
->NotifyBlock
== NULL
);
1188 /* Release the reference we took */
1189 ObDereferenceObjectDeferDelete(KeyBody
);
1194 /* Try the next entry */
1195 NextEntry
= NextEntry
->Flink
;
1199 /* List has been parsed, exit */