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.

406 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. WorkQue.c
  5. Abstract:
  6. This module implements the Work queue routines for the Cdfs File
  7. system.
  8. // @@BEGIN_DDKSPLIT
  9. Author:
  10. Brian Andrew [BrianAn] 01-July-1995
  11. Revision History:
  12. // @@END_DDKSPLIT
  13. --*/
  14. #include "CdProcs.h"
  15. //
  16. // The Bug check file id for this module
  17. //
  18. #define BugCheckFileId (CDFS_BUG_CHECK_WORKQUE)
  19. //
  20. // The following constant is the maximum number of ExWorkerThreads that we
  21. // will allow to be servicing a particular target device at any one time.
  22. //
  23. #define FSP_PER_DEVICE_THRESHOLD (2)
  24. //
  25. // Local support routines
  26. //
  27. VOID
  28. CdAddToWorkque (
  29. IN PIRP_CONTEXT IrpContext,
  30. IN PIRP Irp
  31. );
  32. #ifdef ALLOC_PRAGMA
  33. #pragma alloc_text(PAGE, CdFsdPostRequest)
  34. #pragma alloc_text(PAGE, CdOplockComplete)
  35. #pragma alloc_text(PAGE, CdPrePostIrp)
  36. #endif
  37. NTSTATUS
  38. CdFsdPostRequest (
  39. IN PIRP_CONTEXT IrpContext,
  40. IN PIRP Irp
  41. )
  42. /*++
  43. Routine Description:
  44. This routine enqueues the request packet specified by IrpContext to the
  45. work queue associated with the FileSystemDeviceObject. This is a FSD
  46. routine.
  47. Arguments:
  48. IrpContext - Pointer to the IrpContext to be queued to the Fsp.
  49. Irp - I/O Request Packet.
  50. Return Value:
  51. STATUS_PENDING
  52. --*/
  53. {
  54. PAGED_CODE();
  55. ASSERT_IRP_CONTEXT( IrpContext );
  56. ASSERT_IRP( Irp );
  57. //
  58. // Posting is a three step operation. First lock down any buffers
  59. // in the Irp. Next cleanup the IrpContext for the post and finally
  60. // add this to a workque.
  61. //
  62. CdPrePostIrp( IrpContext, Irp );
  63. CdAddToWorkque( IrpContext, Irp );
  64. //
  65. // And return to our caller
  66. //
  67. return STATUS_PENDING;
  68. }
  69. VOID
  70. CdPrePostIrp (
  71. IN PIRP_CONTEXT IrpContext,
  72. IN PIRP Irp
  73. )
  74. /*++
  75. Routine Description:
  76. This routine performs any neccessary work before STATUS_PENDING is
  77. returned with the Fsd thread. This routine is called within the
  78. filesystem and by the oplock package.
  79. Arguments:
  80. Context - Pointer to the IrpContext to be queued to the Fsp
  81. Irp - I/O Request Packet.
  82. Return Value:
  83. None.
  84. --*/
  85. {
  86. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
  87. BOOLEAN RemovedFcb;
  88. PAGED_CODE();
  89. ASSERT_IRP_CONTEXT( IrpContext );
  90. ASSERT_IRP( Irp );
  91. //
  92. // Case on the type of the operation.
  93. //
  94. switch (IrpContext->MajorFunction) {
  95. case IRP_MJ_CREATE :
  96. //
  97. // If called from the oplock package then there is an
  98. // Fcb to possibly teardown. We will call the teardown
  99. // routine and release the Fcb if still present. The cleanup
  100. // code in create will know not to release this Fcb because
  101. // we will clear the pointer.
  102. //
  103. if ((IrpContext->TeardownFcb != NULL) &&
  104. *(IrpContext->TeardownFcb) != NULL) {
  105. CdTeardownStructures( IrpContext, *(IrpContext->TeardownFcb), &RemovedFcb );
  106. if (!RemovedFcb) {
  107. CdReleaseFcb( IrpContext, *(IrpContext->TeardownFcb) );
  108. }
  109. *(IrpContext->TeardownFcb) = NULL;
  110. IrpContext->TeardownFcb = NULL;
  111. }
  112. break;
  113. //
  114. // We need to lock the user's buffer, unless this is an MDL-read,
  115. // in which case there is no user buffer.
  116. //
  117. case IRP_MJ_READ :
  118. if (!FlagOn( IrpContext->MinorFunction, IRP_MN_MDL )) {
  119. CdLockUserBuffer( IrpContext, IrpSp->Parameters.Read.Length );
  120. }
  121. break;
  122. //
  123. // We also need to check whether this is a query file operation.
  124. //
  125. case IRP_MJ_DIRECTORY_CONTROL :
  126. if (IrpContext->MinorFunction == IRP_MN_QUERY_DIRECTORY) {
  127. CdLockUserBuffer( IrpContext, IrpSp->Parameters.QueryDirectory.Length );
  128. }
  129. break;
  130. }
  131. //
  132. // Cleanup the IrpContext for the post.
  133. //
  134. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_MORE_PROCESSING );
  135. CdCleanupIrpContext( IrpContext, TRUE );
  136. //
  137. // Mark the Irp to show that we've already returned pending to the user.
  138. //
  139. IoMarkIrpPending( Irp );
  140. return;
  141. }
  142. VOID
  143. CdOplockComplete (
  144. IN PIRP_CONTEXT IrpContext,
  145. IN PIRP Irp
  146. )
  147. /*++
  148. Routine Description:
  149. This routine is called by the oplock package when an oplock break has
  150. completed, allowing an Irp to resume execution. If the status in
  151. the Irp is STATUS_SUCCESS, then we queue the Irp to the Fsp queue.
  152. Otherwise we complete the Irp with the status in the Irp.
  153. If we are completing due to an error then check if there is any
  154. cleanup to do.
  155. Arguments:
  156. Irp - I/O Request Packet.
  157. Return Value:
  158. None.
  159. --*/
  160. {
  161. BOOLEAN RemovedFcb;
  162. PAGED_CODE();
  163. //
  164. // Check on the return value in the Irp. If success then we
  165. // are to post this request.
  166. //
  167. if (Irp->IoStatus.Status == STATUS_SUCCESS) {
  168. //
  169. // Check if there is any cleanup work to do.
  170. //
  171. switch (IrpContext->MajorFunction) {
  172. case IRP_MJ_CREATE :
  173. //
  174. // If called from the oplock package then there is an
  175. // Fcb to possibly teardown. We will call the teardown
  176. // routine and release the Fcb if still present. The cleanup
  177. // code in create will know not to release this Fcb because
  178. // we will clear the pointer.
  179. //
  180. if (IrpContext->TeardownFcb != NULL) {
  181. CdTeardownStructures( IrpContext, *(IrpContext->TeardownFcb), &RemovedFcb );
  182. if (!RemovedFcb) {
  183. CdReleaseFcb( IrpContext, *(IrpContext->TeardownFcb) );
  184. }
  185. *(IrpContext->TeardownFcb) = NULL;
  186. IrpContext->TeardownFcb = NULL;
  187. }
  188. break;
  189. }
  190. //
  191. // Insert the Irp context in the workqueue.
  192. //
  193. CdAddToWorkque( IrpContext, Irp );
  194. //
  195. // Otherwise complete the request.
  196. //
  197. } else {
  198. CdCompleteRequest( IrpContext, Irp, Irp->IoStatus.Status );
  199. }
  200. return;
  201. }
  202. //
  203. // Local support routine
  204. //
  205. VOID
  206. CdAddToWorkque (
  207. IN PIRP_CONTEXT IrpContext,
  208. IN PIRP Irp
  209. )
  210. /*++
  211. Routine Description:
  212. This routine is called to acually store the posted Irp to the Fsp
  213. workque.
  214. Arguments:
  215. IrpContext - Pointer to the IrpContext to be queued to the Fsp
  216. Irp - I/O Request Packet.
  217. Return Value:
  218. None.
  219. --*/
  220. {
  221. PVOLUME_DEVICE_OBJECT Vdo;
  222. KIRQL SavedIrql;
  223. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
  224. //
  225. // Check if this request has an associated file object, and thus volume
  226. // device object.
  227. //
  228. if (IrpSp->FileObject != NULL) {
  229. Vdo = CONTAINING_RECORD( IrpSp->DeviceObject,
  230. VOLUME_DEVICE_OBJECT,
  231. DeviceObject );
  232. //
  233. // Check to see if this request should be sent to the overflow
  234. // queue. If not, then send it off to an exworker thread.
  235. //
  236. KeAcquireSpinLock( &Vdo->OverflowQueueSpinLock, &SavedIrql );
  237. if (Vdo->PostedRequestCount > FSP_PER_DEVICE_THRESHOLD) {
  238. //
  239. // We cannot currently respond to this IRP so we'll just enqueue it
  240. // to the overflow queue on the volume.
  241. //
  242. InsertTailList( &Vdo->OverflowQueue,
  243. &IrpContext->WorkQueueItem.List );
  244. Vdo->OverflowQueueCount += 1;
  245. KeReleaseSpinLock( &Vdo->OverflowQueueSpinLock, SavedIrql );
  246. return;
  247. } else {
  248. //
  249. // We are going to send this Irp to an ex worker thread so up
  250. // the count.
  251. //
  252. Vdo->PostedRequestCount += 1;
  253. KeReleaseSpinLock( &Vdo->OverflowQueueSpinLock, SavedIrql );
  254. }
  255. }
  256. //
  257. // Send it off.....
  258. //
  259. ExInitializeWorkItem( &IrpContext->WorkQueueItem,
  260. CdFspDispatch,
  261. IrpContext );
  262. ExQueueWorkItem( &IrpContext->WorkQueueItem, CriticalWorkQueue );
  263. return;
  264. }