Use memory wrappers instead of ExAllocatePool/ExFreePool directly
[reactos.git] / reactos / ntoskrnl / config / cmvalche.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/config/cmvalche.c
5 * PURPOSE: Configuration Manager - Value Cell Cache
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "ntoskrnl.h"
12 #define NDEBUG
13 #include "debug.h"
14
15 FORCEINLINE
16 BOOLEAN
17 CmpIsValueCached(IN HCELL_INDEX CellIndex)
18 {
19 /* Make sure that the cell is valid in the first place */
20 if (CellIndex == HCELL_NIL) return FALSE;
21
22 /*Is this cell actually a pointer to the cached value data? */
23 if (CellIndex & 1) return TRUE;
24
25 /* This is a regular cell */
26 return FALSE;
27 }
28
29 FORCEINLINE
30 VOID
31 CmpSetValueCached(IN PHCELL_INDEX CellIndex)
32 {
33 /* Set the cached bit */
34 *CellIndex |= 1;
35 }
36
37 #define ASSERT_VALUE_CACHE() \
38 ASSERTMSG("Cached Values Not Yet Supported!", FALSE);
39
40 /* FUNCTIONS *****************************************************************/
41
42 VALUE_SEARCH_RETURN_TYPE
43 NTAPI
44 CmpGetValueListFromCache(IN PCM_KEY_CONTROL_BLOCK Kcb,
45 OUT PCELL_DATA *CellData,
46 OUT BOOLEAN *IndexIsCached,
47 OUT PHCELL_INDEX ValueListToRelease)
48 {
49 PHHIVE Hive;
50 PCACHED_CHILD_LIST ChildList;
51 HCELL_INDEX CellToRelease;
52 PCM_KEY_NODE KeyNode;
53
54 /* Set defaults */
55 *ValueListToRelease = HCELL_NIL;
56 *IndexIsCached = FALSE;
57
58 /* Get the hive and value cache */
59 Hive = Kcb->KeyHive;
60 ChildList = &Kcb->ValueCache;
61 KeyNode = (PCM_KEY_NODE)HvGetCell(Hive, Kcb->KeyCell);
62 ChildList = (PCACHED_CHILD_LIST)&KeyNode->ValueList;
63
64 /* Check if the value is cached */
65 if (CmpIsValueCached(ChildList->ValueList))
66 {
67 /* It is: we don't expect this yet! */
68 ASSERT_VALUE_CACHE();
69 *IndexIsCached = TRUE;
70 *CellData = NULL;
71 }
72 else
73 {
74 /* Make sure the KCB is locked exclusive */
75 if (!(CmpIsKcbLockedExclusive(Kcb)) &&
76 !(CmpTryToConvertKcbSharedToExclusive(Kcb)))
77 {
78 /* We need the exclusive lock */
79 return SearchNeedExclusiveLock;
80 }
81
82 /* Select the value list as our cell, and get the actual list array */
83 CellToRelease = ChildList->ValueList;
84 *CellData = (PCELL_DATA)HvGetCell(Hive, CellToRelease);
85 if (!(*CellData)) return SearchFail;
86
87 /* FIXME: Here we would cache the value */
88
89 /* Return the cell to be released */
90 *ValueListToRelease = CellToRelease;
91 }
92
93 /* If we got here, then the value list was found */
94 return SearchSuccess;
95 }
96
97 VALUE_SEARCH_RETURN_TYPE
98 NTAPI
99 CmpGetValueKeyFromCache(IN PCM_KEY_CONTROL_BLOCK Kcb,
100 IN PCELL_DATA CellData,
101 IN ULONG Index,
102 OUT PCM_CACHED_VALUE **CachedValue,
103 OUT PCM_KEY_VALUE *Value,
104 IN BOOLEAN IndexIsCached,
105 OUT BOOLEAN *ValueIsCached,
106 OUT PHCELL_INDEX CellToRelease)
107 {
108 PHHIVE Hive;
109 PCM_KEY_VALUE KeyValue;
110 HCELL_INDEX Cell;
111
112 /* Set defaults */
113 *CellToRelease = HCELL_NIL;
114 *Value = NULL;
115 *ValueIsCached = FALSE;
116
117 /* Get the hive */
118 Hive = Kcb->KeyHive;
119
120 /* Check if the index was cached */
121 if (IndexIsCached)
122 {
123 /* Not expected yet! */
124 ASSERT_VALUE_CACHE();
125 *ValueIsCached = TRUE;
126 }
127 else
128 {
129 /* Get the cell index and the key value associated to it */
130 Cell = CellData->u.KeyList[Index];
131 KeyValue = (PCM_KEY_VALUE)HvGetCell(Hive, Cell);
132 if (!KeyValue) return SearchFail;
133
134 /* Return the cell and the actual key value */
135 *CellToRelease = Cell;
136 *Value = KeyValue;
137 }
138
139 /* If we got here, then we found the key value */
140 return SearchSuccess;
141 }
142
143 VALUE_SEARCH_RETURN_TYPE
144 NTAPI
145 CmpGetValueDataFromCache(IN PCM_KEY_CONTROL_BLOCK Kcb,
146 IN PCM_CACHED_VALUE *CachedValue,
147 IN PCELL_DATA ValueKey,
148 IN BOOLEAN ValueIsCached,
149 OUT PVOID *DataPointer,
150 OUT PBOOLEAN Allocated,
151 OUT PHCELL_INDEX CellToRelease)
152 {
153 PHHIVE Hive;
154 ULONG Length;
155
156 /* Sanity checks */
157 ASSERT(MAXIMUM_CACHED_DATA < CM_KEY_VALUE_BIG);
158 ASSERT((ValueKey->u.KeyValue.DataLength & CM_KEY_VALUE_SPECIAL_SIZE) == 0);
159
160 /* Set defaults */
161 *DataPointer = NULL;
162 *Allocated = FALSE;
163 *CellToRelease = HCELL_NIL;
164
165 /* Get the hive */
166 Hive = Kcb->KeyHive;
167
168 /* Check it the value is cached */
169 if (ValueIsCached)
170 {
171 /* This isn't expected! */
172 ASSERT_VALUE_CACHE();
173 }
174 else
175 {
176 /* It's not, get the value data using the typical routine */
177 if (!CmpGetValueData(Hive,
178 &ValueKey->u.KeyValue,
179 &Length,
180 DataPointer,
181 Allocated,
182 CellToRelease))
183 {
184 /* Nothing found: make sure no data was allocated */
185 ASSERT(*Allocated == FALSE);
186 ASSERT(*DataPointer == NULL);
187 return SearchFail;
188 }
189 }
190
191 /* We found the actual data, return success */
192 return SearchSuccess;
193 }
194
195 VALUE_SEARCH_RETURN_TYPE
196 NTAPI
197 CmpFindValueByNameFromCache(IN PCM_KEY_CONTROL_BLOCK Kcb,
198 IN PCUNICODE_STRING Name,
199 OUT PCM_CACHED_VALUE **CachedValue,
200 OUT ULONG *Index,
201 OUT PCM_KEY_VALUE *Value,
202 OUT BOOLEAN *ValueIsCached,
203 OUT PHCELL_INDEX CellToRelease)
204 {
205 PHHIVE Hive;
206 VALUE_SEARCH_RETURN_TYPE SearchResult = SearchFail;
207 LONG Result;
208 UNICODE_STRING SearchName;
209 PCELL_DATA CellData;
210 PCACHED_CHILD_LIST ChildList;
211 PCM_KEY_VALUE KeyValue;
212 BOOLEAN IndexIsCached;
213 ULONG i = 0;
214 HCELL_INDEX Cell = HCELL_NIL;
215 PCM_KEY_NODE KeyNode;
216
217 /* Set defaults */
218 *CellToRelease = HCELL_NIL;
219 *Value = NULL;
220
221 /* Get the hive and child list */
222 Hive = Kcb->KeyHive;
223 ChildList = &Kcb->ValueCache;
224 KeyNode = (PCM_KEY_NODE)HvGetCell(Hive, Kcb->KeyCell);
225 ChildList = (PCACHED_CHILD_LIST)&KeyNode->ValueList;
226
227 /* Check if the child list has any entries */
228 if (ChildList->Count != 0)
229 {
230 /* Get the value list associated to this child list */
231 SearchResult = CmpGetValueListFromCache(Kcb,
232 &CellData,
233 &IndexIsCached,
234 &Cell);
235 if (SearchResult != SearchSuccess)
236 {
237 /* We either failed or need the exclusive lock */
238 ASSERT((SearchResult == SearchFail) || !(CmpIsKcbLockedExclusive(Kcb)));
239 ASSERT(Cell == HCELL_NIL);
240 return SearchResult;
241 }
242
243 /* The index shouldn't be cached right now */
244 if (IndexIsCached) ASSERT_VALUE_CACHE();
245
246 /* Loop every value */
247 while (TRUE)
248 {
249 /* Check if there's any cell to release */
250 if (*CellToRelease != HCELL_NIL)
251 {
252 /* Release it now */
253 HvReleaseCell(Hive, *CellToRelease);
254 *CellToRelease = HCELL_NIL;
255 }
256
257 /* Get the key value for this index */
258 SearchResult = CmpGetValueKeyFromCache(Kcb,
259 CellData,
260 i,
261 CachedValue,
262 Value,
263 IndexIsCached,
264 ValueIsCached,
265 CellToRelease);
266 if (SearchResult != SearchSuccess)
267 {
268 /* We either failed or need the exclusive lock */
269 ASSERT((SearchResult == SearchFail) || !(CmpIsKcbLockedExclusive(Kcb)));
270 ASSERT(Cell == HCELL_NIL);
271 return SearchResult;
272 }
273
274 /* Check if the both the index and the value are cached */
275 if ((IndexIsCached) && (*ValueIsCached))
276 {
277 /* We don't expect this yet */
278 ASSERT_VALUE_CACHE();
279 Result = -1;
280 }
281 else
282 {
283 /* No cache, so try to compare the name. Is it compressed? */
284 KeyValue = *Value;
285 if (KeyValue->Flags & VALUE_COMP_NAME)
286 {
287 /* It is, do a compressed name comparison */
288 Result = CmpCompareCompressedName(Name,
289 KeyValue->Name,
290 KeyValue->NameLength);
291 }
292 else
293 {
294 /* It's not compressed, so do a standard comparison */
295 SearchName.Length = KeyValue->NameLength;
296 SearchName.MaximumLength = SearchName.Length;
297 SearchName.Buffer = KeyValue->Name;
298 Result = RtlCompareUnicodeString(Name, &SearchName, TRUE);
299 }
300 }
301
302 /* Check if we found the value data */
303 if (!Result)
304 {
305 /* We have, return the index of the value and success */
306 *Index = i;
307 SearchResult = SearchSuccess;
308 goto Quickie;
309 }
310
311 /* We didn't find it, try the next entry */
312 if (++i == ChildList->Count)
313 {
314 /* The entire list was parsed, fail */
315 *Value = NULL;
316 SearchResult = SearchFail;
317 goto Quickie;
318 }
319 }
320 }
321
322 /* We should only get here if the child list is empty */
323 ASSERT(ChildList->Count == 0);
324
325 Quickie:
326 /* Release the value list cell if required, and return search result */
327 if (Cell != HCELL_NIL) HvReleaseCell(Hive, Cell);
328 return SearchResult;
329 }
330
331 VALUE_SEARCH_RETURN_TYPE
332 NTAPI
333 CmpQueryKeyValueData(IN PCM_KEY_CONTROL_BLOCK Kcb,
334 IN PCM_CACHED_VALUE *CachedValue,
335 IN PCM_KEY_VALUE ValueKey,
336 IN BOOLEAN ValueIsCached,
337 IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,
338 IN PVOID KeyValueInformation,
339 IN ULONG Length,
340 OUT PULONG ResultLength,
341 OUT PNTSTATUS Status)
342 {
343 PHHIVE Hive;
344 PKEY_VALUE_INFORMATION Info = (PKEY_VALUE_INFORMATION)KeyValueInformation;
345 PCELL_DATA CellData;
346 USHORT NameSize;
347 ULONG Size, MinimumSize, SizeLeft, KeySize, AlignedData = 0, DataOffset;
348 PVOID Buffer;
349 BOOLEAN IsSmall, BufferAllocated = FALSE;
350 HCELL_INDEX CellToRelease = HCELL_NIL;
351 VALUE_SEARCH_RETURN_TYPE Result = SearchSuccess;
352
353 /* Get the hive and cell data */
354 Hive = Kcb->KeyHive;
355 CellData = (PCELL_DATA)ValueKey;
356
357 /* Check if the value is compressed */
358 if (CellData->u.KeyValue.Flags & VALUE_COMP_NAME)
359 {
360 /* Get the compressed name size */
361 NameSize = CmpCompressedNameSize(CellData->u.KeyValue.Name,
362 CellData->u.KeyValue.NameLength);
363 }
364 else
365 {
366 /* Get the real size */
367 NameSize = CellData->u.KeyValue.NameLength;
368 }
369
370 /* Check what kind of information the caller is requesting */
371 switch (KeyValueInformationClass)
372 {
373 /* Basic information */
374 case KeyValueBasicInformation:
375
376 /* This is how much size we'll need */
377 Size = FIELD_OFFSET(KEY_VALUE_BASIC_INFORMATION, Name) + NameSize;
378
379 /* This is the minimum we can work with */
380 MinimumSize = FIELD_OFFSET(KEY_VALUE_BASIC_INFORMATION, Name);
381
382 /* Return the size we'd like, and assume success */
383 *ResultLength = Size;
384 *Status = STATUS_SUCCESS;
385
386 /* Check if the caller gave us below our minimum */
387 if (Length < MinimumSize)
388 {
389 /* Then we must fail */
390 *Status = STATUS_BUFFER_TOO_SMALL;
391 break;
392 }
393
394 /* Fill out the basic information */
395 Info->KeyValueBasicInformation.TitleIndex = 0;
396 Info->KeyValueBasicInformation.Type = CellData->u.KeyValue.Type;
397 Info->KeyValueBasicInformation.NameLength = NameSize;
398
399 /* Now only the name is left */
400 SizeLeft = Length - MinimumSize;
401 Size = NameSize;
402
403 /* Check if the remaining buffer is too small for the name */
404 if (SizeLeft < Size)
405 {
406 /* Copy only as much as can fit, and tell the caller */
407 Size = SizeLeft;
408 *Status = STATUS_BUFFER_OVERFLOW;
409 }
410
411 /* Check if this is a compressed name */
412 if (CellData->u.KeyValue.Flags & VALUE_COMP_NAME)
413 {
414 /* Copy as much as we can of the compressed name */
415 CmpCopyCompressedName(Info->KeyValueBasicInformation.Name,
416 Size,
417 CellData->u.KeyValue.Name,
418 CellData->u.KeyValue.NameLength);
419 }
420 else
421 {
422 /* Copy as much as we can of the raw name */
423 RtlCopyMemory(Info->KeyValueBasicInformation.Name,
424 CellData->u.KeyValue.Name,
425 Size);
426 }
427
428 /* We're all done */
429 break;
430
431 /* Full key information */
432 case KeyValueFullInformation:
433
434 /* Check if this is a small key and compute key size */
435 IsSmall = CmpIsKeyValueSmall(&KeySize,
436 CellData->u.KeyValue.DataLength);
437
438 /* Calculate the total size required */
439 Size = FIELD_OFFSET(KEY_VALUE_FULL_INFORMATION, Name) +
440 NameSize +
441 KeySize;
442
443 /* And this is the least we can work with */
444 MinimumSize = FIELD_OFFSET(KEY_VALUE_FULL_INFORMATION, Name);
445
446 /* Check if there's any key data */
447 if (KeySize > 0)
448 {
449 /* Calculate the data offset */
450 DataOffset = Size - KeySize;
451
452 /* Align the offset to 4 bytes */
453 AlignedData = ALIGN_UP(DataOffset, ULONG);
454
455 /* If alignment was required, we'll need more space */
456 if (AlignedData > DataOffset) Size += (AlignedData-DataOffset);
457 }
458
459 /* Tell the caller the size we'll finally need, and set success */
460 *ResultLength = Size;
461 *Status = STATUS_SUCCESS;
462
463 /* Check if the caller is giving us too little */
464 if (Length < MinimumSize)
465 {
466 /* Then fail right now */
467 *Status = STATUS_BUFFER_TOO_SMALL;
468 break;
469 }
470
471 /* Fill out the basic information */
472 Info->KeyValueFullInformation.TitleIndex = 0;
473 Info->KeyValueFullInformation.Type = CellData->u.KeyValue.Type;
474 Info->KeyValueFullInformation.DataLength = KeySize;
475 Info->KeyValueFullInformation.NameLength = NameSize;
476
477 /* Only the name is left now */
478 SizeLeft = Length - MinimumSize;
479 Size = NameSize;
480
481 /* Check if the name fits */
482 if (SizeLeft < Size)
483 {
484 /* It doesn't, truncate what we'll copy, and tell the caller */
485 Size = SizeLeft;
486 *Status = STATUS_BUFFER_OVERFLOW;
487 }
488
489 /* Check if this key value is compressed */
490 if (CellData->u.KeyValue.Flags & VALUE_COMP_NAME)
491 {
492 /* It is, copy the compressed name */
493 CmpCopyCompressedName(Info->KeyValueFullInformation.Name,
494 Size,
495 CellData->u.KeyValue.Name,
496 CellData->u.KeyValue.NameLength);
497 }
498 else
499 {
500 /* It's not, copy the raw name */
501 RtlCopyMemory(Info->KeyValueFullInformation.Name,
502 CellData->u.KeyValue.Name,
503 Size);
504 }
505
506 /* Now check if the key had any data */
507 if (KeySize > 0)
508 {
509 /* Was it a small key? */
510 if (IsSmall)
511 {
512 /* Then the data is directly into the cell */
513 Buffer = &CellData->u.KeyValue.Data;
514 }
515 else
516 {
517 /* Otherwise, we must retrieve it from the value cache */
518 Result = CmpGetValueDataFromCache(Kcb,
519 CachedValue,
520 CellData,
521 ValueIsCached,
522 &Buffer,
523 &BufferAllocated,
524 &CellToRelease);
525 if (Result != SearchSuccess)
526 {
527 /* We failed, nothing should be allocated */
528 ASSERT(Buffer == NULL);
529 ASSERT(BufferAllocated == FALSE);
530 *Status = STATUS_INSUFFICIENT_RESOURCES;
531 }
532 }
533
534 /* Now that we know we truly have data, set its offset */
535 Info->KeyValueFullInformation.DataOffset = AlignedData;
536
537 /* Only the data remains to be copied */
538 SizeLeft = (((LONG)Length - (LONG)AlignedData) < 0) ?
539 0 : (Length - AlignedData);
540 Size = KeySize;
541
542 /* Check if the caller has no space for it */
543 if (SizeLeft < Size)
544 {
545 /* Truncate what we'll copy, and tell the caller */
546 Size = SizeLeft;
547 *Status = STATUS_BUFFER_OVERFLOW;
548 }
549
550 /* Sanity check */
551 ASSERT((IsSmall ? (Size <= CM_KEY_VALUE_SMALL) : TRUE));
552
553 /* Make sure we have a valid buffer */
554 if (Buffer)
555 {
556 /* Copy the data into the aligned offset */
557 RtlCopyMemory((PVOID)((ULONG_PTR)Info + AlignedData),
558 Buffer,
559 Size);
560 }
561 }
562 else
563 {
564 /* We don't have any data, set the offset to -1, not 0! */
565 Info->KeyValueFullInformation.DataOffset = 0xFFFFFFFF;
566 }
567
568 /* We're done! */
569 break;
570
571 /* Partial information requested (no name or alignment!) */
572 case KeyValuePartialInformation:
573
574 /* Check if this is a small key and compute key size */
575 IsSmall = CmpIsKeyValueSmall(&KeySize,
576 CellData->u.KeyValue.DataLength);
577
578 /* Calculate the total size required */
579 Size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data) + KeySize;
580
581 /* And this is the least we can work with */
582 MinimumSize = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
583
584 /* Tell the caller the size we'll finally need, and set success */
585 *ResultLength = Size;
586 *Status = STATUS_SUCCESS;
587
588 /* Check if the caller is giving us too little */
589 if (Length < MinimumSize)
590 {
591 /* Then fail right now */
592 *Status = STATUS_BUFFER_TOO_SMALL;
593 break;
594 }
595
596 /* Fill out the basic information */
597 Info->KeyValuePartialInformation.TitleIndex = 0;
598 Info->KeyValuePartialInformation.Type = CellData->u.KeyValue.Type;
599 Info->KeyValuePartialInformation.DataLength = KeySize;
600
601 /* Now check if the key had any data */
602 if (KeySize > 0)
603 {
604 /* Was it a small key? */
605 if (IsSmall)
606 {
607 /* Then the data is directly into the cell */
608 Buffer = &CellData->u.KeyValue.Data;
609 }
610 else
611 {
612 /* Otherwise, we must retrieve it from the value cache */
613 Result = CmpGetValueDataFromCache(Kcb,
614 CachedValue,
615 CellData,
616 ValueIsCached,
617 &Buffer,
618 &BufferAllocated,
619 &CellToRelease);
620 if (Result != SearchSuccess)
621 {
622 /* We failed, nothing should be allocated */
623 ASSERT(Buffer == NULL);
624 ASSERT(BufferAllocated == FALSE);
625 *Status = STATUS_INSUFFICIENT_RESOURCES;
626 }
627 }
628
629 /* Only the data remains to be copied */
630 SizeLeft = Length - MinimumSize;
631 Size = KeySize;
632
633 /* Check if the caller has no space for it */
634 if (SizeLeft < Size)
635 {
636 /* Truncate what we'll copy, and tell the caller */
637 Size = SizeLeft;
638 *Status = STATUS_BUFFER_OVERFLOW;
639 }
640
641 /* Sanity check */
642 ASSERT((IsSmall ? (Size <= CM_KEY_VALUE_SMALL) : TRUE));
643
644 /* Make sure we have a valid buffer */
645 if (Buffer)
646 {
647 /* Copy the data into the aligned offset */
648 RtlCopyMemory(Info->KeyValuePartialInformation.Data,
649 Buffer,
650 Size);
651 }
652 }
653
654 /* We're done! */
655 break;
656
657 /* Other information class */
658 default:
659
660 /* We got some class that we don't support */
661 *Status = STATUS_INVALID_PARAMETER;
662 break;
663 }
664
665 /* Return the search result as well */
666 return Result;
667 }
668
669 VALUE_SEARCH_RETURN_TYPE
670 NTAPI
671 CmpCompareNewValueDataAgainstKCBCache(IN PCM_KEY_CONTROL_BLOCK Kcb,
672 IN PUNICODE_STRING ValueName,
673 IN ULONG Type,
674 IN PVOID Data,
675 IN ULONG DataSize)
676 {
677 VALUE_SEARCH_RETURN_TYPE SearchResult;
678 PCM_KEY_NODE KeyNode;
679 PCM_CACHED_VALUE *CachedValue;
680 ULONG Index;
681 PCM_KEY_VALUE Value;
682 BOOLEAN ValueCached, BufferAllocated = FALSE;
683 PVOID Buffer;
684 HCELL_INDEX ValueCellToRelease = HCELL_NIL, CellToRelease = HCELL_NIL;
685 BOOLEAN IsSmall;
686 ULONG CompareResult;
687 PAGED_CODE();
688
689 /* Check if this is a symlink */
690 if (Kcb->Flags & KEY_SYM_LINK)
691 {
692 /* We need the exclusive lock */
693 if (!(CmpIsKcbLockedExclusive(Kcb)) &&
694 !(CmpTryToConvertKcbSharedToExclusive(Kcb)))
695 {
696 /* We need the exclusive lock */
697 return SearchNeedExclusiveLock;
698 }
699
700 /* Otherwise, get the key node */
701 KeyNode = (PCM_KEY_NODE)HvGetCell(Kcb->KeyHive, Kcb->KeyCell);
702 if (!KeyNode) return SearchFail;
703
704 /* Cleanup the KCB cache */
705 CmpCleanUpKcbValueCache(Kcb);
706
707 /* Sanity checks */
708 ASSERT(!(CMP_IS_CELL_CACHED(Kcb->ValueCache.ValueList)));
709 ASSERT(!(Kcb->ExtFlags & CM_KCB_SYM_LINK_FOUND));
710
711 /* Set the value cache */
712 Kcb->ValueCache.Count = KeyNode->ValueList.Count;
713 Kcb->ValueCache.ValueList = KeyNode->ValueList.List;
714
715 /* Release the cell */
716 HvReleaseCell(Kcb->KeyHive, Kcb->KeyCell);
717 }
718
719 /* Do the search */
720 SearchResult = CmpFindValueByNameFromCache(Kcb,
721 ValueName,
722 &CachedValue,
723 &Index,
724 &Value,
725 &ValueCached,
726 &ValueCellToRelease);
727 if (SearchResult == SearchNeedExclusiveLock)
728 {
729 /* We need the exclusive lock */
730 ASSERT(!CmpIsKcbLockedExclusive(Kcb));
731 ASSERT(ValueCellToRelease == HCELL_NIL);
732 ASSERT(Value == NULL);
733 goto Quickie;
734 }
735 else if (SearchResult == SearchSuccess)
736 {
737 /* Sanity check */
738 ASSERT(Value);
739
740 /* First of all, check if the key size and type matches */
741 if ((Type == Value->Type) &&
742 (DataSize == (Value->DataLength & ~CM_KEY_VALUE_SPECIAL_SIZE)))
743 {
744 /* Check if this is a small key */
745 IsSmall = (DataSize <= CM_KEY_VALUE_SMALL) ? TRUE: FALSE;
746 if (IsSmall)
747 {
748 /* Compare against the data directly */
749 Buffer = &Value->Data;
750 }
751 else
752 {
753 /* Do a search */
754 SearchResult = CmpGetValueDataFromCache(Kcb,
755 CachedValue,
756 (PCELL_DATA)Value,
757 ValueCached,
758 &Buffer,
759 &BufferAllocated,
760 &CellToRelease);
761 if (SearchResult != SearchSuccess)
762 {
763 /* Sanity checks */
764 ASSERT(Buffer == NULL);
765 ASSERT(BufferAllocated == FALSE);
766 goto Quickie;
767 }
768 }
769
770 /* Now check the data size */
771 if (DataSize)
772 {
773 /* Do the compare */
774 CompareResult = RtlCompareMemory(Buffer,
775 Data,
776 DataSize &
777 ~CM_KEY_VALUE_SPECIAL_SIZE);
778 }
779 else
780 {
781 /* It's equal */
782 CompareResult = 0;
783 }
784
785 /* Now check if the compare wasn't equal */
786 if (CompareResult != DataSize) SearchResult = SearchFail;
787 }
788 else
789 {
790 /* The length or type isn't equal */
791 SearchResult = SearchFail;
792 }
793 }
794
795 Quickie:
796 /* Release the value cell */
797 if (ValueCellToRelease) HvReleaseCell(Kcb->KeyHive, ValueCellToRelease);
798
799 /* Free the buffer */
800 if (BufferAllocated) CmpFree(Buffer, 0);
801
802 /* Free the cell */
803 if (CellToRelease) HvReleaseCell(Kcb->KeyHive, CellToRelease);
804
805 /* Return the search result */
806 return SearchResult;
807 }