Source code of Windows XP (NT5)
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.

719 lines
17 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. PrefxSup.c
  5. Abstract:
  6. This module implements the Cdfs Prefix support routines
  7. // @@BEGIN_DDKSPLIT
  8. Author:
  9. Brian Andrew [BrianAn] 07-July-1995
  10. Revision History:
  11. // @@END_DDKSPLIT
  12. --*/
  13. #include "CdProcs.h"
  14. //
  15. // The Bug check file id for this module
  16. //
  17. #define BugCheckFileId (CDFS_BUG_CHECK_PREFXSUP)
  18. //
  19. // Local support routines.
  20. //
  21. PNAME_LINK
  22. CdFindNameLink (
  23. IN PIRP_CONTEXT IrpContext,
  24. IN PRTL_SPLAY_LINKS *RootNode,
  25. IN PUNICODE_STRING Name
  26. );
  27. BOOLEAN
  28. CdInsertNameLink (
  29. IN PIRP_CONTEXT IrpContext,
  30. IN PRTL_SPLAY_LINKS *RootNode,
  31. IN PNAME_LINK NameLink
  32. );
  33. #ifdef ALLOC_PRAGMA
  34. #pragma alloc_text(PAGE, CdFindNameLink)
  35. #pragma alloc_text(PAGE, CdFindPrefix)
  36. #pragma alloc_text(PAGE, CdInsertNameLink)
  37. #pragma alloc_text(PAGE, CdInsertPrefix)
  38. #pragma alloc_text(PAGE, CdRemovePrefix)
  39. #endif
  40. VOID
  41. CdInsertPrefix (
  42. IN PIRP_CONTEXT IrpContext,
  43. IN PFCB Fcb,
  44. IN PCD_NAME Name,
  45. IN BOOLEAN IgnoreCase,
  46. IN BOOLEAN ShortNameMatch,
  47. IN PFCB ParentFcb
  48. )
  49. /*++
  50. Routine Description:
  51. This routine inserts the names in the given Lcb into the links for the
  52. parent.
  53. Arguments:
  54. Fcb - This is the Fcb whose name is being inserted into the tree.
  55. Name - This is the name for the component. The IgnoreCase flag tells
  56. us which entry this belongs to.
  57. IgnoreCase - Indicates if we should insert the case-insensitive name.
  58. ShortNameMatch - Indicates if this is the short name.
  59. ParentFcb - This is the ParentFcb. The prefix tree is attached to this.
  60. Return Value:
  61. None.
  62. --*/
  63. {
  64. ULONG PrefixFlags;
  65. PNAME_LINK NameLink;
  66. PPREFIX_ENTRY PrefixEntry;
  67. PRTL_SPLAY_LINKS *TreeRoot;
  68. PWCHAR NameBuffer;
  69. PAGED_CODE();
  70. //
  71. // Check if we need to allocate a prefix entry for the short name.
  72. // If we can't allocate one then fail quietly. We don't have to
  73. // insert the name.
  74. //
  75. PrefixEntry = &Fcb->FileNamePrefix;
  76. if (ShortNameMatch) {
  77. if (Fcb->ShortNamePrefix == NULL) {
  78. Fcb->ShortNamePrefix = ExAllocatePoolWithTag( CdPagedPool,
  79. sizeof( PREFIX_ENTRY ),
  80. TAG_PREFIX_ENTRY );
  81. if (Fcb->ShortNamePrefix == NULL) { return; }
  82. RtlZeroMemory( Fcb->ShortNamePrefix, sizeof( PREFIX_ENTRY ));
  83. }
  84. PrefixEntry = Fcb->ShortNamePrefix;
  85. }
  86. //
  87. // Capture the local variables for the separate cases.
  88. //
  89. if (IgnoreCase) {
  90. PrefixFlags = PREFIX_FLAG_IGNORE_CASE_IN_TREE;
  91. NameLink = &PrefixEntry->IgnoreCaseName;
  92. TreeRoot = &ParentFcb->IgnoreCaseRoot;
  93. } else {
  94. PrefixFlags = PREFIX_FLAG_EXACT_CASE_IN_TREE;
  95. NameLink = &PrefixEntry->ExactCaseName;
  96. TreeRoot = &ParentFcb->ExactCaseRoot;
  97. }
  98. //
  99. // If neither name is in the tree then check whether we have a buffer for this
  100. // name
  101. //
  102. if (!FlagOn( PrefixEntry->PrefixFlags,
  103. PREFIX_FLAG_EXACT_CASE_IN_TREE | PREFIX_FLAG_IGNORE_CASE_IN_TREE )) {
  104. //
  105. // Allocate a new buffer if the embedded buffer is too small.
  106. //
  107. NameBuffer = PrefixEntry->FileNameBuffer;
  108. if (Name->FileName.Length > BYTE_COUNT_EMBEDDED_NAME) {
  109. NameBuffer = ExAllocatePoolWithTag( CdPagedPool,
  110. Name->FileName.Length * 2,
  111. TAG_PREFIX_NAME );
  112. //
  113. // Exit if no name buffer.
  114. //
  115. if (NameBuffer == NULL) { return; }
  116. }
  117. //
  118. // Split the buffer and fill in the separate components.
  119. //
  120. PrefixEntry->ExactCaseName.FileName.Buffer = NameBuffer;
  121. PrefixEntry->IgnoreCaseName.FileName.Buffer = Add2Ptr( NameBuffer,
  122. Name->FileName.Length,
  123. PWCHAR );
  124. PrefixEntry->IgnoreCaseName.FileName.MaximumLength =
  125. PrefixEntry->IgnoreCaseName.FileName.Length =
  126. PrefixEntry->ExactCaseName.FileName.MaximumLength =
  127. PrefixEntry->ExactCaseName.FileName.Length = Name->FileName.Length;
  128. }
  129. //
  130. // Only insert the name if not already present.
  131. //
  132. if (!FlagOn( PrefixEntry->PrefixFlags, PrefixFlags )) {
  133. //
  134. // Initialize the name in the prefix entry.
  135. //
  136. RtlCopyMemory( NameLink->FileName.Buffer,
  137. Name->FileName.Buffer,
  138. Name->FileName.Length );
  139. CdInsertNameLink( IrpContext,
  140. TreeRoot,
  141. NameLink );
  142. PrefixEntry->Fcb = Fcb;
  143. SetFlag( PrefixEntry->PrefixFlags, PrefixFlags );
  144. }
  145. return;
  146. }
  147. VOID
  148. CdRemovePrefix (
  149. IN PIRP_CONTEXT IrpContext,
  150. IN PFCB Fcb
  151. )
  152. /*++
  153. Routine Description:
  154. This routine is called to remove all of the previx entries of a
  155. given Fcb from its parent Fcb.
  156. Arguments:
  157. Fcb - Fcb whose entries are to be removed.
  158. Return Value:
  159. None
  160. --*/
  161. {
  162. PAGED_CODE();
  163. //
  164. // Start with the short name prefix entry.
  165. //
  166. if (Fcb->ShortNamePrefix != NULL) {
  167. if (FlagOn( Fcb->ShortNamePrefix->PrefixFlags, PREFIX_FLAG_IGNORE_CASE_IN_TREE )) {
  168. Fcb->ParentFcb->IgnoreCaseRoot = RtlDelete( &Fcb->ShortNamePrefix->IgnoreCaseName.Links );
  169. }
  170. if (FlagOn( Fcb->ShortNamePrefix->PrefixFlags, PREFIX_FLAG_EXACT_CASE_IN_TREE )) {
  171. Fcb->ParentFcb->ExactCaseRoot = RtlDelete( &Fcb->ShortNamePrefix->ExactCaseName.Links );
  172. }
  173. ClearFlag( Fcb->ShortNamePrefix->PrefixFlags,
  174. PREFIX_FLAG_IGNORE_CASE_IN_TREE | PREFIX_FLAG_EXACT_CASE_IN_TREE );
  175. }
  176. //
  177. // Now do the long name prefix entries.
  178. //
  179. if (FlagOn( Fcb->FileNamePrefix.PrefixFlags, PREFIX_FLAG_IGNORE_CASE_IN_TREE )) {
  180. Fcb->ParentFcb->IgnoreCaseRoot = RtlDelete( &Fcb->FileNamePrefix.IgnoreCaseName.Links );
  181. }
  182. if (FlagOn( Fcb->FileNamePrefix.PrefixFlags, PREFIX_FLAG_EXACT_CASE_IN_TREE )) {
  183. Fcb->ParentFcb->ExactCaseRoot = RtlDelete( &Fcb->FileNamePrefix.ExactCaseName.Links );
  184. }
  185. ClearFlag( Fcb->FileNamePrefix.PrefixFlags,
  186. PREFIX_FLAG_IGNORE_CASE_IN_TREE | PREFIX_FLAG_EXACT_CASE_IN_TREE );
  187. //
  188. // Deallocate any buffer we may have allocated.
  189. //
  190. if ((Fcb->FileNamePrefix.ExactCaseName.FileName.Buffer != (PWCHAR) &Fcb->FileNamePrefix.FileNameBuffer) &&
  191. (Fcb->FileNamePrefix.ExactCaseName.FileName.Buffer != NULL)) {
  192. ExFreePool( Fcb->FileNamePrefix.ExactCaseName.FileName.Buffer );
  193. Fcb->FileNamePrefix.ExactCaseName.FileName.Buffer = NULL;
  194. }
  195. return;
  196. }
  197. VOID
  198. CdFindPrefix (
  199. IN PIRP_CONTEXT IrpContext,
  200. IN OUT PFCB *CurrentFcb,
  201. IN OUT PUNICODE_STRING RemainingName,
  202. IN BOOLEAN IgnoreCase
  203. )
  204. /*++
  205. Routine Description:
  206. This routine begins from the given CurrentFcb and walks through all of
  207. components of the name looking for the longest match in the prefix
  208. splay trees. The search is relative to the starting Fcb so the
  209. full name may not begin with a '\'. On return this routine will
  210. update Current Fcb with the lowest point it has travelled in the
  211. tree. It will also hold only that resource on return and it must
  212. hold that resource.
  213. Arguments:
  214. CurrentFcb - Address to store the lowest Fcb we find on this search.
  215. On return we will have acquired this Fcb. On entry this is the
  216. Fcb to examine.
  217. RemainingName - Supplies a buffer to store the exact case of the name being
  218. searched for. Initially will contain the upcase name based on the
  219. IgnoreCase flag.
  220. IgnoreCase - Indicates if we are doing a case-insensitive compare.
  221. Return Value:
  222. None
  223. --*/
  224. {
  225. UNICODE_STRING LocalRemainingName;
  226. UNICODE_STRING FinalName;
  227. PNAME_LINK NameLink;
  228. PPREFIX_ENTRY PrefixEntry;
  229. PAGED_CODE();
  230. //
  231. // Make a local copy of the input strings.
  232. //
  233. LocalRemainingName = *RemainingName;
  234. //
  235. // Loop until we find the longest matching prefix.
  236. //
  237. while (TRUE) {
  238. //
  239. // If there are no characters left or we are not at an IndexFcb then
  240. // return immediately.
  241. //
  242. if ((LocalRemainingName.Length == 0) ||
  243. (SafeNodeType( *CurrentFcb ) != CDFS_NTC_FCB_INDEX)) {
  244. return;
  245. }
  246. //
  247. // Split off the next component from the name.
  248. //
  249. CdDissectName( IrpContext,
  250. &LocalRemainingName,
  251. &FinalName );
  252. //
  253. // Check if this name is in the splay tree for this Scb.
  254. //
  255. if (IgnoreCase) {
  256. NameLink = CdFindNameLink( IrpContext,
  257. &(*CurrentFcb)->IgnoreCaseRoot,
  258. &FinalName );
  259. //
  260. // Get the prefix entry from this NameLink. Don't access any
  261. // fields within it until we verify we have a name link.
  262. //
  263. PrefixEntry = (PPREFIX_ENTRY) CONTAINING_RECORD( NameLink,
  264. PREFIX_ENTRY,
  265. IgnoreCaseName );
  266. } else {
  267. NameLink = CdFindNameLink( IrpContext,
  268. &(*CurrentFcb)->ExactCaseRoot,
  269. &FinalName );
  270. PrefixEntry = (PPREFIX_ENTRY) CONTAINING_RECORD( NameLink,
  271. PREFIX_ENTRY,
  272. ExactCaseName );
  273. }
  274. //
  275. // If we didn't find a match then exit.
  276. //
  277. if (NameLink == NULL) { return; }
  278. //
  279. // If this is a case-insensitive match then copy the exact case of the name into
  280. // the input buffer.
  281. //
  282. if (IgnoreCase) {
  283. RtlCopyMemory( FinalName.Buffer,
  284. PrefixEntry->ExactCaseName.FileName.Buffer,
  285. PrefixEntry->ExactCaseName.FileName.Length );
  286. }
  287. //
  288. // Update the caller's remaining name string to reflect the fact that we found
  289. // a match.
  290. //
  291. *RemainingName = LocalRemainingName;
  292. //
  293. // Move down to the next component in the tree. Acquire without waiting.
  294. // If this fails then lock the Fcb to reference this Fcb and then drop
  295. // the parent and acquire the child.
  296. //
  297. if (!CdAcquireFcbExclusive( IrpContext, PrefixEntry->Fcb, TRUE )) {
  298. //
  299. // If we can't wait then raise CANT_WAIT.
  300. //
  301. if (!FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT )) {
  302. CdRaiseStatus( IrpContext, STATUS_CANT_WAIT );
  303. }
  304. CdLockVcb( IrpContext, IrpContext->Vcb );
  305. PrefixEntry->Fcb->FcbReference += 1;
  306. CdUnlockVcb( IrpContext, IrpContext->Vcb );
  307. CdReleaseFcb( IrpContext, *CurrentFcb );
  308. CdAcquireFcbExclusive( IrpContext, PrefixEntry->Fcb, FALSE );
  309. CdLockVcb( IrpContext, IrpContext->Vcb );
  310. PrefixEntry->Fcb->FcbReference -= 1;
  311. CdUnlockVcb( IrpContext, IrpContext->Vcb );
  312. } else {
  313. CdReleaseFcb( IrpContext, *CurrentFcb );
  314. }
  315. *CurrentFcb = PrefixEntry->Fcb;
  316. }
  317. }
  318. //
  319. // Local support routine
  320. //
  321. PNAME_LINK
  322. CdFindNameLink (
  323. IN PIRP_CONTEXT IrpContext,
  324. IN PRTL_SPLAY_LINKS *RootNode,
  325. IN PUNICODE_STRING Name
  326. )
  327. /*++
  328. Routine Description:
  329. This routine searches through a splay link tree looking for a match for the
  330. input name. If we find the corresponding name we will rebalance the
  331. tree.
  332. Arguments:
  333. RootNode - Supplies the parent to search.
  334. Name - This is the name to search for. Note if we are doing a case
  335. insensitive search the name would have been upcased already.
  336. Return Value:
  337. PNAME_LINK - The name link found or NULL if there is no match.
  338. --*/
  339. {
  340. FSRTL_COMPARISON_RESULT Comparison;
  341. PNAME_LINK Node;
  342. PRTL_SPLAY_LINKS Links;
  343. PAGED_CODE();
  344. Links = *RootNode;
  345. while (Links != NULL) {
  346. Node = CONTAINING_RECORD( Links, NAME_LINK, Links );
  347. //
  348. // Compare the prefix in the tree with the full name
  349. //
  350. Comparison = CdFullCompareNames( IrpContext, &Node->FileName, Name );
  351. //
  352. // See if they don't match
  353. //
  354. if (Comparison == GreaterThan) {
  355. //
  356. // The prefix is greater than the full name
  357. // so we go down the left child
  358. //
  359. Links = RtlLeftChild( Links );
  360. //
  361. // And continue searching down this tree
  362. //
  363. } else if (Comparison == LessThan) {
  364. //
  365. // The prefix is less than the full name
  366. // so we go down the right child
  367. //
  368. Links = RtlRightChild( Links );
  369. //
  370. // And continue searching down this tree
  371. //
  372. } else {
  373. //
  374. // We found it.
  375. //
  376. // Splay the tree and save the new root.
  377. //
  378. *RootNode = RtlSplay( Links );
  379. return Node;
  380. }
  381. }
  382. //
  383. // We didn't find the Link.
  384. //
  385. return NULL;
  386. }
  387. //
  388. // Local support routine
  389. //
  390. BOOLEAN
  391. CdInsertNameLink (
  392. IN PIRP_CONTEXT IrpContext,
  393. IN PRTL_SPLAY_LINKS *RootNode,
  394. IN PNAME_LINK NameLink
  395. )
  396. /*++
  397. Routine Description:
  398. This routine will insert a name in the splay tree pointed to
  399. by RootNode.
  400. The name could already exist in this tree for a case-insensitive tree.
  401. In that case we simply return FALSE and do nothing.
  402. Arguments:
  403. RootNode - Supplies a pointer to the table.
  404. NameLink - Contains the new link to enter.
  405. Return Value:
  406. BOOLEAN - TRUE if the name is inserted, FALSE otherwise.
  407. --*/
  408. {
  409. FSRTL_COMPARISON_RESULT Comparison;
  410. PNAME_LINK Node;
  411. PAGED_CODE();
  412. RtlInitializeSplayLinks( &NameLink->Links );
  413. //
  414. // If we are the first entry in the tree, just become the root.
  415. //
  416. if (*RootNode == NULL) {
  417. *RootNode = &NameLink->Links;
  418. return TRUE;
  419. }
  420. Node = CONTAINING_RECORD( *RootNode, NAME_LINK, Links );
  421. while (TRUE) {
  422. //
  423. // Compare the prefix in the tree with the prefix we want
  424. // to insert.
  425. //
  426. Comparison = CdFullCompareNames( IrpContext, &Node->FileName, &NameLink->FileName );
  427. //
  428. // If we found the entry, return immediately.
  429. //
  430. if (Comparison == EqualTo) { return FALSE; }
  431. //
  432. // If the tree prefix is greater than the new prefix then
  433. // we go down the left subtree
  434. //
  435. if (Comparison == GreaterThan) {
  436. //
  437. // We want to go down the left subtree, first check to see
  438. // if we have a left subtree
  439. //
  440. if (RtlLeftChild( &Node->Links ) == NULL) {
  441. //
  442. // there isn't a left child so we insert ourselves as the
  443. // new left child
  444. //
  445. RtlInsertAsLeftChild( &Node->Links, &NameLink->Links );
  446. //
  447. // and exit the while loop
  448. //
  449. break;
  450. } else {
  451. //
  452. // there is a left child so simply go down that path, and
  453. // go back to the top of the loop
  454. //
  455. Node = CONTAINING_RECORD( RtlLeftChild( &Node->Links ),
  456. NAME_LINK,
  457. Links );
  458. }
  459. } else {
  460. //
  461. // The tree prefix is either less than or a proper prefix
  462. // of the new string. We treat both cases as less than when
  463. // we do insert. So we want to go down the right subtree,
  464. // first check to see if we have a right subtree
  465. //
  466. if (RtlRightChild( &Node->Links ) == NULL) {
  467. //
  468. // These isn't a right child so we insert ourselves as the
  469. // new right child
  470. //
  471. RtlInsertAsRightChild( &Node->Links, &NameLink->Links );
  472. //
  473. // and exit the while loop
  474. //
  475. break;
  476. } else {
  477. //
  478. // there is a right child so simply go down that path, and
  479. // go back to the top of the loop
  480. //
  481. Node = CONTAINING_RECORD( RtlRightChild( &Node->Links ),
  482. NAME_LINK,
  483. Links );
  484. }
  485. }
  486. }
  487. return TRUE;
  488. }