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.

361 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. fcbtable.c
  5. Abstract:
  6. This module implements the data structures that facilitate management of the
  7. collection of FCB's associated with a NET_ROOT
  8. Author:
  9. Balan Sethu Raman (SethuR) 10/17/96
  10. Revision History:
  11. This was derived from the original implementation of prefix tables done
  12. by Joe Linn.
  13. --*/
  14. #include "precomp.h"
  15. #pragma hdrstop
  16. #ifdef ALLOC_PRAGMA
  17. #pragma alloc_text(PAGE, RxTableComputePathHashValue)
  18. #pragma alloc_text(PAGE, RxInitializeFcbTable)
  19. #pragma alloc_text(PAGE, RxFinalizeFcbTable)
  20. #pragma alloc_text(PAGE, RxFcbTableLookupFcb)
  21. #pragma alloc_text(PAGE, RxFcbTableInsertFcb)
  22. #pragma alloc_text(PAGE, RxFcbTableRemoveFcb)
  23. #endif
  24. //
  25. // The debug trace level
  26. //
  27. #define Dbg (DEBUG_TRACE_PREFIX)
  28. ULONG
  29. RxTableComputePathHashValue (
  30. IN PUNICODE_STRING Name
  31. )
  32. /*++
  33. Routine Description:
  34. here, we compute a caseinsensitive hashvalue. we want to avoid a call/char to
  35. the unicodeupcase routine but we want to still have some reasonable spread on
  36. the hashvalues. many rules just dont work for known important cases. for
  37. example, the (use the first k and the last n) rule that old c compilers used
  38. doesn't pickup the difference among \nt\private\......\slm.ini and that would be
  39. nice. note that the underlying comparison used already takes cognizance of the
  40. length before comparing.
  41. the rule we have selected is to use the 2nd, the last 4, and three selected
  42. at 1/4 points
  43. Arguments:
  44. Name - the name to be hashed
  45. Return Value:
  46. ULONG which is a hashvalue for the name given.
  47. --*/
  48. {
  49. ULONG HashValue;
  50. LONG i,j;
  51. LONG Length = Name->Length / sizeof( WCHAR );
  52. PWCHAR Buffer = Name->Buffer;
  53. LONG Probe[8];
  54. PAGED_CODE();
  55. HashValue = 0;
  56. Probe[0] = 1;
  57. Probe[1] = Length - 1;
  58. Probe[2] = Length - 2;
  59. Probe[3] = Length - 3;
  60. Probe[4] = Length - 4;
  61. Probe[5] = Length >> 2;
  62. Probe[6] = (2 * Length) >> 2;
  63. Probe[7] = (3 * Length) >> 2;
  64. for (i = 0; i < 8; i++) {
  65. j = Probe[i];
  66. if ((j < 0) || (j >= Length)) {
  67. continue;
  68. }
  69. HashValue = (HashValue << 3) + RtlUpcaseUnicodeChar( Buffer[j] );
  70. }
  71. RxDbgTrace( 0, Dbg, ("RxTableComputeHashValue Hashv=%ld Name=%wZ\n", HashValue, Name ));
  72. return HashValue;
  73. }
  74. #define HASH_BUCKET(TABLE,HASHVALUE) &((TABLE)->HashBuckets[(HASHVALUE) % (TABLE)->NumberOfBuckets])
  75. VOID
  76. RxInitializeFcbTable (
  77. IN OUT PRX_FCB_TABLE FcbTable,
  78. IN BOOLEAN CaseInsensitiveMatch
  79. )
  80. /*++
  81. Routine Description:
  82. The routine initializes the RX_FCB_TABLE data structure
  83. Arguments:
  84. pFcbTable - the table instance to be initialized.
  85. CaseInsensitiveMatch - indicates if all the lookups will be case
  86. insensitive
  87. --*/
  88. {
  89. ULONG i;
  90. PAGED_CODE();
  91. //
  92. // this is not zero'd so you have to be careful to init everything
  93. //
  94. FcbTable->NodeTypeCode = RDBSS_NTC_FCB_TABLE;
  95. FcbTable->NodeByteSize = sizeof( RX_PREFIX_TABLE );
  96. ExInitializeResourceLite( &FcbTable->TableLock );
  97. FcbTable->Version = 0;
  98. FcbTable->TableEntryForNull = NULL;
  99. FcbTable->CaseInsensitiveMatch = CaseInsensitiveMatch;
  100. FcbTable->NumberOfBuckets = RX_FCB_TABLE_NUMBER_OF_HASH_BUCKETS;
  101. for (i=0; i < FcbTable->NumberOfBuckets; i++) {
  102. InitializeListHead( &FcbTable->HashBuckets[i] );
  103. }
  104. FcbTable->Lookups = 0;
  105. FcbTable->FailedLookups = 0;
  106. FcbTable->Compares = 0;
  107. }
  108. VOID
  109. RxFinalizeFcbTable (
  110. IN OUT PRX_FCB_TABLE FcbTable
  111. )
  112. /*++
  113. Routine Description:
  114. The routine deinitializes a prefix table.
  115. Arguments:
  116. FcbTable - the table to be finalized.
  117. Return Value:
  118. None.
  119. --*/
  120. {
  121. ULONG i;
  122. PAGED_CODE();
  123. ExDeleteResourceLite( &FcbTable->TableLock );
  124. #if DBG
  125. for (i=0; i < FcbTable->NumberOfBuckets; i++) {
  126. ASSERT( IsListEmpty( &FcbTable->HashBuckets[i] ) );
  127. }
  128. #endif
  129. }
  130. PFCB
  131. RxFcbTableLookupFcb (
  132. IN PRX_FCB_TABLE FcbTable,
  133. IN PUNICODE_STRING Path
  134. )
  135. /*++
  136. Routine Description:
  137. The routine looks up a path in the RX_FCB_TABLE instance.
  138. Arguments:
  139. FcbTable - the table to be looked in.
  140. Path - the name to be looked up
  141. Return Value:
  142. a pointer to an FCB instance if successful, otherwise NULL
  143. --*/
  144. {
  145. ULONG HashValue;
  146. PLIST_ENTRY HashBucket, ListEntry;
  147. PRX_FCB_TABLE_ENTRY FcbTableEntry;
  148. PFCB Fcb = NULL;
  149. PAGED_CODE();
  150. RxDbgTrace(+1, Dbg, ("RxFcbTableLookupName %lx %\n",FcbTable));
  151. if (Path->Length == 0) {
  152. FcbTableEntry = FcbTable->TableEntryForNull;
  153. } else {
  154. HashValue = RxTableComputePathHashValue( Path );
  155. HashBucket = HASH_BUCKET( FcbTable, HashValue );
  156. for (ListEntry = HashBucket->Flink;
  157. ListEntry != HashBucket;
  158. ListEntry = ListEntry->Flink) {
  159. FcbTableEntry = (PRX_FCB_TABLE_ENTRY)CONTAINING_RECORD( ListEntry, RX_FCB_TABLE_ENTRY, HashLinks );
  160. InterlockedIncrement( &FcbTable->Compares );
  161. if ((FcbTableEntry->HashValue == HashValue) &&
  162. (FcbTableEntry->Path.Length == Path->Length) &&
  163. (RtlEqualUnicodeString( Path, &FcbTableEntry->Path, FcbTable->CaseInsensitiveMatch ))) {
  164. break;
  165. }
  166. }
  167. if (ListEntry == HashBucket) {
  168. FcbTableEntry = NULL;
  169. }
  170. }
  171. InterlockedIncrement( &FcbTable->Lookups );
  172. if (FcbTableEntry == NULL) {
  173. InterlockedIncrement( &FcbTable->FailedLookups );
  174. } else {
  175. Fcb = (PFCB)CONTAINING_RECORD( FcbTableEntry, FCB, FcbTableEntry );
  176. RxReferenceNetFcb( Fcb );
  177. }
  178. RxDbgTraceUnIndent( -1,Dbg );
  179. return Fcb;
  180. }
  181. NTSTATUS
  182. RxFcbTableInsertFcb (
  183. IN OUT PRX_FCB_TABLE FcbTable,
  184. IN OUT PFCB Fcb
  185. )
  186. /*++
  187. Routine Description:
  188. This routine inserts a FCB in the RX_FCB_TABLE instance.
  189. Arguments:
  190. FcbTable - the table to be looked in.
  191. Fcb - the FCB instance to be inserted
  192. Return Value:
  193. STATUS_SUCCESS if successful
  194. Notes:
  195. The insertion routine combines the semantics of an insertion followed by
  196. lookup. This is the reason for the additional reference. Otherwise an
  197. additional call to reference the FCB inserted in the table needs to
  198. be made
  199. --*/
  200. {
  201. PRX_FCB_TABLE_ENTRY FcbTableEntry;
  202. ULONG HashValue;
  203. PLIST_ENTRY ListEntry, HashBucket;
  204. PAGED_CODE();
  205. ASSERT( RxIsFcbTableLockExclusive( FcbTable ) );
  206. FcbTableEntry = &Fcb->FcbTableEntry;
  207. FcbTableEntry->HashValue = RxTableComputePathHashValue( &FcbTableEntry->Path );
  208. HashBucket = HASH_BUCKET( FcbTable, FcbTableEntry->HashValue );
  209. RxReferenceNetFcb( Fcb );
  210. if (FcbTableEntry->Path.Length){
  211. InsertHeadList( HashBucket, &FcbTableEntry->HashLinks );
  212. } else {
  213. FcbTable->TableEntryForNull = FcbTableEntry;
  214. }
  215. InterlockedIncrement( &FcbTable->Version );
  216. return STATUS_SUCCESS;
  217. }
  218. NTSTATUS
  219. RxFcbTableRemoveFcb (
  220. IN OUT PRX_FCB_TABLE FcbTable,
  221. IN OUT PFCB Fcb
  222. )
  223. /*++
  224. Routine Description:
  225. This routine deletes an instance from the table
  226. Arguments:
  227. FcbTable - the table to be looked in.
  228. Fcb - the FCB instance to be inserted
  229. Return Value:
  230. STATUS_SUCCESS if successful
  231. --*/
  232. {
  233. PRX_FCB_TABLE_ENTRY FcbTableEntry;
  234. PAGED_CODE();
  235. ASSERT( RxIsPrefixTableLockExclusive( FcbTable ) );
  236. FcbTableEntry = &Fcb->FcbTableEntry;
  237. if (FcbTableEntry->Path.Length) {
  238. RemoveEntryList( &FcbTableEntry->HashLinks );
  239. } else {
  240. FcbTable->TableEntryForNull = NULL;
  241. }
  242. InitializeListHead( &FcbTableEntry->HashLinks );
  243. InterlockedIncrement( &FcbTable->Version );
  244. return STATUS_SUCCESS;
  245. }