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.

480 lines
16 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. smbcedb.h
  5. Abstract:
  6. This module defines all functions, along with implementations for inline functions
  7. related to accessing the SMB connection engine
  8. Revision History:
  9. Balan Sethu Raman [SethuR] 6-March-1995
  10. Notes:
  11. The various data structures created by the mini rdr (Server Entries, Session Entries
  12. and Net Root Entries) are used in asynchronous operations. Hence a reference count
  13. mechanism is used to keep track of the creation/use/destruction of these data structures.
  14. The usage patterns for these data structures falls into one of two cases
  15. 1) A prior reference exists and access is required
  16. 2) A new reference need be created.
  17. These two scenarios are dealt with by two sets of access routines
  18. SmbCeGetAssociatedServerEntry,
  19. SmbCeGetAssociatedNetRootEntry
  20. and
  21. SmbCeReferenceAssociatedServerEntry,
  22. SmbCeReferenceAssociatedNetRootEntry.
  23. The first set of routines include the necessary asserts in a debug build to ensure that a
  24. reference does exist.
  25. The dereferencing mechanism is provided by the following routines
  26. SmbCeDereferenceServerEntry,
  27. SmbCeDereferenceSessionEntry,
  28. SmbCeDereferenceNetRootEntry.
  29. The dereferencing routines also ensure that the data structures are deleted if the reference
  30. count is zero.
  31. The construction of the various SMB mini redirector structures ( Server,Session and Net root entries )
  32. follow a two phase protocol since network traffic is involved. The first set of routines
  33. initiate the construction while the second set of routines complete the construction.
  34. These routines are
  35. SmbCeInitializeServerEntry,
  36. SmbCeCompleteServerEntryInitialization,
  37. SmbCeInitializeSessionEntry,
  38. SmbCeCompleteSessionEntryInitialization,
  39. SmbCeInitializeNetRootEntry,
  40. and SmbCeCompleteNetRootEntryInitialization.
  41. Each of the SMB mini redirector data structures embodies a state diagram that consist of
  42. the following states
  43. SMBCEDB_ACTIVE, // the instance is in use
  44. SMBCEDB_INVALID, // the instance has been invalidated/disconnected.
  45. SMBCEDB_MARKED_FOR_DELETION, // the instance has been marked for deletion.
  46. SMBCEDB_RECYCLE, // the instance is available for recycling
  47. SMBCEDB_START_CONSTRUCTION, // Initiate construction.
  48. SMBCEDB_CONSTRUCTION_IN_PROGRESS, // the instance construction is in progress
  49. SMBCEDB_DESTRUCTION_IN_PROGRESS // the instance destruction is in progress
  50. A SMB MRX data structure instance begins its life in SMBCEDB_START_CONSTRUCTION state.
  51. When the construction is initiated the state transitions to SMBCEDB_CONSTRUCTION_IN_PROGRESS.
  52. On completion of the construction the state is either transitioned to SMBCEDB_ACTIVE if the
  53. construction was successful. If the construction was not successful the state transitions to
  54. SMBCEDB_MARKED_FOR_DELETION if scavenging is to be done or SMBCEDB_DESTRUCTION_IN_PROGRESS
  55. if the tear down has been initiated.
  56. An instance in the SMBCEDB_ACTIVE state transitions to SMBCEDB_INVALID when the transport/remote server
  57. information associated with it has been invalidated due to disconnects etc. This state is a
  58. cue for a reconnect attempt to be initiated.
  59. The SMBCEDB_RECYCLE state is not in use currently.
  60. All the state transitions are accomplished by the following set of routines which ensure that
  61. the appropriate concurrency control action is taken.
  62. SmbCeUpdateServerEntryState,
  63. SmbCeUpdateSessionEntryState,
  64. and SmbCeUpdateNetRootEntryState.
  65. Since the Server,Session and NetRoot entries are often referenced together the following
  66. two routines provide a batching mechanism to minimize the concurrency control overhead.
  67. SmbCeReferenceAssociatedEntries,
  68. SmbCeDereferenceEntries
  69. In addition this file also contains helper functions to access certain fields of
  70. MRX_SRV_CALL,MRX_NET_ROOT and MRX_V_NET_ROOT which are intrepreted differently by the SMB
  71. mini redirector.
  72. --*/
  73. #ifndef _SMBCEDB_H_
  74. #define _SMBCEDB_H_
  75. #include <smbcedbp.h> // To accomodate inline routines.
  76. //
  77. // All the routines below return the referenced object if successful. It is the caller's
  78. // responsibility to dereference them subsequently.
  79. //
  80. PSMBCEDB_SERVER_ENTRY
  81. SmbCeFindServerEntry(
  82. PUNICODE_STRING pServerName,
  83. SMBCEDB_SERVER_TYPE ServerType,
  84. PRX_CONNECTION_ID RxConnectionId);
  85. extern NTSTATUS
  86. SmbCeFindOrConstructServerEntry(
  87. PUNICODE_STRING pServerName,
  88. SMBCEDB_SERVER_TYPE ServerType,
  89. PSMBCEDB_SERVER_ENTRY *pServerEntryPtr,
  90. PBOOLEAN pNewServerEntry,
  91. PRX_CONNECTION_ID RxConnectionId);
  92. extern NTSTATUS
  93. SmbCeInitializeServerEntry(
  94. IN PMRX_SRV_CALL pSrvCall,
  95. IN OUT PMRX_SRVCALL_CALLBACK_CONTEXT pCallbackContext,
  96. IN BOOLEAN DeferNetworkInitialization);
  97. extern NTSTATUS
  98. SmbCeFindOrConstructSessionEntry(
  99. IN PMRX_V_NET_ROOT pVirtualNetRoot,
  100. OUT PSMBCEDB_SESSION_ENTRY *pSessionEntryPtr);
  101. extern NTSTATUS
  102. SmbCeFindOrConstructNetRootEntry(
  103. IN PMRX_NET_ROOT pNetRoot,
  104. OUT PSMBCEDB_NET_ROOT_ENTRY *pNetRootEntryPtr);
  105. extern NTSTATUS
  106. SmbCeFindOrConstructVNetRootContext(
  107. IN OUT PMRX_V_NET_ROOT pVNetRoot,
  108. IN BOOLEAN fDeferNetworkInitialization,
  109. IN BOOLEAN fCscAgentOpen);
  110. //
  111. // The finalization routines are invoked in the context of a worker thread to finalize
  112. // the construction of an entry as well as resume other entries waiting for it.
  113. //
  114. extern VOID
  115. SmbCeCompleteServerEntryInitialization(
  116. PSMBCEDB_SERVER_ENTRY pServerEntry,
  117. NTSTATUS Status);
  118. extern VOID
  119. SmbCeCompleteSessionEntryInitialization(
  120. PVOID pSessionEntry,
  121. NTSTATUS Status,
  122. BOOLEAN SecuritySignatureReturned);
  123. extern VOID
  124. SmbCeCompleteVNetRootContextInitialization(
  125. PVOID pVNetRootContextEntry);
  126. extern VOID
  127. SmbReferenceRecord(
  128. PREFERENCE_RECORD pReferenceRecord,
  129. PVOID FileName,
  130. ULONG FileLine);
  131. //
  132. // Routines for referencing/dereferencing SMB Mini redirector information associated with
  133. // the wrapper data structures.
  134. //
  135. INLINE PSMBCEDB_SERVER_ENTRY
  136. SmbCeGetAssociatedServerEntry(
  137. PMRX_SRV_CALL pSrvCall)
  138. {
  139. ASSERT(pSrvCall->Context != NULL);
  140. return (PSMBCEDB_SERVER_ENTRY)(pSrvCall->Context);
  141. }
  142. INLINE PSMBCE_V_NET_ROOT_CONTEXT
  143. SmbCeGetAssociatedVNetRootContext(
  144. PMRX_V_NET_ROOT pVNetRoot)
  145. {
  146. ASSERT(pVNetRoot != NULL);
  147. return (PSMBCE_V_NET_ROOT_CONTEXT)(pVNetRoot->Context);
  148. }
  149. INLINE PSMBCEDB_NET_ROOT_ENTRY
  150. SmbCeGetAssociatedNetRootEntry(
  151. PMRX_NET_ROOT pNetRoot)
  152. {
  153. ASSERT(pNetRoot->Context != NULL);
  154. return (PSMBCEDB_NET_ROOT_ENTRY)(pNetRoot->Context);
  155. }
  156. //
  157. // All the macros for referencing and dereferencing begin with a prefix SmbCep...
  158. // The p stands for a private version which is used for implementing reference tracking.
  159. // By selectively turning on the desired flag it is possible to track every instance
  160. // of a given type as the reference count is modified.
  161. //
  162. #define MRXSMB_REF_TRACE_SERVER_ENTRY (0x00000001)
  163. #define MRXSMB_REF_TRACE_NETROOT_ENTRY (0x00000002)
  164. #define MRXSMB_REF_TRACE_SESSION_ENTRY (0x00000004)
  165. #define MRXSMB_REF_TRACE_VNETROOT_CONTEXT (0x00000008)
  166. #define MRXSMB_LOG_REF_TRACKING (0x80000000)
  167. #define MRXSMB_PRINT_REF_TRACKING (0x40000000)
  168. extern ULONG MRxSmbReferenceTracingValue;
  169. VOID
  170. MRxSmbTrackRefCount(
  171. PVOID pInstance,
  172. PCHAR FileName,
  173. ULONG Line);
  174. VOID
  175. MRxSmbTrackDerefCount(
  176. PVOID pInstance,
  177. PCHAR FileName,
  178. ULONG Line);
  179. #define MRXSMB_REF_TRACING_ON(TraceMask) (TraceMask & MRxSmbReferenceTracingValue)
  180. //#define MRXSMB_PRINT_REF_COUNT(TYPE,Count) \
  181. // if (MRXSMB_REF_TRACING_ON( MRXSMB_REF_TRACE_ ## TYPE )) { \
  182. // DbgPrint("%ld\n",Count); \
  183. // }
  184. INLINE VOID
  185. SmbCepReferenceServerEntry(
  186. PSMBCEDB_SERVER_ENTRY pServerEntry)
  187. {
  188. ASSERT(pServerEntry->Header.ObjectType == SMBCEDB_OT_SERVER);
  189. InterlockedIncrement(&pServerEntry->Header.SwizzleCount);
  190. }
  191. INLINE VOID
  192. SmbCepReferenceSessionEntry(
  193. PSMBCEDB_SESSION_ENTRY pSessionEntry)
  194. {
  195. ASSERT(pSessionEntry->Header.ObjectType == SMBCEDB_OT_SESSION);
  196. InterlockedIncrement(&(pSessionEntry->Header.SwizzleCount));
  197. }
  198. INLINE VOID
  199. SmbCepReferenceNetRootEntry(
  200. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry,
  201. PVOID FileName,
  202. ULONG FileLine)
  203. {
  204. ASSERT(pNetRootEntry->Header.ObjectType == SMBCEDB_OT_NETROOT);
  205. InterlockedIncrement(&(pNetRootEntry->Header.SwizzleCount));
  206. }
  207. INLINE VOID
  208. SmbCepReferenceVNetRootContext(
  209. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext)
  210. {
  211. ASSERT(pVNetRootContext->Header.ObjectType == SMBCEDB_OT_VNETROOTCONTEXT);
  212. InterlockedIncrement(&(pVNetRootContext->Header.SwizzleCount));
  213. }
  214. INLINE PSMBCEDB_SERVER_ENTRY
  215. SmbCeReferenceAssociatedServerEntry(
  216. PMRX_SRV_CALL pSrvCall)
  217. {
  218. PSMBCEDB_SERVER_ENTRY pServerEntry;
  219. if ((pServerEntry = pSrvCall->Context) != NULL) {
  220. ASSERT(pServerEntry->Header.SwizzleCount > 0);
  221. MRxSmbTrackRefCount(pServerEntry,__FILE__,__LINE__);
  222. SmbCepReferenceServerEntry(pServerEntry);
  223. }
  224. return pServerEntry;
  225. }
  226. INLINE PSMBCEDB_NET_ROOT_ENTRY
  227. SmbCeReferenceAssociatedNetRootEntry(
  228. PMRX_NET_ROOT pNetRoot)
  229. {
  230. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry;
  231. if ((pNetRootEntry = pNetRoot->Context) != NULL) {
  232. ASSERT(pNetRootEntry->Header.SwizzleCount > 0);
  233. MRxSmbTrackRefCount(pNetRootEntry,__FILE__,__LINE__);
  234. SmbCepReferenceNetRootEntry(pNetRootEntry,__FILE__,__LINE__);
  235. }
  236. return pNetRootEntry;
  237. }
  238. extern VOID
  239. SmbCepDereferenceServerEntry(
  240. PSMBCEDB_SERVER_ENTRY pServerEntry);
  241. extern VOID
  242. SmbCepDereferenceSessionEntry(
  243. PSMBCEDB_SESSION_ENTRY pSessionEntry);
  244. extern VOID
  245. SmbCepDereferenceNetRootEntry(
  246. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry,
  247. PVOID FileName,
  248. ULONG FileLine);
  249. extern VOID
  250. SmbCepDereferenceVNetRootContext(
  251. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext);
  252. #define SmbCeReferenceServerEntry(pServerEntry) \
  253. MRxSmbTrackRefCount(pServerEntry,__FILE__,__LINE__); \
  254. SmbReferenceRecord(&pServerEntry->ReferenceRecord[0],__FILE__,__LINE__); \
  255. SmbCepReferenceServerEntry(pServerEntry)
  256. #define SmbCeReferenceNetRootEntry(pNetRootEntry) \
  257. MRxSmbTrackRefCount(pNetRootEntry,__FILE__,__LINE__); \
  258. SmbCepReferenceNetRootEntry(pNetRootEntry,__FILE__,__LINE__)
  259. #define SmbCeReferenceVNetRootContext(pVNetRootContext) \
  260. MRxSmbTrackRefCount(pVNetRootContext,__FILE__,__LINE__); \
  261. SmbReferenceRecord(&pVNetRootContext->ReferenceRecord[0],__FILE__,__LINE__); \
  262. SmbCepReferenceVNetRootContext(pVNetRootContext)
  263. #define SmbCeReferenceSessionEntry(pSessionEntry) \
  264. MRxSmbTrackRefCount(pSessionEntry,__FILE__,__LINE__); \
  265. SmbCepReferenceSessionEntry(pSessionEntry)
  266. #define SmbCeDereferenceServerEntry(pServerEntry) \
  267. MRxSmbTrackDerefCount(pServerEntry,__FILE__,__LINE__); \
  268. SmbReferenceRecord(&pServerEntry->ReferenceRecord[0],__FILE__,__LINE__); \
  269. SmbCepDereferenceServerEntry(pServerEntry)
  270. #define SmbCeDereferenceNetRootEntry(pNetRootEntry) \
  271. MRxSmbTrackDerefCount(pNetRootEntry,__FILE__,__LINE__); \
  272. SmbCepDereferenceNetRootEntry(pNetRootEntry,__FILE__,__LINE__)
  273. #define SmbCeDereferenceSessionEntry(pSessionEntry) \
  274. MRxSmbTrackDerefCount(pSessionEntry,__FILE__,__LINE__); \
  275. SmbCepDereferenceSessionEntry(pSessionEntry)
  276. #define SmbCeDereferenceVNetRootContext(pVNetRootContext) \
  277. MRxSmbTrackDerefCount(pVNetRootContext,__FILE__,__LINE__); \
  278. SmbReferenceRecord(&pVNetRootContext->ReferenceRecord[0],__FILE__,__LINE__); \
  279. SmbCepDereferenceVNetRootContext(pVNetRootContext)
  280. INLINE VOID
  281. SmbCeDereferenceEntries(
  282. PSMBCEDB_SERVER_ENTRY pServerEntry,
  283. PSMBCEDB_SESSION_ENTRY pSessionEntry,
  284. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry)
  285. {
  286. SmbCeDereferenceNetRootEntry(pNetRootEntry);
  287. SmbCeDereferenceSessionEntry(pSessionEntry);
  288. SmbCeDereferenceServerEntry(pServerEntry);
  289. }
  290. //
  291. // Routines for updating the state of SMB MRX data structures
  292. //
  293. #define SmbCeUpdateServerEntryState(pServerEntry,NEWSTATE) \
  294. InterlockedExchange(&pServerEntry->Header.State,(NEWSTATE))
  295. #define SmbCeUpdateSessionEntryState(pSessionEntry,NEWSTATE) \
  296. InterlockedExchange(&pSessionEntry->Header.State,(NEWSTATE))
  297. #define SmbCeUpdateNetRootEntryState(pNetRootEntry,NEWSTATE) \
  298. InterlockedExchange(&pNetRootEntry->Header.State,(NEWSTATE))
  299. #define SmbCeUpdateVNetRootContextState(pVNetRootContext,NEWSTATE) \
  300. InterlockedExchange(&pVNetRootContext->Header.State,(NEWSTATE))
  301. INLINE BOOLEAN
  302. SmbCeIsServerInDisconnectedMode(
  303. PSMBCEDB_SERVER_ENTRY pServerEntry)
  304. {
  305. return ((pServerEntry->Server.CscState == ServerCscDisconnected) ||
  306. (pServerEntry->Server.CscState == ServerCscTransitioningToShadowing));
  307. }
  308. INLINE BOOLEAN
  309. SmbCeIsServerSetupForDisconnectedOperation(
  310. PSMBCEDB_SERVER_ENTRY pServerEntry)
  311. {
  312. return (pServerEntry->pTransport == NULL);
  313. }
  314. //
  315. // The RDBSS wrapper stores all the server names with a backslash prepended to
  316. // them. This helps synthesize UNC names easily. In order to manipulate the
  317. // Server name in the SMB protocol the \ needs to be stripped off.
  318. INLINE VOID
  319. SmbCeGetServerName(
  320. PMRX_SRV_CALL pSrvCall,
  321. PUNICODE_STRING pServerName)
  322. {
  323. ASSERT(pSrvCall->pSrvCallName != NULL);
  324. pServerName->Buffer = pSrvCall->pSrvCallName->Buffer + 1;
  325. pServerName->Length = pSrvCall->pSrvCallName->Length - sizeof(WCHAR);
  326. pServerName->MaximumLength = pSrvCall->pSrvCallName->MaximumLength - sizeof(WCHAR);
  327. }
  328. INLINE VOID
  329. SmbCeGetNetRootName(
  330. PMRX_NET_ROOT pNetRoot,
  331. PUNICODE_STRING pNetRootName)
  332. {
  333. ASSERT(pNetRoot->pNetRootName != NULL);
  334. *pNetRootName = *pNetRoot->pNetRootName;
  335. }
  336. extern NTSTATUS
  337. SmbCeDestroyAssociatedVNetRootContext(
  338. PMRX_V_NET_ROOT pVNetRoot);
  339. extern VOID
  340. SmbCeTearDownVNetRootContext(
  341. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext);
  342. extern NTSTATUS
  343. SmbCeGetUserNameAndDomainName(
  344. PSMBCEDB_SESSION_ENTRY pSessionEntry,
  345. PUNICODE_STRING pUserName,
  346. PUNICODE_STRING pUserDomainName);
  347. extern BOOLEAN
  348. SmbCeAreServerEntriesAliased(
  349. PSMBCEDB_SERVER_ENTRY pServernEntry1,
  350. PSMBCEDB_SERVER_ENTRY pServerEntry2);
  351. extern VOID
  352. SmbCeUnblockSerializedSessionSetupRequests(
  353. PSMBCEDB_SESSION_ENTRY pSessionEntry);
  354. extern NTSTATUS
  355. SmbCeUpdateSrvCall(
  356. IN PSMBCEDB_SERVER_ENTRY pServerEntry);
  357. extern NTSTATUS
  358. SmbCeUpdateNetRoot(
  359. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry,
  360. PMRX_NET_ROOT pNetRoot);
  361. extern NTSTATUS
  362. SmbCeScavengeRelatedContexts(
  363. IN PSMBCEDB_SERVER_ENTRY pServerEntry);
  364. extern VOID
  365. SmbCeResumeOutstandingRequests(
  366. IN OUT PSMBCEDB_REQUESTS pRequests,
  367. IN NTSTATUS Status);
  368. // given \\server\share, this routine returns a refcounted serverentry
  369. NTSTATUS
  370. FindServerEntryFromCompleteUNCPath(
  371. USHORT *lpuServerShareName,
  372. PSMBCEDB_SERVER_ENTRY *ppServerEntry);
  373. // given \\server\share, this routine returns a refcounted netroot entry
  374. NTSTATUS
  375. FindNetRootEntryFromCompleteUNCPath(
  376. USHORT *lpuServerShareName,
  377. PSMBCEDB_NET_ROOT_ENTRY *ppNetRootEntry);
  378. #endif // _SMBCEDB_H_