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.

430 lines
9.4 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. cancelapi.c
  5. Abstract:
  6. This module contains the cancel safe DDI set
  7. Author:
  8. Nar Ganapathy (narg) 1-Jan-1999
  9. Environment:
  10. Kernel mode
  11. Revision History:
  12. --*/
  13. #include "iomgr.h"
  14. //
  15. // The library exposes everything with the name "Wdmlib". This ensures drivers
  16. // using the backward compatible Cancel DDI Lib won't opportunistically pick
  17. // up the kernel exports just because they were built using the XP DDK.
  18. //
  19. #if CSQLIB
  20. #define CSQLIB_DDI(x) Wdmlib##x
  21. #else
  22. #define CSQLIB_DDI(x) x
  23. #endif
  24. VOID
  25. IopCsqCancelRoutine(
  26. IN PDEVICE_OBJECT DeviceObject,
  27. IN PIRP Irp
  28. );
  29. NTSTATUS
  30. CSQLIB_DDI(IoCsqInitialize)(
  31. IN PIO_CSQ Csq,
  32. IN PIO_CSQ_INSERT_IRP CsqInsertIrp,
  33. IN PIO_CSQ_REMOVE_IRP CsqRemoveIrp,
  34. IN PIO_CSQ_PEEK_NEXT_IRP CsqPeekNextIrp,
  35. IN PIO_CSQ_ACQUIRE_LOCK CsqAcquireLock,
  36. IN PIO_CSQ_RELEASE_LOCK CsqReleaseLock,
  37. IN PIO_CSQ_COMPLETE_CANCELED_IRP CsqCompleteCanceledIrp
  38. )
  39. /*++
  40. Routine Description:
  41. This routine initializes the Cancel queue
  42. Arguments:
  43. Csq - Pointer to the cancel queue.
  44. Return Value:
  45. The function returns STATUS_SUCCESS on successful initialization
  46. --*/
  47. {
  48. Csq->CsqInsertIrp = CsqInsertIrp;
  49. Csq->CsqRemoveIrp = CsqRemoveIrp;
  50. Csq->CsqPeekNextIrp = CsqPeekNextIrp;
  51. Csq->CsqAcquireLock = CsqAcquireLock;
  52. Csq->CsqReleaseLock = CsqReleaseLock;
  53. Csq->CsqCompleteCanceledIrp = CsqCompleteCanceledIrp;
  54. Csq->ReservePointer = NULL;
  55. Csq->Type = IO_TYPE_CSQ;
  56. return STATUS_SUCCESS;
  57. }
  58. VOID
  59. CSQLIB_DDI(IoCsqInsertIrp)(
  60. IN PIO_CSQ Csq,
  61. IN PIRP Irp,
  62. IN PIO_CSQ_IRP_CONTEXT Context
  63. )
  64. /*++
  65. Routine Description:
  66. This routine inserts the IRP into the queue and associates the context with the IRP.
  67. The context has to be in non-paged pool if the context will be used in a DPC or interrupt routine.
  68. The routine assumes that Irp->Tail.Overlay.DriverContext[3] is available for use by the APIs.
  69. It's ok to pass a NULL context if the driver assumes that it will always use IoCsqRemoveNextIrp to
  70. remove an IRP.
  71. Arguments:
  72. Csq - Pointer to the cancel queue.
  73. Irp - Irp to be inserted
  74. Context - Context to be associated with Irp.
  75. Return Value:
  76. None. The caller is expected to call this from its dispatch routine and return STATUS_PENDING. Note
  77. that once this routine returns the IRP can be canceled and freed. The only guarantee is that the
  78. context field is not freed and the caller should use IoCsqRemoveIrp to retreive an IRP given the context.
  79. --*/
  80. {
  81. KIRQL irql;
  82. PDRIVER_CANCEL cancelRoutine;
  83. #if CSQLIB
  84. PVOID originalDriverContext;
  85. #endif
  86. //
  87. // Set the association between the context and the IRP.
  88. //
  89. if (Context) {
  90. Irp->Tail.Overlay.DriverContext[3] = Context;
  91. Context->Irp = Irp;
  92. Context->Csq = Csq;
  93. Context->Type = IO_TYPE_CSQ_IRP_CONTEXT;
  94. } else {
  95. Irp->Tail.Overlay.DriverContext[3] = Csq;
  96. }
  97. #if !CSQLIB
  98. IoMarkIrpPending(Irp);
  99. #endif
  100. Csq->ReservePointer = NULL; // Force drivers to be good citizens
  101. #if CSQLIB
  102. originalDriverContext = Irp->Tail.Overlay.DriverContext[3];
  103. #endif
  104. Csq->CsqAcquireLock(Csq, &irql);
  105. Csq->CsqInsertIrp(Csq, Irp);
  106. //
  107. // If the driver changes the driverContext[3] value.
  108. // to something else, its an indication that it does not
  109. // want the IRP to be inserted. So return without inserting the IRP.
  110. // We use this as an indication because CsqInsertIrp is a VOID function
  111. // and we don't want to change the API from a VOID.
  112. //
  113. #if CSQLIB
  114. if (Irp->Tail.Overlay.DriverContext[3] != originalDriverContext) {
  115. Csq->CsqReleaseLock(Csq, irql);
  116. return ;
  117. }
  118. IoMarkIrpPending(Irp);
  119. #endif
  120. cancelRoutine = IoSetCancelRoutine(Irp, IopCsqCancelRoutine);
  121. ASSERT(!cancelRoutine);
  122. if (Irp->Cancel) {
  123. cancelRoutine = IoSetCancelRoutine(Irp, NULL);
  124. if (cancelRoutine) {
  125. Csq->CsqRemoveIrp(Csq, Irp);
  126. if (Context) {
  127. Context->Irp = NULL;
  128. }
  129. Irp->Tail.Overlay.DriverContext[3] = NULL;
  130. Csq->CsqReleaseLock(Csq, irql);
  131. Csq->CsqCompleteCanceledIrp(Csq, Irp);
  132. } else {
  133. //
  134. // The cancel routine beat us to it.
  135. //
  136. Csq->CsqReleaseLock(Csq, irql);
  137. }
  138. } else {
  139. Csq->CsqReleaseLock(Csq, irql);
  140. }
  141. }
  142. PIRP
  143. CSQLIB_DDI(IoCsqRemoveNextIrp)(
  144. IN PIO_CSQ Csq,
  145. IN PVOID PeekContext
  146. )
  147. /*++
  148. Routine Description:
  149. This routine removes the next IRP from the queue. This routine will enumerate the queue
  150. and return an IRP that's not canceled. If an IRP in the queue is canceled it goes to the next
  151. IRP. If no IRP is available it returns a NULL. The IRP returned is safe and cannot be canceled.
  152. Arguments:
  153. Csq - Pointer to the cancel queue.
  154. Return Value:
  155. Returns the IRP or NULL.
  156. --*/
  157. {
  158. KIRQL irql;
  159. PIO_CSQ_IRP_CONTEXT context;
  160. PDRIVER_CANCEL cancelRoutine;
  161. PIRP irp;
  162. irp = NULL;
  163. Csq->ReservePointer = NULL; // Force drivers to be good citizens
  164. Csq->CsqAcquireLock(Csq, &irql);
  165. irp = Csq->CsqPeekNextIrp(Csq, NULL, PeekContext);
  166. while (1) {
  167. //
  168. // This routine will return a pointer to the next IRP in the queue adjacent to
  169. // the irp passed as a parameter. If the irp is NULL, it returns the IRP at the head of
  170. // the queue.
  171. //
  172. if (!irp) {
  173. Csq->CsqReleaseLock(Csq, irql);
  174. return NULL;
  175. }
  176. cancelRoutine = IoSetCancelRoutine(irp, NULL);
  177. if (!cancelRoutine) {
  178. irp = Csq->CsqPeekNextIrp(Csq, irp, PeekContext);
  179. continue;
  180. }
  181. Csq->CsqRemoveIrp(Csq, irp); // Remove this IRP from the queue
  182. break;
  183. }
  184. context = irp->Tail.Overlay.DriverContext[3];
  185. if (context->Type == IO_TYPE_CSQ_IRP_CONTEXT) {
  186. context->Irp = NULL;
  187. ASSERT(context->Csq == Csq);
  188. }
  189. irp->Tail.Overlay.DriverContext[3] = NULL;
  190. Csq->CsqReleaseLock(Csq, irql);
  191. return irp;
  192. }
  193. PIRP
  194. CSQLIB_DDI(IoCsqRemoveIrp)(
  195. IN PIO_CSQ Csq,
  196. IN PIO_CSQ_IRP_CONTEXT Context
  197. )
  198. /*++
  199. Routine Description:
  200. This routine removes the IRP that's associated with a context from the queue.
  201. It's expected that this routine will be called from a timer or DPC or other threads which complete an
  202. IO. Note that the IRP associated with this context could already have been freed.
  203. Arguments:
  204. Csq - Pointer to the cancel queue.
  205. Context - Context associated with Irp.
  206. Return Value:
  207. Returns the IRP associated with the context. If the value is not NULL, the IRP was successfully
  208. retrieved and can be used safely. If the value is NULL, the IRP was already canceled.
  209. --*/
  210. {
  211. KIRQL irql;
  212. PIRP irp;
  213. PDRIVER_CANCEL cancelRoutine;
  214. Csq->ReservePointer = NULL; // Force drivers to be good citizens
  215. Csq->CsqAcquireLock(Csq, &irql);
  216. if (Context->Irp ) {
  217. ASSERT(Context->Csq == Csq);
  218. irp = Context->Irp;
  219. cancelRoutine = IoSetCancelRoutine(irp, NULL);
  220. if (!cancelRoutine) {
  221. Csq->CsqReleaseLock(Csq, irql);
  222. return NULL;
  223. }
  224. ASSERT(Context == irp->Tail.Overlay.DriverContext[3]);
  225. Csq->CsqRemoveIrp(Csq, irp);
  226. //
  227. // Break the association.
  228. //
  229. Context->Irp = NULL;
  230. irp->Tail.Overlay.DriverContext[3] = NULL;
  231. ASSERT(Context->Csq == Csq);
  232. Csq->CsqReleaseLock(Csq, irql);
  233. return irp;
  234. } else {
  235. Csq->CsqReleaseLock(Csq, irql);
  236. return NULL;
  237. }
  238. }
  239. VOID
  240. IopCsqCancelRoutine(
  241. IN PDEVICE_OBJECT DeviceObject,
  242. IN PIRP Irp
  243. )
  244. /*++
  245. Routine Description:
  246. This routine removes the IRP that's associated with a context from the queue.
  247. It's expected that this routine will be called from a timer or DPC or other threads which complete an
  248. IO. Note that the IRP associated with this context could already have been freed.
  249. Arguments:
  250. Csq - Pointer to the cancel queue.
  251. Context - Context associated with Irp.
  252. Return Value:
  253. Returns the IRP associated with the context. If the value is not NULL, the IRP was successfully
  254. retrieved and can be used safely. If the value is NULL, the IRP was already canceled.
  255. --*/
  256. {
  257. KIRQL irql;
  258. PIO_CSQ_IRP_CONTEXT irpContext;
  259. PIO_CSQ cfq;
  260. UNREFERENCED_PARAMETER (DeviceObject);
  261. IoReleaseCancelSpinLock(Irp->CancelIrql);
  262. irpContext = Irp->Tail.Overlay.DriverContext[3];
  263. if (irpContext->Type == IO_TYPE_CSQ_IRP_CONTEXT) {
  264. cfq = irpContext->Csq;
  265. } else if (irpContext->Type == IO_TYPE_CSQ) {
  266. cfq = (PIO_CSQ)irpContext;
  267. } else {
  268. //
  269. // Bad type
  270. //
  271. ASSERT(0);
  272. return;
  273. }
  274. ASSERT(cfq);
  275. cfq->ReservePointer = NULL; // Force drivers to be good citizens
  276. cfq->CsqAcquireLock(cfq, &irql);
  277. cfq->CsqRemoveIrp(cfq, Irp);
  278. //
  279. // Break the association if necessary.
  280. //
  281. if (irpContext != (PIO_CSQ_IRP_CONTEXT)cfq) {
  282. irpContext->Irp = NULL;
  283. Irp->Tail.Overlay.DriverContext[3] = NULL;
  284. }
  285. cfq->CsqReleaseLock(cfq, irql);
  286. cfq->CsqCompleteCanceledIrp(cfq, Irp);
  287. }