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.

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