Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1072 lines
26 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. PrefxSup.c
  5. Abstract:
  6. This module implements the Ntfs Prefix support routines
  7. Author:
  8. Gary Kimura [GaryKi] 21-May-1991
  9. Revision History:
  10. --*/
  11. #include "NtfsProc.h"
  12. //
  13. // The Bug check file id for this module
  14. //
  15. #define BugCheckFileId (NTFS_BUG_CHECK_PREFXSUP)
  16. //
  17. // The debug trace level for this module
  18. //
  19. #define Dbg (DEBUG_TRACE_PREFXSUP)
  20. //
  21. // Local procedures
  22. //
  23. #ifdef NTFS_CHECK_SPLAY
  24. VOID
  25. NtfsCheckSplay (
  26. IN PRTL_SPLAY_LINKS Root
  27. );
  28. #endif
  29. #ifdef ALLOC_PRAGMA
  30. #pragma alloc_text(PAGE, NtfsFindPrefix)
  31. #pragma alloc_text(PAGE, NtfsFindNameLink)
  32. #pragma alloc_text(PAGE, NtfsInsertNameLink)
  33. #pragma alloc_text(PAGE, NtfsInsertPrefix)
  34. #pragma alloc_text(PAGE, NtfsRemovePrefix)
  35. #ifdef NTFS_CHECK_SPLAY
  36. #pragma alloc_text(PAGE, NtfsCheckSplay)
  37. #endif
  38. #endif
  39. VOID
  40. NtfsInsertPrefix (
  41. IN PLCB Lcb,
  42. IN ULONG CreateFlags
  43. )
  44. /*++
  45. Routine Description:
  46. This routine inserts the names in the given Lcb into the links for the
  47. parent.
  48. Arguments:
  49. Lcb - This is the link to insert.
  50. CreateFlags - Indicates if we should insert the case-insensitive name.
  51. Return Value:
  52. None.
  53. --*/
  54. {
  55. PAGED_CODE();
  56. ASSERT( (Lcb->Scb == NULL) ||
  57. NtfsIsExclusiveScb( Lcb->Scb ) );
  58. //
  59. // Attempt to insert the case-insensitive name. It is possible that
  60. // we can't enter this name.
  61. //
  62. if (FlagOn( CreateFlags, CREATE_FLAG_IGNORE_CASE )) {
  63. if (!FlagOn( Lcb->LcbState, LCB_STATE_IGNORE_CASE_IN_TREE ) &&
  64. NtfsInsertNameLink( &Lcb->Scb->ScbType.Index.IgnoreCaseNode,
  65. &Lcb->IgnoreCaseLink )) {
  66. SetFlag( Lcb->LcbState, LCB_STATE_IGNORE_CASE_IN_TREE );
  67. }
  68. } else if (!FlagOn( Lcb->LcbState, LCB_STATE_EXACT_CASE_IN_TREE )) {
  69. if (!NtfsInsertNameLink( &Lcb->Scb->ScbType.Index.ExactCaseNode,
  70. &Lcb->ExactCaseLink )) {
  71. NtfsBugCheck( 0, 0, 0 );
  72. }
  73. SetFlag( Lcb->LcbState, LCB_STATE_EXACT_CASE_IN_TREE );
  74. }
  75. return;
  76. }
  77. VOID
  78. NtfsRemovePrefix (
  79. IN PLCB Lcb
  80. )
  81. /*++
  82. Routine Description:
  83. This routine deletes all of the Prefix entries that exist for the input
  84. Lcb.
  85. Arguments:
  86. Lcb - Supplies the Lcb whose prefixes are to be removed
  87. Return Value:
  88. None.
  89. --*/
  90. {
  91. PAGED_CODE();
  92. //
  93. // Remove the case-insensitive link.
  94. //
  95. if (FlagOn( Lcb->LcbState, LCB_STATE_IGNORE_CASE_IN_TREE )) {
  96. NtfsRemoveNameLink( &Lcb->Scb->ScbType.Index.IgnoreCaseNode,
  97. &Lcb->IgnoreCaseLink );
  98. ClearFlag( Lcb->LcbState, LCB_STATE_IGNORE_CASE_IN_TREE );
  99. }
  100. //
  101. // Now do the same for the exact case name.
  102. //
  103. if (FlagOn( Lcb->LcbState, LCB_STATE_EXACT_CASE_IN_TREE )) {
  104. NtfsRemoveNameLink( &Lcb->Scb->ScbType.Index.ExactCaseNode,
  105. &Lcb->ExactCaseLink );
  106. ClearFlag( Lcb->LcbState, LCB_STATE_EXACT_CASE_IN_TREE );
  107. }
  108. return;
  109. }
  110. PLCB
  111. NtfsFindPrefix (
  112. IN PIRP_CONTEXT IrpContext,
  113. IN PSCB StartingScb,
  114. OUT PFCB *CurrentFcb,
  115. OUT PLCB *LcbForTeardown,
  116. IN OUT UNICODE_STRING FullFileName,
  117. IN OUT PULONG CreateFlags,
  118. OUT PUNICODE_STRING RemainingName
  119. )
  120. /*++
  121. Routine Description:
  122. This routine begins from the given Scb and walks through all of
  123. components of the name looking for the longest match in the prefix
  124. splay trees. The search is relative to the starting Scb so the
  125. full name may not begin with a '\'. On return this routine will
  126. update Current Fcb with the lowest point it has travelled in the
  127. tree. It will also hold only that resource on return and it must
  128. hold that resource.
  129. Arguments:
  130. StartingScb - Supplies the Scb to start the search from.
  131. CurrentFcb - Address to store the lowest Fcb we find on this search.
  132. On return we will have acquired this Fcb.
  133. LcbForTeardown - If we encounter an Lcb we must teardown on error we
  134. store it here.
  135. FullFileName - Supplies the input string to search for. After the search the
  136. buffer for this string will be modified so that for the characters that did
  137. match will be the exact case of what we found.
  138. CreateFlags - Flags for the create option - we are interested in whether
  139. this is a case-insensitive compare and we also will set the dos only component flag
  140. RemainingName - Returns the string when the prefix no longer matches.
  141. For example, if the input string is "alpha\beta" only matches the
  142. root directory then the remaining string is "alpha\beta". If the
  143. same string matches an LCB for "alpha" then the remaining string is
  144. "beta".
  145. Return Value:
  146. PLCB - Returns a pointer to the Lcb corresponding to the longest match
  147. in the splay tree. NULL if we didn't even find one entry.
  148. --*/
  149. {
  150. PSCB LastScb = NULL;
  151. PLCB LastLcb = NULL;
  152. PLCB ThisLcb;
  153. PNAME_LINK NameLink;
  154. UNICODE_STRING NextComponent;
  155. UNICODE_STRING Tail;
  156. NTSTATUS Status;
  157. BOOLEAN DroppedParent;
  158. BOOLEAN NeedSnapShot = FALSE;
  159. PAGED_CODE();
  160. //
  161. // Start by setting the remaining name to the full name to be parsed.
  162. //
  163. *RemainingName = FullFileName;
  164. //
  165. // If there are no characters left or the starting Scb is not an index
  166. // or the name begins with a ':' or the Fcb denotes a reparse point
  167. // then return without looking up the name.
  168. //
  169. if (RemainingName->Length == 0 ||
  170. StartingScb->AttributeTypeCode != $INDEX_ALLOCATION ||
  171. RemainingName->Buffer[0] == L':' ||
  172. FlagOn( (StartingScb->Fcb)->Info.FileAttributes, FILE_ATTRIBUTE_REPARSE_POINT )) {
  173. return NULL;
  174. }
  175. //
  176. // Loop until we find the longest matching prefix.
  177. //
  178. while (TRUE) {
  179. ASSERT( NtfsIsExclusiveScb( StartingScb ) );
  180. //
  181. // Get the next component off of the list.
  182. //
  183. Status = NtfsDissectName( *RemainingName,
  184. &NextComponent,
  185. &Tail );
  186. if (!NT_SUCCESS( Status )) {
  187. return NULL;
  188. }
  189. //
  190. // Check if this name is in the splay tree for this Scb.
  191. //
  192. if (FlagOn( *CreateFlags, CREATE_FLAG_IGNORE_CASE )) {
  193. NameLink = NtfsFindNameLink( &StartingScb->ScbType.Index.IgnoreCaseNode,
  194. &NextComponent );
  195. ThisLcb = CONTAINING_RECORD( NameLink, LCB, IgnoreCaseLink );
  196. } else {
  197. NameLink = NtfsFindNameLink( &StartingScb->ScbType.Index.ExactCaseNode,
  198. &NextComponent );
  199. ThisLcb = CONTAINING_RECORD( NameLink, LCB, ExactCaseLink );
  200. }
  201. //
  202. // If we didn't find a match then return the Fcb for the current Scb.
  203. //
  204. if (NameLink == NULL) {
  205. if (NeedSnapShot) {
  206. //
  207. // NtfsCreateScb was not called on the StartingScb so take a
  208. // snapshot now.
  209. //
  210. NtfsSnapshotScb( IrpContext, StartingScb );
  211. }
  212. return LastLcb;
  213. }
  214. //
  215. // If this is a case-insensitive match then copy the exact case of the name into
  216. // the input buffer.
  217. //
  218. if (FlagOn( *CreateFlags, CREATE_FLAG_IGNORE_CASE )) {
  219. RtlCopyMemory( NextComponent.Buffer,
  220. ThisLcb->ExactCaseLink.LinkName.Buffer,
  221. NextComponent.Length );
  222. }
  223. //
  224. // Update the caller's remaining name string to reflect the fact that we found
  225. // a match.
  226. //
  227. *RemainingName = Tail;
  228. //
  229. // Before we give up the previous Lcb check if the name was a DOS-ONLY
  230. // name and set the return boolean if so.
  231. //
  232. if (LastLcb != NULL &&
  233. LastLcb->FileNameAttr->Flags == FILE_NAME_DOS) {
  234. SetFlag( *CreateFlags, CREATE_FLAG_DOS_ONLY_COMPONENT );
  235. }
  236. //
  237. // Update the pointers to the Lcb.
  238. //
  239. LastLcb = ThisLcb;
  240. DroppedParent = FALSE;
  241. //
  242. // We want to acquire the next Fcb and release the one we currently
  243. // have. Try to do a fast acquire.
  244. //
  245. if (!NtfsAcquireFcbWithPaging( IrpContext, ThisLcb->Fcb, ACQUIRE_DONT_WAIT )) {
  246. //
  247. // Reference the link and Fcb so they don't go away.
  248. //
  249. ThisLcb->ReferenceCount += 1;
  250. NtfsAcquireFcbTable( IrpContext, StartingScb->Vcb );
  251. ThisLcb->Fcb->ReferenceCount += 1;
  252. NtfsReleaseFcbTable( IrpContext, StartingScb->Vcb );
  253. //
  254. // Set the IrpContext to acquire paging io resources if our target
  255. // has one. This will lock the MappedPageWriter out of this file.
  256. //
  257. if (ThisLcb->Fcb->PagingIoResource != NULL) {
  258. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_ACQUIRE_PAGING );
  259. }
  260. NtfsReleaseScbWithPaging( IrpContext, StartingScb );
  261. NtfsAcquireFcbWithPaging( IrpContext, ThisLcb->Fcb, 0 );
  262. NtfsAcquireExclusiveScb( IrpContext, StartingScb );
  263. ThisLcb->ReferenceCount -= 1;
  264. NtfsReleaseScb( IrpContext, StartingScb );
  265. NtfsAcquireFcbTable( IrpContext, StartingScb->Vcb );
  266. ThisLcb->Fcb->ReferenceCount -= 1;
  267. NtfsReleaseFcbTable( IrpContext, StartingScb->Vcb );
  268. DroppedParent = TRUE;
  269. } else {
  270. //
  271. // Don't forget to release the starting Scb.
  272. //
  273. NtfsReleaseScbWithPaging( IrpContext, StartingScb );
  274. }
  275. *LcbForTeardown = ThisLcb;
  276. *CurrentFcb = ThisLcb->Fcb;
  277. //
  278. // It is possible that the Lcb we just found could have been removed
  279. // from the prefix table in the window where we dropped the parent Scb.
  280. // In that case we need to check that it is still in the prefix
  281. // table. If not then raise CANT_WAIT to force a rescan through the
  282. // prefix table.
  283. //
  284. if (DroppedParent &&
  285. !FlagOn( ThisLcb->LcbState,
  286. LCB_STATE_IGNORE_CASE_IN_TREE | LCB_STATE_EXACT_CASE_IN_TREE )) {
  287. NtfsRaiseStatus( IrpContext, STATUS_CANT_WAIT, NULL, NULL );
  288. }
  289. //
  290. // If we found a match but the Fcb is uninitialized or is not a directory
  291. // then we are done. Also finished if the remaining name length is 0.
  292. //
  293. if (!FlagOn( ThisLcb->Fcb->FcbState, FCB_STATE_DUP_INITIALIZED ) ||
  294. !IsDirectory( &ThisLcb->Fcb->Info ) ||
  295. RemainingName->Length == 0) {
  296. return LastLcb;
  297. }
  298. //
  299. // Get the Scb for the $INDEX_ALLOCATION for this Fcb.
  300. //
  301. LastScb = StartingScb;
  302. // Since the SCB is usually track on the end of the SCB look
  303. // for it in the FCB first.
  304. if (FlagOn( ThisLcb->Fcb->FcbState, FCB_STATE_COMPOUND_INDEX ) &&
  305. (SafeNodeType( &((PFCB_INDEX) ThisLcb->Fcb)->Scb ) == NTFS_NTC_SCB_INDEX)) {
  306. NeedSnapShot = TRUE;
  307. StartingScb = (PSCB) &((PFCB_INDEX) ThisLcb->Fcb)->Scb;
  308. ASSERT(!FlagOn( StartingScb->ScbState, SCB_STATE_ATTRIBUTE_DELETED) &&
  309. (StartingScb->AttributeTypeCode == $INDEX_ALLOCATION) &&
  310. NtfsAreNamesEqual( IrpContext->Vcb->UpcaseTable, &StartingScb->AttributeName, &NtfsFileNameIndex, FALSE ));
  311. } else {
  312. NeedSnapShot = FALSE;
  313. StartingScb = NtfsCreateScb( IrpContext,
  314. ThisLcb->Fcb,
  315. $INDEX_ALLOCATION,
  316. &NtfsFileNameIndex,
  317. FALSE,
  318. NULL );
  319. }
  320. //
  321. // If there is no normalized name in this Scb, find it now.
  322. //
  323. if ((StartingScb->ScbType.Index.NormalizedName.Length == 0) &&
  324. (LastScb->ScbType.Index.NormalizedName.Length != 0)) {
  325. NtfsUpdateNormalizedName( IrpContext, LastScb, StartingScb, NULL, FALSE, FALSE );
  326. }
  327. }
  328. }
  329. BOOLEAN
  330. NtfsInsertNameLink (
  331. IN PRTL_SPLAY_LINKS *RootNode,
  332. IN PNAME_LINK NameLink
  333. )
  334. /*++
  335. Routine Description:
  336. This routine will insert a name in the splay tree pointed to
  337. by RootNode.
  338. The name could already exist in this tree for a case-insensitive tree.
  339. In that case we simply return FALSE and do nothing.
  340. Arguments:
  341. RootNode - Supplies a pointer to the table.
  342. NameLink - Contains the new link to enter.
  343. Return Value:
  344. BOOLEAN - TRUE if the name is inserted, FALSE otherwise.
  345. --*/
  346. {
  347. FSRTL_COMPARISON_RESULT Comparison;
  348. PNAME_LINK Node;
  349. ULONG i;
  350. ULONG MinLength;
  351. PAGED_CODE();
  352. RtlInitializeSplayLinks( &NameLink->Links );
  353. //
  354. // If we are the first entry in the tree, just become the root.
  355. //
  356. if (*RootNode == NULL) {
  357. *RootNode = &NameLink->Links;
  358. return TRUE;
  359. }
  360. #ifdef NTFS_CHECK_SPLAY
  361. NtfsCheckSplay( *RootNode );
  362. #endif
  363. Node = CONTAINING_RECORD( *RootNode, NAME_LINK, Links );
  364. while (TRUE) {
  365. //
  366. // Lets assume that we are appending to a directory and are greater than
  367. // all entries.
  368. //
  369. Comparison = LessThan;
  370. //
  371. // If the first characters match then we need to do a full string compare.
  372. //
  373. if (Node->LinkName.Buffer[0] == NameLink->LinkName.Buffer[0]) {
  374. //
  375. // Figure out the minimum of the two lengths
  376. //
  377. if (Node->LinkName.Length < NameLink->LinkName.Length) {
  378. MinLength = Node->LinkName.Length;
  379. } else {
  380. MinLength = NameLink->LinkName.Length;
  381. }
  382. //
  383. // Loop through looking at all of the characters in both strings
  384. // testing for equalilty, less than, and greater than
  385. //
  386. i = (ULONG) RtlCompareMemory( Node->LinkName.Buffer, NameLink->LinkName.Buffer, MinLength );
  387. //
  388. // Check if we didn't match up to the length of the shorter name.
  389. //
  390. if (i < MinLength) {
  391. if (Node->LinkName.Buffer[i / sizeof( WCHAR )] > NameLink->LinkName.Buffer[i / sizeof( WCHAR )]) {
  392. Comparison = GreaterThan;
  393. }
  394. //
  395. // We match up to the length of the shorter string. If the lengths are different
  396. // then move down the splay tree.
  397. //
  398. } else if (Node->LinkName.Length > NameLink->LinkName.Length) {
  399. Comparison = GreaterThan;
  400. //
  401. // Exit if the strings are the same length.
  402. //
  403. } else if (Node->LinkName.Length == NameLink->LinkName.Length) {
  404. return FALSE;
  405. }
  406. //
  407. // Compare the first characters to figure out the comparison value.
  408. //
  409. } else if (Node->LinkName.Buffer[0] > NameLink->LinkName.Buffer[0]) {
  410. Comparison = GreaterThan;
  411. }
  412. //
  413. // If the tree prefix is greater than the new prefix then
  414. // we go down the left subtree
  415. //
  416. if (Comparison == GreaterThan) {
  417. //
  418. // We want to go down the left subtree, first check to see
  419. // if we have a left subtree
  420. //
  421. if (RtlLeftChild( &Node->Links ) == NULL) {
  422. //
  423. // there isn't a left child so we insert ourselves as the
  424. // new left child
  425. //
  426. RtlInsertAsLeftChild( &Node->Links, &NameLink->Links );
  427. //
  428. // and exit the while loop
  429. //
  430. *RootNode = RtlSplay( &NameLink->Links );
  431. break;
  432. } else {
  433. //
  434. // there is a left child so simply go down that path, and
  435. // go back to the top of the loop
  436. //
  437. Node = CONTAINING_RECORD( RtlLeftChild( &Node->Links ),
  438. NAME_LINK,
  439. Links );
  440. }
  441. } else {
  442. //
  443. // The tree prefix is either less than or a proper prefix
  444. // of the new string. We treat both cases as less than when
  445. // we do insert. So we want to go down the right subtree,
  446. // first check to see if we have a right subtree
  447. //
  448. if (RtlRightChild( &Node->Links ) == NULL) {
  449. //
  450. // These isn't a right child so we insert ourselves as the
  451. // new right child
  452. //
  453. RtlInsertAsRightChild( &Node->Links, &NameLink->Links );
  454. //
  455. // and exit the while loop
  456. //
  457. *RootNode = RtlSplay( &NameLink->Links );
  458. break;
  459. } else {
  460. //
  461. // there is a right child so simply go down that path, and
  462. // go back to the top of the loop
  463. //
  464. Node = CONTAINING_RECORD( RtlRightChild( &Node->Links ),
  465. NAME_LINK,
  466. Links );
  467. }
  468. }
  469. }
  470. #ifdef NTFS_CHECK_SPLAY
  471. NtfsCheckSplay( *RootNode );
  472. #endif
  473. return TRUE;
  474. }
  475. PNAME_LINK
  476. NtfsFindNameLink (
  477. IN PRTL_SPLAY_LINKS *RootNode,
  478. IN PUNICODE_STRING Name
  479. )
  480. /*++
  481. Routine Description:
  482. This routine searches through a splay link tree looking for a match for the
  483. input name. If we find the corresponding name we will rebalance the
  484. tree.
  485. Arguments:
  486. RootNode - Supplies the parent to search.
  487. Name - This is the name to search for. Note if we are doing a case
  488. insensitive search the name would have been upcased already.
  489. Return Value:
  490. PNAME_LINK - The name link found or NULL if there is no match.
  491. --*/
  492. {
  493. PNAME_LINK Node;
  494. PRTL_SPLAY_LINKS Links;
  495. ULONG i;
  496. ULONG MinLength;
  497. PAGED_CODE();
  498. Links = *RootNode;
  499. #ifdef NTFS_CHECK_SPLAY
  500. if (Links != NULL) {
  501. NtfsCheckSplay( Links );
  502. }
  503. #endif
  504. while (Links != NULL) {
  505. Node = CONTAINING_RECORD( Links, NAME_LINK, Links );
  506. //
  507. // If the first characters are equal then compare the full strings.
  508. //
  509. if (Node->LinkName.Buffer[0] == Name->Buffer[0]) {
  510. //
  511. // Figure out the minimum of the two lengths
  512. //
  513. if (Node->LinkName.Length < Name->Length) {
  514. MinLength = Node->LinkName.Length;
  515. } else {
  516. MinLength = Name->Length;
  517. }
  518. //
  519. // Loop through looking at all of the characters in both strings
  520. // testing for equalilty, less than, and greater than
  521. //
  522. i = (ULONG) RtlCompareMemory( Node->LinkName.Buffer, Name->Buffer, MinLength );
  523. //
  524. // Check if we didn't match up to the length of the shorter name.
  525. //
  526. if (i < MinLength) {
  527. if (Node->LinkName.Buffer[i / sizeof( WCHAR )] < Name->Buffer[i / sizeof( WCHAR )]) {
  528. //
  529. // The prefix is less than the full name
  530. // so we go down the right child
  531. //
  532. Links = RtlRightChild( Links );
  533. } else {
  534. //
  535. // The prefix is greater than the full name
  536. // so we go down the left child
  537. //
  538. Links = RtlLeftChild( Links );
  539. }
  540. //
  541. // We match up to the length of the shorter string. If the lengths are different
  542. // then move down the splay tree.
  543. //
  544. } else if (Node->LinkName.Length < Name->Length) {
  545. //
  546. // The prefix is less than the full name
  547. // so we go down the right child
  548. //
  549. Links = RtlRightChild( Links );
  550. } else if (Node->LinkName.Length > Name->Length) {
  551. //
  552. // The prefix is greater than the full name
  553. // so we go down the left child
  554. //
  555. Links = RtlLeftChild( Links );
  556. //
  557. // The strings are of equal lengths.
  558. //
  559. } else {
  560. *RootNode = RtlSplay( Links );
  561. #ifdef NTFS_CHECK_SPLAY
  562. NtfsCheckSplay( *RootNode );
  563. #endif
  564. return Node;
  565. }
  566. //
  567. // The first characters are different. Use them as a key for which
  568. // way to branch.
  569. //
  570. } else if (Node->LinkName.Buffer[0] < Name->Buffer[0]) {
  571. //
  572. // The prefix is less than the full name
  573. // so we go down the right child
  574. //
  575. Links = RtlRightChild( Links );
  576. } else {
  577. //
  578. // The prefix is greater than the full name
  579. // so we go down the left child
  580. //
  581. Links = RtlLeftChild( Links );
  582. }
  583. }
  584. //
  585. // We didn't find the Link.
  586. //
  587. return NULL;
  588. }
  589. //
  590. // Local support routine
  591. //
  592. FSRTL_COMPARISON_RESULT
  593. NtfsFullCompareNames (
  594. IN PUNICODE_STRING NameA,
  595. IN PUNICODE_STRING NameB
  596. )
  597. /*++
  598. Routine Description:
  599. This function compares two names as fast as possible. Note that since
  600. this comparison is case sensitive we can do a direct memory comparison.
  601. Arguments:
  602. NameA & NameB - The names to compare.
  603. Return Value:
  604. COMPARISON - returns
  605. LessThan if NameA < NameB lexicalgraphically,
  606. GreaterThan if NameA > NameB lexicalgraphically,
  607. EqualTo if NameA is equal to NameB
  608. --*/
  609. {
  610. ULONG i;
  611. ULONG MinLength;
  612. PAGED_CODE();
  613. //
  614. // Figure out the minimum of the two lengths
  615. //
  616. if (NameA->Length < NameB->Length) {
  617. MinLength = NameA->Length;
  618. } else {
  619. MinLength = NameB->Length;
  620. }
  621. //
  622. // Loop through looking at all of the characters in both strings
  623. // testing for equalilty, less than, and greater than
  624. //
  625. i = (ULONG) RtlCompareMemory( NameA->Buffer, NameB->Buffer, MinLength );
  626. if (i < MinLength) {
  627. return (NameA->Buffer[i / sizeof( WCHAR )] < NameB->Buffer[i / sizeof( WCHAR )] ?
  628. LessThan :
  629. GreaterThan);
  630. }
  631. if (NameA->Length < NameB->Length) {
  632. return LessThan;
  633. }
  634. if (NameA->Length > NameB->Length) {
  635. return GreaterThan;
  636. }
  637. return EqualTo;
  638. }
  639. #ifdef NTFS_CHECK_SPLAY
  640. VOID
  641. NtfsCheckSplay (
  642. IN PRTL_SPLAY_LINKS Root
  643. )
  644. {
  645. PRTL_SPLAY_LINKS Current;
  646. PAGED_CODE();
  647. Current = Root;
  648. //
  649. // Root must point to itself.
  650. //
  651. if (Current->Parent != Current) {
  652. KdPrint(("NTFS: Bad splay root\n", 0));
  653. DbgBreakPoint();
  654. }
  655. goto LeftChild;
  656. while (TRUE) {
  657. LeftChild:
  658. //
  659. // If there is a left child then verify it.
  660. //
  661. if (Current->LeftChild != NULL) {
  662. //
  663. // The child can't point to itself and
  664. // the current node must be the parent of the
  665. // child.
  666. //
  667. if ((Current->LeftChild == Current) ||
  668. (Current->LeftChild->Parent != Current)) {
  669. KdPrint(("NTFS: Bad left child\n", 0));
  670. DbgBreakPoint();
  671. }
  672. //
  673. // Go to the left child and verify it.
  674. //
  675. Current = Current->LeftChild;
  676. goto LeftChild;
  677. }
  678. //
  679. // If there is no left child then check for a right child.
  680. //
  681. goto RightChild;
  682. RightChild:
  683. //
  684. // If there is a right child then verify it.
  685. //
  686. if (Current->RightChild != NULL) {
  687. //
  688. // The child can't point to itself and
  689. // the current node must be the parent of the
  690. // child.
  691. //
  692. if ((Current->RightChild == Current) ||
  693. (Current->RightChild->Parent != Current)) {
  694. KdPrint(("NTFS: Bad Right child\n", 0));
  695. DbgBreakPoint();
  696. }
  697. //
  698. // Go to the right child and verify it. We always
  699. // start in the left child of a new node.
  700. //
  701. Current = Current->RightChild;
  702. goto LeftChild;
  703. }
  704. //
  705. // There is no right child. If we are a right child then move up to
  706. // the parent and keep going until we reach the root or reach a left child.
  707. //
  708. goto Parent;
  709. Parent:
  710. //
  711. // We may be in the root now.
  712. //
  713. if (Current == Root) {
  714. return;
  715. }
  716. //
  717. // If we are a left child then go to the parent and look for a right child.
  718. //
  719. if (Current == Current->Parent->LeftChild) {
  720. Current = Current->Parent;
  721. goto RightChild;
  722. }
  723. //
  724. // We are a right child. Go to the parent and check again.
  725. //
  726. Current = Current->Parent;
  727. goto Parent;
  728. }
  729. }
  730. #endif