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.

1034 lines
25 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. Close.c
  5. Abstract:
  6. This module implements the File Close routine for Cdfs called by the
  7. Fsd/Fsp dispatch routines.
  8. The close operation interacts with both the async and delayed close queues
  9. in the CdData structure. Since close may be called recursively we may
  10. violate the locking order in acquiring the Vcb or Fcb. In this case
  11. we may move the request to the async close queue. If this is the last
  12. reference on the Fcb and there is a chance the user may reopen this
  13. file again soon we would like to defer the close. In this case we
  14. may move the request to the async close queue.
  15. Once we are past the decode file operation there is no need for the
  16. file object. If we are moving the request to either of the work
  17. queues then we remember all of the information from the file object and
  18. complete the request with STATUS_SUCCESS. The Io system can then
  19. reuse the file object and we can complete the request when convenient.
  20. The async close queue consists of requests which we would like to
  21. complete as soon as possible. They are queued using the original
  22. IrpContext where some of the fields have been overwritten with
  23. information from the file object. We will extract this information,
  24. cleanup the IrpContext and then call the close worker routine.
  25. The delayed close queue consists of requests which we would like to
  26. defer the close for. We keep size of this list within a range
  27. determined by the size of the system. We let it grow to some maximum
  28. value and then shrink to some minimum value. We allocate a small
  29. structure which contains the key information from the file object
  30. and use this information along with an IrpContext on the stack
  31. to complete the request.
  32. // @@BEGIN_DDKSPLIT
  33. Author:
  34. Brian Andrew [BrianAn] 01-July-1995
  35. Revision History:
  36. // @@END_DDKSPLIT
  37. --*/
  38. #include "CdProcs.h"
  39. //
  40. // The Bug check file id for this module
  41. //
  42. #define BugCheckFileId (CDFS_BUG_CHECK_CLOSE)
  43. //
  44. // Local support routines
  45. //
  46. BOOLEAN
  47. CdCommonClosePrivate (
  48. IN PIRP_CONTEXT IrpContext,
  49. IN PVCB Vcb,
  50. IN PFCB Fcb,
  51. IN ULONG UserReference,
  52. IN BOOLEAN FromFsd
  53. );
  54. VOID
  55. CdQueueClose (
  56. IN PIRP_CONTEXT IrpContext,
  57. IN PFCB Fcb,
  58. IN ULONG UserReference,
  59. IN BOOLEAN DelayedClose
  60. );
  61. PIRP_CONTEXT
  62. CdRemoveClose (
  63. IN PVCB Vcb OPTIONAL
  64. );
  65. VOID
  66. CdCloseWorker (
  67. IN PDEVICE_OBJECT DeviceObject,
  68. IN PVOID Context
  69. );
  70. #ifdef ALLOC_PRAGMA
  71. #pragma alloc_text(PAGE, CdCommonClose)
  72. #pragma alloc_text(PAGE, CdCommonClosePrivate)
  73. #pragma alloc_text(PAGE, CdQueueClose)
  74. #pragma alloc_text(PAGE, CdRemoveClose)
  75. #pragma alloc_text(PAGE, CdCloseWorker)
  76. #endif
  77. VOID
  78. CdFspClose (
  79. IN PVCB Vcb OPTIONAL
  80. )
  81. /*++
  82. Routine Description:
  83. This routine is called to process the close queues in the CdData. If the
  84. Vcb is passed then we want to remove all of the closes for this Vcb.
  85. Otherwise we will do as many of the delayed closes as we need to do.
  86. Arguments:
  87. Vcb - If specified then we are looking for all of the closes for the
  88. given Vcb.
  89. Return Value:
  90. None
  91. --*/
  92. {
  93. PIRP_CONTEXT IrpContext;
  94. IRP_CONTEXT StackIrpContext;
  95. THREAD_CONTEXT ThreadContext;
  96. PFCB Fcb;
  97. ULONG UserReference;
  98. ULONG VcbHoldCount = 0;
  99. PVCB CurrentVcb = NULL;
  100. BOOLEAN PotentialVcbTeardown = FALSE;
  101. PAGED_CODE();
  102. FsRtlEnterFileSystem();
  103. //
  104. // Continue processing until there are no more closes to process.
  105. //
  106. while (IrpContext = CdRemoveClose( Vcb )) {
  107. //
  108. // If we don't have an IrpContext then use the one on the stack.
  109. // Initialize it for this request.
  110. //
  111. if (SafeNodeType( IrpContext ) != CDFS_NTC_IRP_CONTEXT ) {
  112. //
  113. // Update the local values from the IrpContextLite.
  114. //
  115. Fcb = ((PIRP_CONTEXT_LITE) IrpContext)->Fcb;
  116. UserReference = ((PIRP_CONTEXT_LITE) IrpContext)->UserReference;
  117. //
  118. // Update the stack irp context with the values from the
  119. // IrpContextLite.
  120. //
  121. CdInitializeStackIrpContext( &StackIrpContext,
  122. (PIRP_CONTEXT_LITE) IrpContext );
  123. //
  124. // Free the IrpContextLite.
  125. //
  126. CdFreeIrpContextLite( (PIRP_CONTEXT_LITE) IrpContext );
  127. //
  128. // Remember we have the IrpContext from the stack.
  129. //
  130. IrpContext = &StackIrpContext;
  131. //
  132. // Otherwise cleanup the existing IrpContext.
  133. //
  134. } else {
  135. //
  136. // Remember the Fcb and user reference count.
  137. //
  138. Fcb = (PFCB) IrpContext->Irp;
  139. IrpContext->Irp = NULL;
  140. UserReference = (ULONG) IrpContext->ExceptionStatus;
  141. IrpContext->ExceptionStatus = STATUS_SUCCESS;
  142. }
  143. //
  144. // We have an IrpContext. Now we need to set the top level thread
  145. // context.
  146. //
  147. SetFlag( IrpContext->Flags, IRP_CONTEXT_FSP_FLAGS );
  148. //
  149. // If we were given a Vcb then there is a request on top of this.
  150. //
  151. if (ARGUMENT_PRESENT( Vcb )) {
  152. ClearFlag( IrpContext->Flags,
  153. IRP_CONTEXT_FLAG_TOP_LEVEL | IRP_CONTEXT_FLAG_TOP_LEVEL_CDFS );
  154. }
  155. CdSetThreadContext( IrpContext, &ThreadContext );
  156. //
  157. // If we have hit the maximum number of requests to process without
  158. // releasing the Vcb then release the Vcb now. If we are holding
  159. // a different Vcb to this one then release the previous Vcb.
  160. //
  161. // In either case acquire the current Vcb.
  162. //
  163. // We use the MinDelayedCloseCount from the CdData since it is
  164. // a convenient value based on the system size. Only thing we are trying
  165. // to do here is prevent this routine starving other threads which
  166. // may need this Vcb exclusively.
  167. //
  168. // Note that the check for potential teardown below is unsafe. We'll
  169. // repeat later within the cddata lock.
  170. //
  171. PotentialVcbTeardown = !ARGUMENT_PRESENT( Vcb ) &&
  172. (Fcb->Vcb->VcbCondition != VcbMounted) &&
  173. (Fcb->Vcb->VcbCondition != VcbMountInProgress) &&
  174. (Fcb->Vcb->VcbCleanup == 0);
  175. if (PotentialVcbTeardown ||
  176. (VcbHoldCount > CdData.MinDelayedCloseCount) ||
  177. (Fcb->Vcb != CurrentVcb)) {
  178. if (CurrentVcb != NULL) {
  179. CdReleaseVcb( IrpContext, CurrentVcb );
  180. }
  181. if (PotentialVcbTeardown) {
  182. CdAcquireCdData( IrpContext );
  183. //
  184. // Repeat the checks with global lock held. The volume could have
  185. // been remounted while we didn't hold the lock.
  186. //
  187. PotentialVcbTeardown = !ARGUMENT_PRESENT( Vcb ) &&
  188. (Fcb->Vcb->VcbCondition != VcbMounted) &&
  189. (Fcb->Vcb->VcbCondition != VcbMountInProgress) &&
  190. (Fcb->Vcb->VcbCleanup == 0);
  191. if (!PotentialVcbTeardown) {
  192. CdReleaseCdData( IrpContext);
  193. }
  194. }
  195. CurrentVcb = Fcb->Vcb;
  196. CdAcquireVcbShared( IrpContext, CurrentVcb, FALSE );
  197. VcbHoldCount = 0;
  198. } else {
  199. VcbHoldCount += 1;
  200. }
  201. //
  202. // Call our worker routine to perform the close operation.
  203. //
  204. CdCommonClosePrivate( IrpContext, CurrentVcb, Fcb, UserReference, FALSE );
  205. //
  206. // If the reference count on this Vcb is below our residual reference
  207. // then check if we should dismount the volume.
  208. //
  209. if (PotentialVcbTeardown) {
  210. CdReleaseVcb( IrpContext, CurrentVcb );
  211. CdCheckForDismount( IrpContext, CurrentVcb, FALSE );
  212. CurrentVcb = NULL;
  213. CdReleaseCdData( IrpContext );
  214. PotentialVcbTeardown = FALSE;
  215. }
  216. //
  217. // Complete the current request to cleanup the IrpContext.
  218. //
  219. CdCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  220. }
  221. //
  222. // Release any Vcb we may still hold.
  223. //
  224. if (CurrentVcb != NULL) {
  225. CdReleaseVcb( IrpContext, CurrentVcb );
  226. }
  227. FsRtlExitFileSystem();
  228. }
  229. NTSTATUS
  230. CdCommonClose (
  231. IN PIRP_CONTEXT IrpContext,
  232. IN PIRP Irp
  233. )
  234. /*++
  235. Routine Description:
  236. This routine is the Fsd entry for the close operation. We decode the file
  237. object to find the CDFS structures and type of open. We call our internal
  238. worker routine to perform the actual work. If the work wasn't completed
  239. then we post to one of our worker queues. The Ccb isn't needed after this
  240. point so we delete the Ccb and return STATUS_SUCCESS to our caller in all
  241. cases.
  242. Arguments:
  243. Irp - Supplies the Irp to process
  244. Return Value:
  245. STATUS_SUCCESS
  246. --*/
  247. {
  248. TYPE_OF_OPEN TypeOfOpen;
  249. PVCB Vcb;
  250. PFCB Fcb;
  251. PCCB Ccb;
  252. ULONG UserReference = 0;
  253. BOOLEAN PotentialVcbTeardown = FALSE;
  254. BOOLEAN ForceDismount = FALSE;
  255. PAGED_CODE();
  256. ASSERT_IRP_CONTEXT( IrpContext );
  257. ASSERT_IRP( Irp );
  258. //
  259. // If we were called with our file system device object instead of a
  260. // volume device object, just complete this request with STATUS_SUCCESS.
  261. //
  262. if (IrpContext->Vcb == NULL) {
  263. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  264. return STATUS_SUCCESS;
  265. }
  266. //
  267. // Decode the file object to get the type of open and Fcb/Ccb.
  268. //
  269. TypeOfOpen = CdDecodeFileObject( IrpContext,
  270. IoGetCurrentIrpStackLocation( Irp )->FileObject,
  271. &Fcb,
  272. &Ccb );
  273. //
  274. // No work to do for unopened file objects.
  275. //
  276. if (TypeOfOpen == UnopenedFileObject) {
  277. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  278. return STATUS_SUCCESS;
  279. }
  280. Vcb = Fcb->Vcb;
  281. //
  282. // Clean up any CCB associated with this open.
  283. //
  284. if (Ccb != NULL) {
  285. UserReference = 1;
  286. //
  287. // Was a FSCTL_DISMOUNT issued on this handle? If so, we need to
  288. // force a dismount of the volume now.
  289. //
  290. ForceDismount = BooleanFlagOn( Ccb->Flags, CCB_FLAG_DISMOUNT_ON_CLOSE);
  291. //
  292. // We can always deallocate the Ccb if present.
  293. //
  294. CdDeleteCcb( IrpContext, Ccb );
  295. }
  296. //
  297. // If this is the last reference to a user file or directory on a
  298. // currently mounted volume, then post it to the delayed close queue. Note
  299. // that the VcbCondition check is unsafe, but it doesn't really matter -
  300. // we just might delay the volume teardown a little by posting this close.
  301. //
  302. if ((Vcb->VcbCondition == VcbMounted) &&
  303. (Fcb->FcbReference == 1) &&
  304. ((TypeOfOpen == UserFileOpen) ||
  305. (TypeOfOpen == UserDirectoryOpen))) {
  306. CdQueueClose( IrpContext, Fcb, UserReference, TRUE );
  307. IrpContext = NULL;
  308. //
  309. // Otherwise try to process this close. Post to the async close queue
  310. // if we can't acquire all of the resources.
  311. //
  312. } else {
  313. //
  314. // If we may be dismounting this volume then acquire the CdData
  315. // resource.
  316. //
  317. // Since we now must make volumes go away as soon as reasonable after
  318. // the last user handles closes, key off of the cleanup count. It is
  319. // OK to do this more than neccesary. Since this Fcb could be holding
  320. // a number of other Fcbs (and thus their references), a simple check
  321. // on reference count is not appropriate.
  322. //
  323. // Do an unsafe check first to avoid taking the (global) cddata lock in the
  324. // common case.
  325. //
  326. if (((Vcb->VcbCleanup == 0) || ForceDismount) &&
  327. (Vcb->VcbCondition != VcbMounted)) {
  328. //
  329. // Possible. Acquire CdData to synchronise with the remount path, and
  330. // then repeat the tests.
  331. //
  332. // Note that we must send the notification outside of any locks, since
  333. // the worker that processes the notify could also be calling into our
  334. // pnp path which wants both CdData and VcbResource. For a force dismount
  335. // the volume will be marked invalid (no going back), so we will definitely
  336. // go ahead and dismount below.
  337. //
  338. if (ForceDismount) {
  339. //
  340. // Send notification.
  341. //
  342. FsRtlNotifyVolumeEvent( IoGetCurrentIrpStackLocation( Irp )->FileObject,
  343. FSRTL_VOLUME_DISMOUNT );
  344. }
  345. CdAcquireCdData( IrpContext );
  346. if (((Vcb->VcbCleanup == 0) || ForceDismount) &&
  347. (Vcb->VcbCondition != VcbMounted) &&
  348. (Vcb->VcbCondition != VcbMountInProgress) &&
  349. FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL_CDFS )) {
  350. PotentialVcbTeardown = TRUE;
  351. }
  352. else {
  353. //
  354. // We can't dismount this volume now, there are other references or
  355. // it's just been remounted.
  356. //
  357. CdReleaseCdData( IrpContext);
  358. }
  359. }
  360. if (ForceDismount) {
  361. //
  362. // Physically disconnect this Vcb from the device so a new mount can
  363. // occur. Vcb deletion cannot happen at this time since there is
  364. // a handle on it associated with this very request, but we'll call
  365. // check for dismount again later anyway.
  366. //
  367. CdCheckForDismount( IrpContext, Vcb, TRUE );
  368. }
  369. //
  370. // Call the worker routine to perform the actual work. This routine
  371. // should never raise except for a fatal error.
  372. //
  373. if (!CdCommonClosePrivate( IrpContext, Vcb, Fcb, UserReference, TRUE )) {
  374. //
  375. // If we didn't complete the request then post the request as needed.
  376. //
  377. CdQueueClose( IrpContext, Fcb, UserReference, FALSE );
  378. IrpContext = NULL;
  379. //
  380. // Check whether we should be dismounting the volume and then complete
  381. // the request.
  382. //
  383. } else if (PotentialVcbTeardown) {
  384. CdCheckForDismount( IrpContext, Vcb, FALSE );
  385. }
  386. }
  387. //
  388. // Always complete this request with STATUS_SUCCESS.
  389. //
  390. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  391. if (PotentialVcbTeardown) {
  392. CdReleaseCdData( IrpContext );
  393. }
  394. //
  395. // Always return STATUS_SUCCESS for closes.
  396. //
  397. return STATUS_SUCCESS;
  398. }
  399. //
  400. // Local support routine
  401. //
  402. BOOLEAN
  403. CdCommonClosePrivate (
  404. IN PIRP_CONTEXT IrpContext,
  405. IN PVCB Vcb,
  406. IN PFCB Fcb,
  407. IN ULONG UserReference,
  408. IN BOOLEAN FromFsd
  409. )
  410. /*++
  411. Routine Description:
  412. This is the worker routine for the close operation. We can be called in
  413. an Fsd thread or from a worker Fsp thread. If called from the Fsd thread
  414. then we acquire the resources without waiting. Otherwise we know it is
  415. safe to wait.
  416. We check to see whether we should post this request to the delayed close
  417. queue. If we are to process the close here then we acquire the Vcb and
  418. Fcb. We will adjust the counts and call our teardown routine to see
  419. if any of the structures should go away.
  420. Arguments:
  421. Vcb - Vcb for this volume.
  422. Fcb - Fcb for this request.
  423. UserReference - Number of user references for this file object. This is
  424. zero for an internal stream.
  425. FromFsd - This request was called from an Fsd thread. Indicates whether
  426. we should wait to acquire resources.
  427. DelayedClose - Address to store whether we should try to put this on
  428. the delayed close queue. Ignored if this routine can process this
  429. close.
  430. Return Value:
  431. BOOLEAN - TRUE if this thread processed the close, FALSE otherwise.
  432. --*/
  433. {
  434. BOOLEAN RemovedFcb;
  435. PAGED_CODE();
  436. ASSERT_IRP_CONTEXT( IrpContext );
  437. ASSERT_FCB( Fcb );
  438. //
  439. // Try to acquire the Vcb and Fcb. If we can't acquire them then return
  440. // and let our caller know he should post the request to the async
  441. // queue.
  442. //
  443. if (CdAcquireVcbShared( IrpContext, Vcb, FromFsd )) {
  444. if (!CdAcquireFcbExclusive( IrpContext, Fcb, FromFsd )) {
  445. //
  446. // We couldn't get the Fcb. Release the Vcb and let our caller
  447. // know to post this request.
  448. //
  449. CdReleaseVcb( IrpContext, Vcb );
  450. return FALSE;
  451. }
  452. //
  453. // We didn't get the Vcb. Let our caller know to post this request.
  454. //
  455. } else {
  456. return FALSE;
  457. }
  458. //
  459. // Lock the Vcb and decrement the reference counts.
  460. //
  461. CdLockVcb( IrpContext, Vcb );
  462. CdDecrementReferenceCounts( IrpContext, Fcb, 1, UserReference );
  463. CdUnlockVcb( IrpContext, Vcb );
  464. //
  465. // Call our teardown routine to see if this object can go away.
  466. // If we don't remove the Fcb then release it.
  467. //
  468. CdTeardownStructures( IrpContext, Fcb, &RemovedFcb );
  469. if (!RemovedFcb) {
  470. CdReleaseFcb( IrpContext, Fcb );
  471. }
  472. //
  473. // Release the Vcb and return to our caller. Let him know we completed
  474. // this request.
  475. //
  476. CdReleaseVcb( IrpContext, Vcb );
  477. return TRUE;
  478. }
  479. VOID
  480. CdCloseWorker (
  481. IN PDEVICE_OBJECT DeviceObject,
  482. IN PVOID Context
  483. )
  484. /*++
  485. Routine Description:
  486. Worker routine to call CsFspClose.
  487. Arguments:
  488. DeviceObject - Filesystem registration device object
  489. Context - Callers context
  490. Return Value:
  491. None
  492. --*/
  493. {
  494. CdFspClose (NULL);
  495. }
  496. VOID
  497. CdQueueClose (
  498. IN PIRP_CONTEXT IrpContext,
  499. IN PFCB Fcb,
  500. IN ULONG UserReference,
  501. IN BOOLEAN DelayedClose
  502. )
  503. /*++
  504. Routine Description:
  505. This routine is called to queue a request to either the async or delayed
  506. close queue. For the delayed queue we need to allocate a smaller
  507. structure to contain the information about the file object. We do
  508. that so we don't put the larger IrpContext structures into this long
  509. lived queue. If we can allocate this structure then we put this
  510. on the async queue instead.
  511. Arguments:
  512. Fcb - Fcb for this file object.
  513. UserReference - Number of user references for this file object. This is
  514. zero for an internal stream.
  515. DelayedClose - Indicates whether this should go on the async or delayed
  516. close queue.
  517. Return Value:
  518. None
  519. --*/
  520. {
  521. PIRP_CONTEXT_LITE IrpContextLite = NULL;
  522. BOOLEAN StartWorker = FALSE;
  523. PAGED_CODE();
  524. ASSERT_IRP_CONTEXT( IrpContext );
  525. ASSERT_FCB( Fcb );
  526. //
  527. // Start with the delayed queue request. We can move this to the async
  528. // queue if there is an allocation failure.
  529. //
  530. if (DelayedClose) {
  531. //
  532. // Try to allocate non-paged pool for the IRP_CONTEXT_LITE.
  533. //
  534. IrpContextLite = CdCreateIrpContextLite( IrpContext );
  535. }
  536. //
  537. // We want to clear the top level context in this thread if
  538. // necessary. Call our cleanup routine to do the work.
  539. //
  540. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_MORE_PROCESSING );
  541. CdCleanupIrpContext( IrpContext, TRUE );
  542. //
  543. // Synchronize with the CdData lock.
  544. //
  545. CdLockCdData();
  546. //
  547. // If we have an IrpContext then put the request on the delayed close queue.
  548. //
  549. if (IrpContextLite != NULL) {
  550. //
  551. // Initialize the IrpContextLite.
  552. //
  553. IrpContextLite->NodeTypeCode = CDFS_NTC_IRP_CONTEXT_LITE;
  554. IrpContextLite->NodeByteSize = sizeof( IRP_CONTEXT_LITE );
  555. IrpContextLite->Fcb = Fcb;
  556. IrpContextLite->UserReference = UserReference;
  557. IrpContextLite->RealDevice = IrpContext->RealDevice;
  558. //
  559. // Add this to the delayed close list and increment
  560. // the count.
  561. //
  562. InsertTailList( &CdData.DelayedCloseQueue,
  563. &IrpContextLite->DelayedCloseLinks );
  564. CdData.DelayedCloseCount += 1;
  565. //
  566. // If we are above our threshold then start the delayed
  567. // close operation.
  568. //
  569. if (CdData.DelayedCloseCount > CdData.MaxDelayedCloseCount) {
  570. CdData.ReduceDelayedClose = TRUE;
  571. if (!CdData.FspCloseActive) {
  572. CdData.FspCloseActive = TRUE;
  573. StartWorker = TRUE;
  574. }
  575. }
  576. //
  577. // Unlock the CdData.
  578. //
  579. CdUnlockCdData();
  580. //
  581. // Cleanup the IrpContext.
  582. //
  583. CdCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  584. //
  585. // Otherwise drop into the async case below.
  586. //
  587. } else {
  588. //
  589. // Store the information about the file object into the IrpContext.
  590. //
  591. IrpContext->Irp = (PIRP) Fcb;
  592. IrpContext->ExceptionStatus = (NTSTATUS) UserReference;
  593. //
  594. // Add this to the async close list and increment the count.
  595. //
  596. InsertTailList( &CdData.AsyncCloseQueue,
  597. &IrpContext->WorkQueueItem.List );
  598. CdData.AsyncCloseCount += 1;
  599. //
  600. // Remember to start the Fsp close thread if not currently started.
  601. //
  602. if (!CdData.FspCloseActive) {
  603. CdData.FspCloseActive = TRUE;
  604. StartWorker = TRUE;
  605. }
  606. //
  607. // Unlock the CdData.
  608. //
  609. CdUnlockCdData();
  610. }
  611. //
  612. // Start the FspClose thread if we need to.
  613. //
  614. if (StartWorker) {
  615. IoQueueWorkItem( CdData.CloseItem, CdCloseWorker, CriticalWorkQueue, NULL );
  616. }
  617. //
  618. // Return to our caller.
  619. //
  620. return;
  621. }
  622. //
  623. // Local support routine
  624. //
  625. PIRP_CONTEXT
  626. CdRemoveClose (
  627. IN PVCB Vcb OPTIONAL
  628. )
  629. /*++
  630. Routine Description:
  631. Arguments:
  632. This routine is called to scan the async and delayed close queues looking
  633. for a suitable entry. If the Vcb is specified then we scan both queues
  634. looking for an entry with the same Vcb. Otherwise we will look in the
  635. async queue first for any close item. If none found there then we look
  636. in the delayed close queue provided that we have triggered the delayed
  637. close operation.
  638. Return Value:
  639. PIRP_CONTEXT - NULL if no work item found. Otherwise it is the pointer to
  640. either the IrpContext or IrpContextLite for this request.
  641. --*/
  642. {
  643. PIRP_CONTEXT IrpContext = NULL;
  644. PIRP_CONTEXT NextIrpContext;
  645. PIRP_CONTEXT_LITE NextIrpContextLite;
  646. PLIST_ENTRY Entry;
  647. PAGED_CODE();
  648. ASSERT_OPTIONAL_VCB( Vcb );
  649. //
  650. // Lock the CdData to perform the scan.
  651. //
  652. CdLockCdData();
  653. //
  654. // First check the list of async closes.
  655. //
  656. Entry = CdData.AsyncCloseQueue.Flink;
  657. while (Entry != &CdData.AsyncCloseQueue) {
  658. //
  659. // Extract the IrpContext.
  660. //
  661. NextIrpContext = CONTAINING_RECORD( Entry,
  662. IRP_CONTEXT,
  663. WorkQueueItem.List );
  664. //
  665. // If no Vcb was specified or this Vcb is for our volume
  666. // then perform the close.
  667. //
  668. if (!ARGUMENT_PRESENT( Vcb ) || (NextIrpContext->Vcb == Vcb)) {
  669. RemoveEntryList( Entry );
  670. CdData.AsyncCloseCount -= 1;
  671. IrpContext = NextIrpContext;
  672. break;
  673. }
  674. //
  675. // Move to the next entry.
  676. //
  677. Entry = Entry->Flink;
  678. }
  679. //
  680. // If we didn't find anything look through the delayed close
  681. // queue.
  682. //
  683. // We will only check the delayed close queue if we were given
  684. // a Vcb or the delayed close operation is active.
  685. //
  686. if ((IrpContext == NULL) &&
  687. (ARGUMENT_PRESENT( Vcb ) ||
  688. (CdData.ReduceDelayedClose &&
  689. (CdData.DelayedCloseCount > CdData.MinDelayedCloseCount)))) {
  690. Entry = CdData.DelayedCloseQueue.Flink;
  691. while (Entry != &CdData.DelayedCloseQueue) {
  692. //
  693. // Extract the IrpContext.
  694. //
  695. NextIrpContextLite = CONTAINING_RECORD( Entry,
  696. IRP_CONTEXT_LITE,
  697. DelayedCloseLinks );
  698. //
  699. // If no Vcb was specified or this Vcb is for our volume
  700. // then perform the close.
  701. //
  702. if (!ARGUMENT_PRESENT( Vcb ) || (NextIrpContextLite->Fcb->Vcb == Vcb)) {
  703. RemoveEntryList( Entry );
  704. CdData.DelayedCloseCount -= 1;
  705. IrpContext = (PIRP_CONTEXT) NextIrpContextLite;
  706. break;
  707. }
  708. //
  709. // Move to the next entry.
  710. //
  711. Entry = Entry->Flink;
  712. }
  713. }
  714. //
  715. // If the Vcb wasn't specified and we couldn't find an entry
  716. // then turn off the Fsp thread.
  717. //
  718. if (!ARGUMENT_PRESENT( Vcb ) && (IrpContext == NULL)) {
  719. CdData.FspCloseActive = FALSE;
  720. CdData.ReduceDelayedClose = FALSE;
  721. }
  722. //
  723. // Unlock the CdData.
  724. //
  725. CdUnlockCdData();
  726. return IrpContext;
  727. }