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.

810 lines
18 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. PrefxSup.c
  5. Abstract:
  6. This module implements the Udfs Prefix support routines
  7. // @@BEGIN_DDKSPLIT
  8. Author:
  9. Dan Lovinger [DanLo] 8-Oct-1996
  10. Revision History:
  11. // @@END_DDKSPLIT
  12. --*/
  13. #include "UdfProcs.h"
  14. //
  15. // The Bug check file id for this module
  16. //
  17. #define BugCheckFileId (UDFS_BUG_CHECK_PREFXSUP)
  18. //
  19. // The local debug trace level
  20. //
  21. #define Dbg (UDFS_DEBUG_LEVEL_READ)
  22. //
  23. // Local support routines.
  24. //
  25. PLCB
  26. UdfFindNameLink (
  27. IN PIRP_CONTEXT IrpContext,
  28. IN PRTL_SPLAY_LINKS *RootNode,
  29. IN PUNICODE_STRING Name
  30. );
  31. BOOLEAN
  32. UdfInsertNameLink (
  33. IN PIRP_CONTEXT IrpContext,
  34. IN PRTL_SPLAY_LINKS *RootNode,
  35. IN PLCB NameLink
  36. );
  37. #ifdef ALLOC_PRAGMA
  38. #pragma alloc_text(PAGE, UdfFindNameLink)
  39. #pragma alloc_text(PAGE, UdfFindPrefix)
  40. #pragma alloc_text(PAGE, UdfInitializeLcbFromDirContext)
  41. #pragma alloc_text(PAGE, UdfInsertNameLink)
  42. #pragma alloc_text(PAGE, UdfInsertPrefix)
  43. #pragma alloc_text(PAGE, UdfRemovePrefix)
  44. #endif
  45. PLCB
  46. UdfInsertPrefix (
  47. IN PIRP_CONTEXT IrpContext,
  48. IN PFCB Fcb,
  49. IN PUNICODE_STRING Name,
  50. IN BOOLEAN ShortNameMatch,
  51. IN BOOLEAN IgnoreCase,
  52. IN PFCB ParentFcb
  53. )
  54. /*++
  55. Routine Description:
  56. This routine inserts an Lcb linking the two Fcbs together.
  57. Arguments:
  58. Fcb - This is the Fcb whose name is being inserted into the tree.
  59. Name - This is the name for the component.
  60. ShortNameMatch - Indicates whether this name was found on an explicit 8.3 search
  61. IgnoreCase - Indicates if we should insert into the case-insensitive tree.
  62. ParentFcb - This is the ParentFcb. The prefix tree is attached to this.
  63. Return Value:
  64. PLCB - the Lcb inserted.
  65. --*/
  66. {
  67. PLCB Lcb;
  68. PRTL_SPLAY_LINKS *TreeRoot;
  69. PLIST_ENTRY ListLinks;
  70. ULONG Flags;
  71. PWCHAR NameBuffer;
  72. PAGED_CODE();
  73. //
  74. // Check inputs.
  75. //
  76. ASSERT_IRP_CONTEXT( IrpContext );
  77. ASSERT_FCB( Fcb );
  78. ASSERT_EXCLUSIVE_FCB( Fcb );
  79. ASSERT_EXCLUSIVE_FCB( ParentFcb );
  80. ASSERT_FCB_INDEX( ParentFcb );
  81. //
  82. // It must be the case that an index Fcb is only referenced by a single index. Now
  83. // we walk the child's Lcb queue to insure that if any prefixes have already been
  84. // inserted, they all refer to the index Fcb we are linking to. This is the only way
  85. // we can detect directory cross-linkage.
  86. //
  87. if (SafeNodeType( Fcb ) == UDFS_NTC_FCB_INDEX) {
  88. for (ListLinks = Fcb->ParentLcbQueue.Flink;
  89. ListLinks != &Fcb->ParentLcbQueue;
  90. ListLinks = ListLinks->Flink) {
  91. Lcb = CONTAINING_RECORD( ListLinks, LCB, ChildFcbLinks );
  92. if (Lcb->ParentFcb != ParentFcb) {
  93. UdfRaiseStatus( IrpContext, STATUS_DISK_CORRUPT_ERROR );
  94. }
  95. }
  96. }
  97. //
  98. // Capture the separate cases.
  99. //
  100. if (IgnoreCase) {
  101. TreeRoot = &ParentFcb->IgnoreCaseRoot;
  102. Flags = LCB_FLAG_IGNORE_CASE;
  103. } else {
  104. TreeRoot = &ParentFcb->ExactCaseRoot;
  105. Flags = 0;
  106. }
  107. if (ShortNameMatch) {
  108. SetFlag( Flags, LCB_FLAG_SHORT_NAME );
  109. }
  110. //
  111. // Allocate space for the Lcb.
  112. //
  113. if ( sizeof( LCB ) + Name->Length > SIZEOF_LOOKASIDE_LCB ) {
  114. Lcb = FsRtlAllocatePoolWithTag( UdfPagedPool,
  115. sizeof( LCB ) + Name->Length,
  116. TAG_LCB );
  117. SetFlag( Flags, LCB_FLAG_POOL_ALLOCATED );
  118. } else {
  119. Lcb = ExAllocateFromPagedLookasideList( &UdfLcbLookasideList );
  120. }
  121. //
  122. // Set the type and size.
  123. //
  124. Lcb->NodeTypeCode = UDFS_NTC_LCB;
  125. Lcb->NodeByteSize = sizeof( LCB ) + Name->Length;
  126. //
  127. // Initialize the name-based file attributes.
  128. //
  129. Lcb->FileAttributes = 0;
  130. //
  131. // Set up the filename in the Lcb.
  132. //
  133. Lcb->FileName.Length =
  134. Lcb->FileName.MaximumLength = Name->Length;
  135. Lcb->FileName.Buffer = Add2Ptr( Lcb, sizeof( LCB ), PWCHAR );
  136. RtlCopyMemory( Lcb->FileName.Buffer,
  137. Name->Buffer,
  138. Name->Length );
  139. //
  140. // Insert the Lcb into the prefix tree.
  141. //
  142. Lcb->Flags = Flags;
  143. if (!UdfInsertNameLink( IrpContext,
  144. TreeRoot,
  145. Lcb )) {
  146. //
  147. // This will very rarely occur.
  148. //
  149. UdfFreePool( &Lcb );
  150. Lcb = UdfFindNameLink( IrpContext,
  151. TreeRoot,
  152. Name );
  153. if (Lcb == NULL) {
  154. //
  155. // Even worse.
  156. //
  157. UdfRaiseStatus( IrpContext, STATUS_DRIVER_INTERNAL_ERROR );
  158. }
  159. return Lcb;
  160. }
  161. //
  162. // Link the Fcbs together through the Lcb.
  163. //
  164. Lcb->ParentFcb = ParentFcb;
  165. Lcb->ChildFcb = Fcb;
  166. InsertHeadList( &ParentFcb->ChildLcbQueue, &Lcb->ParentFcbLinks );
  167. InsertHeadList( &Fcb->ParentLcbQueue, &Lcb->ChildFcbLinks );
  168. //
  169. // Initialize the reference count.
  170. //
  171. Lcb->Reference = 0;
  172. return Lcb;
  173. }
  174. VOID
  175. UdfRemovePrefix (
  176. IN PIRP_CONTEXT IrpContext,
  177. IN PLCB Lcb
  178. )
  179. /*++
  180. Routine Description:
  181. This routine is called to remove a given prefix of an Fcb.
  182. Arguments:
  183. Lcb - the prefix being removed.
  184. Return Value:
  185. None
  186. --*/
  187. {
  188. PAGED_CODE();
  189. //
  190. // Check inputs.
  191. //
  192. ASSERT_IRP_CONTEXT( IrpContext );
  193. ASSERT_LCB( Lcb );
  194. //
  195. // Check the acquisition of the two Fcbs.
  196. //
  197. ASSERT_EXCLUSIVE_FCB_OR_VCB( Lcb->ParentFcb );
  198. ASSERT_EXCLUSIVE_FCB_OR_VCB( Lcb->ChildFcb );
  199. //
  200. // Now remove the linkage and delete the Lcb.
  201. //
  202. RemoveEntryList( &Lcb->ParentFcbLinks );
  203. RemoveEntryList( &Lcb->ChildFcbLinks );
  204. if (FlagOn( Lcb->Flags, LCB_FLAG_IGNORE_CASE )) {
  205. Lcb->ParentFcb->IgnoreCaseRoot = RtlDelete( &Lcb->Links );
  206. } else {
  207. Lcb->ParentFcb->ExactCaseRoot = RtlDelete( &Lcb->Links );
  208. }
  209. if (FlagOn( Lcb->Flags, LCB_FLAG_POOL_ALLOCATED )) {
  210. ExFreePool( Lcb );
  211. } else {
  212. ExFreeToPagedLookasideList( &UdfLcbLookasideList, Lcb );
  213. }
  214. return;
  215. }
  216. PLCB
  217. UdfFindPrefix (
  218. IN PIRP_CONTEXT IrpContext,
  219. IN OUT PFCB *CurrentFcb,
  220. IN OUT PUNICODE_STRING RemainingName,
  221. IN BOOLEAN IgnoreCase
  222. )
  223. /*++
  224. Routine Description:
  225. This routine begins from the given CurrentFcb and walks through all of
  226. components of the name looking for the longest match in the prefix
  227. splay trees. The search is relative to the starting Fcb so the
  228. full name may not begin with a '\'. On return this routine will
  229. update Current Fcb with the lowest point it has travelled in the
  230. tree. It will also hold only that resource on return and it must
  231. hold that resource.
  232. Arguments:
  233. CurrentFcb - Address to store the lowest Fcb we find on this search.
  234. On return we will have acquired this Fcb. On entry this is the
  235. Fcb to examine.
  236. RemainingName - Supplies a buffer to store the exact case of the name being
  237. searched for. Initially will contain the upcase name based on the
  238. IgnoreCase flag.
  239. IgnoreCase - Indicates if we are doing a case-insensitive compare.
  240. Return Value:
  241. The Lcb used to find the current Fcb, NULL if we didn't find any prefix
  242. Fcbs.
  243. --*/
  244. {
  245. UNICODE_STRING LocalRemainingName;
  246. UNICODE_STRING FinalName;
  247. PLCB NameLink;
  248. PLCB CurrentLcb = NULL;
  249. PAGED_CODE();
  250. //
  251. // Check inputs.
  252. //
  253. ASSERT_IRP_CONTEXT( IrpContext );
  254. ASSERT_FCB( *CurrentFcb );
  255. ASSERT_EXCLUSIVE_FCB( *CurrentFcb );
  256. //
  257. // Make a local copy of the input strings.
  258. //
  259. LocalRemainingName = *RemainingName;
  260. //
  261. // Loop until we find the longest matching prefix.
  262. //
  263. while (TRUE) {
  264. //
  265. // If there are no characters left or we are not at an IndexFcb then
  266. // return immediately.
  267. //
  268. if ((LocalRemainingName.Length == 0) ||
  269. (SafeNodeType( *CurrentFcb ) != UDFS_NTC_FCB_INDEX)) {
  270. return CurrentLcb;
  271. }
  272. //
  273. // Split off the next component from the name.
  274. //
  275. UdfDissectName( IrpContext,
  276. &LocalRemainingName,
  277. &FinalName );
  278. //
  279. // Check if this name is in the splay tree for this Scb.
  280. //
  281. if (IgnoreCase) {
  282. NameLink = UdfFindNameLink( IrpContext,
  283. &(*CurrentFcb)->IgnoreCaseRoot,
  284. &FinalName );
  285. } else {
  286. NameLink = UdfFindNameLink( IrpContext,
  287. &(*CurrentFcb)->ExactCaseRoot,
  288. &FinalName );
  289. }
  290. //
  291. // If we didn't find a match then exit.
  292. //
  293. if (NameLink == NULL) {
  294. break;
  295. }
  296. CurrentLcb = NameLink;
  297. //
  298. // If this is a case-insensitive match then copy the exact case of the name into
  299. // the input buffer.
  300. //
  301. if (IgnoreCase) {
  302. RtlCopyMemory( FinalName.Buffer,
  303. NameLink->FileName.Buffer,
  304. NameLink->FileName.Length );
  305. }
  306. //
  307. // Update the caller's remaining name string to reflect the fact that we found
  308. // a match.
  309. //
  310. *RemainingName = LocalRemainingName;
  311. //
  312. // Move down to the next component in the tree. Acquire without waiting.
  313. // If this fails then lock the Fcb to reference this Fcb and then drop
  314. // the parent and acquire the child.
  315. //
  316. ASSERT( NameLink->ParentFcb == *CurrentFcb );
  317. if (!UdfAcquireFcbExclusive( IrpContext, NameLink->ChildFcb, TRUE )) {
  318. //
  319. // If we can't wait then raise CANT_WAIT.
  320. //
  321. if (!FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT )) {
  322. UdfRaiseStatus( IrpContext, STATUS_CANT_WAIT );
  323. }
  324. UdfLockVcb( IrpContext, IrpContext->Vcb );
  325. NameLink->ChildFcb->FcbReference += 1;
  326. NameLink->Reference += 1;
  327. UdfUnlockVcb( IrpContext, IrpContext->Vcb );
  328. UdfReleaseFcb( IrpContext, *CurrentFcb );
  329. UdfAcquireFcbExclusive( IrpContext, NameLink->ChildFcb, FALSE );
  330. UdfLockVcb( IrpContext, IrpContext->Vcb );
  331. NameLink->ChildFcb->FcbReference -= 1;
  332. NameLink->Reference -= 1;
  333. UdfUnlockVcb( IrpContext, IrpContext->Vcb );
  334. } else {
  335. UdfReleaseFcb( IrpContext, *CurrentFcb );
  336. }
  337. *CurrentFcb = NameLink->ChildFcb;
  338. }
  339. return CurrentLcb;
  340. }
  341. VOID
  342. UdfInitializeLcbFromDirContext (
  343. IN PIRP_CONTEXT IrpContext,
  344. IN PLCB Lcb,
  345. IN PDIR_ENUM_CONTEXT DirContext
  346. )
  347. /*++
  348. Routine Description:
  349. This routine performs common initialization of Lcbs from found directory
  350. entries.
  351. Arguments:
  352. Lcb - the Lcb to initialize.
  353. DirContext - the directory enumeration context, enumerated to the FID associated
  354. with this Lcb.
  355. Return Value:
  356. None.
  357. --*/
  358. {
  359. PAGED_CODE();
  360. //
  361. // Check inputs.
  362. //
  363. ASSERT_IRP_CONTEXT( IrpContext );
  364. ASSERT_LCB( Lcb );
  365. ASSERT( DirContext->Fid != NULL );
  366. //
  367. // This is falling down trivial now. Simply update the hidden flag in the Lcb.
  368. //
  369. if (FlagOn( DirContext->Fid->Flags, NSR_FID_F_HIDDEN )) {
  370. SetFlag( Lcb->FileAttributes, FILE_ATTRIBUTE_HIDDEN );
  371. }
  372. }
  373. //
  374. // Local support routine
  375. //
  376. PLCB
  377. UdfFindNameLink (
  378. IN PIRP_CONTEXT IrpContext,
  379. IN PRTL_SPLAY_LINKS *RootNode,
  380. IN PUNICODE_STRING Name
  381. )
  382. /*++
  383. Routine Description:
  384. This routine searches through a splay link tree looking for a match for the
  385. input name. If we find the corresponding name we will rebalance the
  386. tree.
  387. Arguments:
  388. RootNode - Supplies the parent to search.
  389. Name - This is the name to search for. Note if we are doing a case
  390. insensitive search the name would have been upcased already.
  391. Return Value:
  392. PLCB - The name link found or NULL if there is no match.
  393. --*/
  394. {
  395. FSRTL_COMPARISON_RESULT Comparison;
  396. PLCB Node;
  397. PRTL_SPLAY_LINKS Links;
  398. PAGED_CODE();
  399. Links = *RootNode;
  400. while (Links != NULL) {
  401. Node = CONTAINING_RECORD( Links, LCB, Links );
  402. //
  403. // Compare the prefix in the tree with the full name
  404. //
  405. Comparison = UdfFullCompareNames( IrpContext, &Node->FileName, Name );
  406. //
  407. // See if they don't match
  408. //
  409. if (Comparison == GreaterThan) {
  410. //
  411. // The prefix is greater than the full name
  412. // so we go down the left child
  413. //
  414. Links = RtlLeftChild( Links );
  415. //
  416. // And continue searching down this tree
  417. //
  418. } else if (Comparison == LessThan) {
  419. //
  420. // The prefix is less than the full name
  421. // so we go down the right child
  422. //
  423. Links = RtlRightChild( Links );
  424. //
  425. // And continue searching down this tree
  426. //
  427. } else {
  428. //
  429. // We found it.
  430. //
  431. // Splay the tree and save the new root.
  432. //
  433. *RootNode = RtlSplay( Links );
  434. return Node;
  435. }
  436. }
  437. //
  438. // We didn't find the Link.
  439. //
  440. return NULL;
  441. }
  442. //
  443. // Local support routine
  444. //
  445. BOOLEAN
  446. UdfInsertNameLink (
  447. IN PIRP_CONTEXT IrpContext,
  448. IN PRTL_SPLAY_LINKS *RootNode,
  449. IN PLCB NameLink
  450. )
  451. /*++
  452. Routine Description:
  453. This routine will insert a name in the splay tree pointed to
  454. by RootNode.
  455. Arguments:
  456. RootNode - Supplies a pointer to the table.
  457. NameLink - Contains the new link to enter.
  458. Return Value:
  459. BOOLEAN - TRUE if the name is inserted, FALSE otherwise.
  460. --*/
  461. {
  462. FSRTL_COMPARISON_RESULT Comparison;
  463. PLCB Node;
  464. PAGED_CODE();
  465. //
  466. // Check inputs.
  467. //
  468. ASSERT_IRP_CONTEXT( IrpContext );
  469. RtlInitializeSplayLinks( &NameLink->Links );
  470. //
  471. // If we are the first entry in the tree, just become the root.
  472. //
  473. if (*RootNode == NULL) {
  474. *RootNode = &NameLink->Links;
  475. return TRUE;
  476. }
  477. Node = CONTAINING_RECORD( *RootNode, LCB, Links );
  478. while (TRUE) {
  479. //
  480. // Compare the prefix in the tree with the prefix we want
  481. // to insert.
  482. //
  483. Comparison = UdfFullCompareNames( IrpContext, &Node->FileName, &NameLink->FileName );
  484. //
  485. // If we found the entry, return immediately.
  486. //
  487. if (Comparison == EqualTo) { return FALSE; }
  488. //
  489. // If the tree prefix is greater than the new prefix then
  490. // we go down the left subtree
  491. //
  492. if (Comparison == GreaterThan) {
  493. //
  494. // We want to go down the left subtree, first check to see
  495. // if we have a left subtree
  496. //
  497. if (RtlLeftChild( &Node->Links ) == NULL) {
  498. //
  499. // there isn't a left child so we insert ourselves as the
  500. // new left child
  501. //
  502. RtlInsertAsLeftChild( &Node->Links, &NameLink->Links );
  503. //
  504. // and exit the while loop
  505. //
  506. break;
  507. } else {
  508. //
  509. // there is a left child so simply go down that path, and
  510. // go back to the top of the loop
  511. //
  512. Node = CONTAINING_RECORD( RtlLeftChild( &Node->Links ),
  513. LCB,
  514. Links );
  515. }
  516. } else {
  517. //
  518. // The tree prefix is either less than or a proper prefix
  519. // of the new string. We treat both cases as less than when
  520. // we do insert. So we want to go down the right subtree,
  521. // first check to see if we have a right subtree
  522. //
  523. if (RtlRightChild( &Node->Links ) == NULL) {
  524. //
  525. // These isn't a right child so we insert ourselves as the
  526. // new right child
  527. //
  528. RtlInsertAsRightChild( &Node->Links, &NameLink->Links );
  529. //
  530. // and exit the while loop
  531. //
  532. break;
  533. } else {
  534. //
  535. // there is a right child so simply go down that path, and
  536. // go back to the top of the loop
  537. //
  538. Node = CONTAINING_RECORD( RtlRightChild( &Node->Links ),
  539. LCB,
  540. Links );
  541. }
  542. }
  543. }
  544. return TRUE;
  545. }