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.

397 lines
10 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. cancel.c
  5. Abstract:
  6. This module implements the support for cancelling operations in the RDBSS driver
  7. Author:
  8. Balan Sethu Raman [SethuR] 7-June-95
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. //
  14. // The debug trace level
  15. //
  16. #define Dbg (DEBUG_TRACE_CANCEL)
  17. NTSTATUS
  18. RxSetMinirdrCancelRoutine(
  19. IN OUT PRX_CONTEXT RxContext,
  20. IN PMRX_CALLDOWN MRxCancelRoutine)
  21. /*++
  22. Routine Description:
  23. The routine sets up a mini rdr cancel routine for an RxContext.
  24. Arguments:
  25. RxContext - the context
  26. MRxCancelRoutine - the cancel routine
  27. Return Value:
  28. None.
  29. Notes:
  30. --*/
  31. {
  32. NTSTATUS Status;
  33. KIRQL SavedIrql;
  34. KeAcquireSpinLock( &RxStrucSupSpinLock, &SavedIrql );
  35. if (!FlagOn(RxContext->Flags,RX_CONTEXT_FLAG_CANCELLED)) {
  36. RxContext->MRxCancelRoutine = MRxCancelRoutine;
  37. Status = STATUS_SUCCESS;
  38. } else {
  39. Status = STATUS_CANCELLED;
  40. }
  41. KeReleaseSpinLock( &RxStrucSupSpinLock, SavedIrql );
  42. return Status;
  43. }
  44. VOID
  45. RxpCancelRoutine(
  46. PRX_CONTEXT pRxContext)
  47. /*++
  48. Routine Description:
  49. This routine is invoked to the underlying mini rdr cancel routine at a non
  50. DPC level
  51. Arguments:
  52. RxContext - the context
  53. Return Value:
  54. None.
  55. Notes:
  56. --*/
  57. {
  58. PMRX_CALLDOWN MRxCancelRoutine;
  59. PAGED_CODE();
  60. MRxCancelRoutine = pRxContext->MRxCancelRoutine;
  61. if (MRxCancelRoutine != NULL) {
  62. (MRxCancelRoutine)(pRxContext);
  63. } else {
  64. if (!RxCancelOperationInOverflowQueue(pRxContext)) {
  65. //RxCancelBlockingOperation(pRxContext);
  66. }
  67. }
  68. RxLog((
  69. "Dec RxC %lx L %ld %lx\n",
  70. pRxContext,__LINE__,pRxContext->ReferenceCount));
  71. RxWmiLog(LOG,
  72. RxpCancelRoutine,
  73. LOGPTR(pRxContext)
  74. LOGULONG(pRxContext->ReferenceCount));
  75. RxDereferenceAndDeleteRxContext(pRxContext);
  76. }
  77. VOID
  78. RxCancelRoutine(
  79. PDEVICE_OBJECT pDeviceObject,
  80. PIRP pIrp)
  81. /*++
  82. Routine Description:
  83. The cancel routine for RDBSS
  84. Arguments:
  85. pDeviceObject - the device object
  86. pIrp - the IRP to be cancelled
  87. Return Value:
  88. None.
  89. Notes:
  90. Any request can be in one of three states in RDBSS ---
  91. 1) the request is being processed.
  92. 2) the request is waiting for the results of a calldown to a minirdr.
  93. 3) the request is waiting on a resource.
  94. Any request that has been accepted by RDBSS ( the corresponding RxContext has been
  95. created and the cancel routine has been set ) is a valid target for cancel. The RDBSS
  96. driver does not make any effort to cancel requests that are in state (1) as described
  97. above.
  98. The cancelling action is invoked on those requests which are either in state (2),
  99. state(3) or when it is about to transition to/from either of these states.
  100. In order to expedite cancelling a similar strategy needs to be in place for the mini
  101. redirectors. This is provided by enabling the mini redirectors to register a cancel routine
  102. and rreserving fields in the RxContext for the mini redirectors to store the state information.
  103. As part of the RDBSS cancel routine the following steps will be taken .....
  104. 1) Locate the RxContext corresponding to the given IRP.
  105. 2) If the RxContext is waiting for a minirdr invoke the appropriate cancel routine.
  106. Note that the request is not immediately completed in all cases. This implies that there
  107. will be latencies in cancelling a request. The goal is to minimise the latency rather
  108. than completely eliminate them.
  109. --*/
  110. {
  111. KIRQL SavedIrql;
  112. PRX_CONTEXT pRxContext;
  113. PLIST_ENTRY pListEntry;
  114. // Locate the context corresponding to the given Irp.
  115. KeAcquireSpinLock( &RxStrucSupSpinLock, &SavedIrql );
  116. pListEntry = RxActiveContexts.Flink;
  117. while (pListEntry != &RxActiveContexts) {
  118. pRxContext = CONTAINING_RECORD(pListEntry,RX_CONTEXT,ContextListEntry);
  119. if (pRxContext->CurrentIrp == pIrp) {
  120. break;
  121. } else {
  122. pListEntry = pListEntry->Flink;
  123. }
  124. }
  125. if ((pListEntry != &RxActiveContexts) &&
  126. !FlagOn(pRxContext->Flags,RX_CONTEXT_FLAG_CANCELLED)) {
  127. SetFlag( pRxContext->Flags, RX_CONTEXT_FLAG_CANCELLED );
  128. InterlockedIncrement(&pRxContext->ReferenceCount);
  129. } else {
  130. pRxContext = NULL;
  131. }
  132. KeReleaseSpinLock( &RxStrucSupSpinLock, SavedIrql );
  133. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  134. if (pRxContext != NULL) {
  135. if (!RxShouldPostCompletion()) {
  136. RxpCancelRoutine(pRxContext);
  137. } else {
  138. RxDispatchToWorkerThread(
  139. RxFileSystemDeviceObject,
  140. CriticalWorkQueue,
  141. RxpCancelRoutine,
  142. pRxContext);
  143. }
  144. }
  145. }
  146. NTSTATUS
  147. RxCancelNotifyChangeDirectoryRequestsForVNetRoot(
  148. PV_NET_ROOT pVNetRoot,
  149. BOOLEAN ForceFilesClosed
  150. )
  151. /*++
  152. Routine Description:
  153. This routine cancels all the outstanding requests for a given instance of V_NET_ROOT. The
  154. V_NET_ROOT's are created/deleted are independent of the files that are opened/manipulated
  155. in them. Therefore it is imperative that when a delete operation is attempted
  156. all the outstanding requests are cancelled.
  157. Arguments:
  158. pVNetRoot - the V_NET_ROOT instance about to be deleted
  159. ForceFilesClosed - if true force close, otherwise fai if opens exist
  160. Return Value:
  161. None.
  162. Notes:
  163. --*/
  164. {
  165. KIRQL SavedIrql;
  166. PRX_CONTEXT pRxContext;
  167. PLIST_ENTRY pListEntry;
  168. LIST_ENTRY CancelledContexts;
  169. PMRX_CALLDOWN MRxCancelRoutine;
  170. NTSTATUS Status = STATUS_SUCCESS;
  171. InitializeListHead(&CancelledContexts);
  172. // Locate the context corresponding to the given Irp.
  173. KeAcquireSpinLock( &RxStrucSupSpinLock, &SavedIrql );
  174. pListEntry = RxActiveContexts.Flink;
  175. while (pListEntry != &RxActiveContexts) {
  176. pRxContext = CONTAINING_RECORD(pListEntry,RX_CONTEXT,ContextListEntry);
  177. pListEntry = pListEntry->Flink;
  178. if ((pRxContext->MajorFunction == IRP_MJ_DIRECTORY_CONTROL) &&
  179. (pRxContext->MinorFunction == IRP_MN_NOTIFY_CHANGE_DIRECTORY) &&
  180. (pRxContext->pFcb != NULL) &&
  181. (pRxContext->NotifyChangeDirectory.pVNetRoot == (PMRX_V_NET_ROOT)pVNetRoot) &&
  182. (pRxContext->MRxCancelRoutine != NULL)) {
  183. if (!ForceFilesClosed)
  184. {
  185. Status = STATUS_FILES_OPEN;
  186. break;
  187. }
  188. SetFlag( pRxContext->Flags, RX_CONTEXT_FLAG_CANCELLED );
  189. RemoveEntryList(&pRxContext->ContextListEntry);
  190. InsertTailList(&CancelledContexts,&pRxContext->ContextListEntry);
  191. InterlockedIncrement(&pRxContext->ReferenceCount);
  192. }
  193. }
  194. KeReleaseSpinLock( &RxStrucSupSpinLock, SavedIrql );
  195. if (Status == STATUS_SUCCESS)
  196. {
  197. while (!IsListEmpty(&CancelledContexts)) {
  198. pListEntry = RemoveHeadList(&CancelledContexts);
  199. InitializeListHead(pListEntry);
  200. pRxContext = CONTAINING_RECORD(pListEntry,RX_CONTEXT,ContextListEntry);
  201. //check to see if this guy is already completed..........if so, don't call down.
  202. if (pRxContext->CurrentIrp != NULL) {
  203. MRxCancelRoutine = pRxContext->MRxCancelRoutine;
  204. pRxContext->MRxCancelRoutine = NULL;
  205. ASSERT(MRxCancelRoutine != NULL);
  206. RxLog(("CCtx %lx CRtn %lx\n",pRxContext,MRxCancelRoutine));
  207. RxWmiLog(LOG,
  208. RxCancelNotifyChangeDirectoryRequestsForVNetRoot,
  209. LOGPTR(pRxContext)
  210. LOGPTR(MRxCancelRoutine));
  211. (MRxCancelRoutine)(pRxContext);
  212. }
  213. RxDereferenceAndDeleteRxContext(pRxContext);
  214. }
  215. }
  216. return Status;
  217. }
  218. VOID
  219. RxCancelNotifyChangeDirectoryRequestsForFobx(
  220. PFOBX pFobx)
  221. /*++
  222. Routine Description:
  223. This routine cancels all the outstanding requests for a given FileObject This
  224. handles the case when a directory handle is closed while it has outstanding
  225. change notify requests pending.
  226. Arguments:
  227. pFobx - the FOBX instance about to be closed
  228. Return Value:
  229. None.
  230. Notes:
  231. --*/
  232. {
  233. KIRQL SavedIrql;
  234. PRX_CONTEXT pRxContext;
  235. PLIST_ENTRY pListEntry;
  236. LIST_ENTRY CancelledContexts;
  237. PMRX_CALLDOWN MRxCancelRoutine;
  238. InitializeListHead(&CancelledContexts);
  239. // Locate the context corresponding to the given Irp.
  240. KeAcquireSpinLock( &RxStrucSupSpinLock, &SavedIrql );
  241. pListEntry = RxActiveContexts.Flink;
  242. while (pListEntry != &RxActiveContexts) {
  243. pRxContext = CONTAINING_RECORD(pListEntry,RX_CONTEXT,ContextListEntry);
  244. pListEntry = pListEntry->Flink;
  245. if ((pRxContext->MajorFunction == IRP_MJ_DIRECTORY_CONTROL) &&
  246. (pRxContext->MinorFunction == IRP_MN_NOTIFY_CHANGE_DIRECTORY) &&
  247. (pRxContext->pFobx == (PMRX_FOBX)pFobx) &&
  248. (pRxContext->MRxCancelRoutine != NULL)) {
  249. SetFlag( pRxContext->Flags, RX_CONTEXT_FLAG_CANCELLED );
  250. RemoveEntryList(&pRxContext->ContextListEntry);
  251. InsertTailList(&CancelledContexts,&pRxContext->ContextListEntry);
  252. InterlockedIncrement(&pRxContext->ReferenceCount);
  253. }
  254. }
  255. KeReleaseSpinLock( &RxStrucSupSpinLock, SavedIrql );
  256. while (!IsListEmpty(&CancelledContexts)) {
  257. pListEntry = RemoveHeadList(&CancelledContexts);
  258. InitializeListHead(pListEntry);
  259. pRxContext = CONTAINING_RECORD(pListEntry,RX_CONTEXT,ContextListEntry);
  260. // check to see if this IRP is already completed..........if so,
  261. // don't call down.
  262. if (pRxContext->CurrentIrp != NULL) {
  263. MRxCancelRoutine = pRxContext->MRxCancelRoutine;
  264. pRxContext->MRxCancelRoutine = NULL;
  265. ASSERT(MRxCancelRoutine != NULL);
  266. RxLog(("CCtx %lx CRtn %lx\n",pRxContext,MRxCancelRoutine));
  267. RxWmiLog(LOG,
  268. RxCancelNotifyChangeDirectoryRequestsForFobx,
  269. LOGPTR(pRxContext)
  270. LOGPTR(MRxCancelRoutine));
  271. (MRxCancelRoutine)(pRxContext);
  272. }
  273. RxDereferenceAndDeleteRxContext(pRxContext);
  274. }
  275. }
  276.