3 Copyright (c) 1989-2000 Microsoft Corporation
11 This module implements the File Close routine for Cdfs called by the
12 Fsd/Fsp dispatch routines.
14 The close operation interacts with both the async and delayed close queues
15 in the CdData structure. Since close may be called recursively we may
16 violate the locking order in acquiring the Vcb or Fcb. In this case
17 we may move the request to the async close queue. If this is the last
18 reference on the Fcb and there is a chance the user may reopen this
19 file again soon we would like to defer the close. In this case we
20 may move the request to the async close queue.
22 Once we are past the decode file operation there is no need for the
23 file object. If we are moving the request to either of the work
24 queues then we remember all of the information from the file object and
25 complete the request with STATUS_SUCCESS. The Io system can then
26 reuse the file object and we can complete the request when convenient.
28 The async close queue consists of requests which we would like to
29 complete as soon as possible. They are queued using the original
30 IrpContext where some of the fields have been overwritten with
31 information from the file object. We will extract this information,
32 cleanup the IrpContext and then call the close worker routine.
34 The delayed close queue consists of requests which we would like to
35 defer the close for. We keep size of this list within a range
36 determined by the size of the system. We let it grow to some maximum
37 value and then shrink to some minimum value. We allocate a small
38 structure which contains the key information from the file object
39 and use this information along with an IrpContext on the stack
40 to complete the request.
48 // The Bug check file id for this module
51 #define BugCheckFileId (CDFS_BUG_CHECK_CLOSE)
54 // Local support routines
57 _Requires_lock_held_(_Global_critical_region_
)
59 CdCommonClosePrivate (
60 _In_ PIRP_CONTEXT IrpContext
,
63 _In_ ULONG UserReference
,
69 _In_ PIRP_CONTEXT IrpContext
,
71 _In_ ULONG UserReference
,
72 _In_ BOOLEAN DelayedClose
80 // Tell prefast this is a workitem routine
81 IO_WORKITEM_ROUTINE CdCloseWorker
;
84 NTAPI
/* ReactOS Change: GCC Does not support STDCALL by default */
86 _In_ PDEVICE_OBJECT DeviceObject
,
87 _In_opt_ PVOID Context
91 #pragma alloc_text(PAGE, CdFspClose)
92 #pragma alloc_text(PAGE, CdCommonClose)
93 #pragma alloc_text(PAGE, CdCommonClosePrivate)
94 #pragma alloc_text(PAGE, CdQueueClose)
95 #pragma alloc_text(PAGE, CdRemoveClose)
96 #pragma alloc_text(PAGE, CdCloseWorker)
109 This routine is called to process the close queues in the CdData. If the
110 Vcb is passed then we want to remove all of the closes for this Vcb.
111 Otherwise we will do as many of the delayed closes as we need to do.
115 Vcb - If specified then we are looking for all of the closes for the
125 PIRP_CONTEXT IrpContext
;
126 IRP_CONTEXT StackIrpContext
;
128 THREAD_CONTEXT ThreadContext
= {0};
133 ULONG VcbHoldCount
= 0;
134 PVCB CurrentVcb
= NULL
;
136 BOOLEAN PotentialVcbTeardown
= FALSE
;
140 FsRtlEnterFileSystem();
143 // Continue processing until there are no more closes to process.
146 while ((IrpContext
= CdRemoveClose( Vcb
)) != NULL
) {
149 // If we don't have an IrpContext then use the one on the stack.
150 // Initialize it for this request.
153 if (SafeNodeType( IrpContext
) != CDFS_NTC_IRP_CONTEXT
) {
156 // Update the local values from the IrpContextLite.
159 Fcb
= ((PIRP_CONTEXT_LITE
) IrpContext
)->Fcb
;
160 UserReference
= ((PIRP_CONTEXT_LITE
) IrpContext
)->UserReference
;
163 // Update the stack irp context with the values from the
167 CdInitializeStackIrpContext( &StackIrpContext
,
168 (PIRP_CONTEXT_LITE
) IrpContext
);
171 // Free the IrpContextLite.
174 CdFreeIrpContextLite( *(PVOID
*)&IrpContext
); /* ReactOS Change: GCC "error: invalid lvalue in unary '&'" */
177 // Remember we have the IrpContext from the stack.
180 IrpContext
= &StackIrpContext
;
183 // Otherwise cleanup the existing IrpContext.
189 // Remember the Fcb and user reference count.
192 Fcb
= (PFCB
) IrpContext
->Irp
;
193 IrpContext
->Irp
= NULL
;
195 UserReference
= (ULONG
) IrpContext
->ExceptionStatus
;
196 IrpContext
->ExceptionStatus
= STATUS_SUCCESS
;
199 _Analysis_assume_(Fcb
!= NULL
&& Fcb
->Vcb
!= NULL
);
202 // We have an IrpContext. Now we need to set the top level thread
206 SetFlag( IrpContext
->Flags
, IRP_CONTEXT_FSP_FLAGS
);
209 // If we were given a Vcb then there is a request on top of this.
212 if (ARGUMENT_PRESENT( Vcb
)) {
214 ClearFlag( IrpContext
->Flags
,
215 IRP_CONTEXT_FLAG_TOP_LEVEL
| IRP_CONTEXT_FLAG_TOP_LEVEL_CDFS
);
218 CdSetThreadContext( IrpContext
, &ThreadContext
);
221 // If we have hit the maximum number of requests to process without
222 // releasing the Vcb then release the Vcb now. If we are holding
223 // a different Vcb to this one then release the previous Vcb.
225 // In either case acquire the current Vcb.
227 // We use the MinDelayedCloseCount from the CdData since it is
228 // a convenient value based on the system size. Only thing we are trying
229 // to do here is prevent this routine starving other threads which
230 // may need this Vcb exclusively.
232 // Note that the check for potential teardown below is unsafe. We'll
233 // repeat later within the cddata lock.
236 PotentialVcbTeardown
= !ARGUMENT_PRESENT( Vcb
) &&
237 (Fcb
->Vcb
->VcbCondition
!= VcbMounted
) &&
238 (Fcb
->Vcb
->VcbCondition
!= VcbMountInProgress
) &&
239 (Fcb
->Vcb
->VcbCleanup
== 0);
241 if (PotentialVcbTeardown
||
242 (VcbHoldCount
> CdData
.MinDelayedCloseCount
) ||
243 (Fcb
->Vcb
!= CurrentVcb
)) {
245 if (CurrentVcb
!= NULL
) {
247 CdReleaseVcb( IrpContext
, CurrentVcb
);
250 if (PotentialVcbTeardown
) {
252 CdAcquireCdData( IrpContext
);
255 // Repeat the checks with global lock held. The volume could have
256 // been remounted while we didn't hold the lock.
259 PotentialVcbTeardown
= !ARGUMENT_PRESENT( Vcb
) &&
260 (Fcb
->Vcb
->VcbCondition
!= VcbMounted
) &&
261 (Fcb
->Vcb
->VcbCondition
!= VcbMountInProgress
) &&
262 (Fcb
->Vcb
->VcbCleanup
== 0);
264 if (!PotentialVcbTeardown
) {
266 CdReleaseCdData( IrpContext
);
270 CurrentVcb
= Fcb
->Vcb
;
272 _Analysis_assume_( CurrentVcb
!= NULL
);
274 CdAcquireVcbShared( IrpContext
, CurrentVcb
, FALSE
);
284 // Call our worker routine to perform the close operation.
287 CdCommonClosePrivate( IrpContext
, CurrentVcb
, Fcb
, UserReference
, FALSE
);
290 // If the reference count on this Vcb is below our residual reference
291 // then check if we should dismount the volume.
294 if (PotentialVcbTeardown
) {
296 CdReleaseVcb( IrpContext
, CurrentVcb
);
297 CdCheckForDismount( IrpContext
, CurrentVcb
, FALSE
);
301 CdReleaseCdData( IrpContext
);
302 PotentialVcbTeardown
= FALSE
;
306 // Complete the current request to cleanup the IrpContext.
309 CdCompleteRequest( IrpContext
, NULL
, STATUS_SUCCESS
);
313 // Release any Vcb we may still hold.
316 if (CurrentVcb
!= NULL
) {
318 CdReleaseVcb( IrpContext
, CurrentVcb
);
323 #pragma prefast(suppress:26165, "Esp:1153")
325 FsRtlExitFileSystem();
328 _Requires_lock_held_(_Global_critical_region_
)
331 _Inout_ PIRP_CONTEXT IrpContext
,
339 This routine is the Fsd entry for the close operation. We decode the file
340 object to find the CDFS structures and type of open. We call our internal
341 worker routine to perform the actual work. If the work wasn't completed
342 then we post to one of our worker queues. The Ccb isn't needed after this
343 point so we delete the Ccb and return STATUS_SUCCESS to our caller in all
348 Irp - Supplies the Irp to process
357 TYPE_OF_OPEN TypeOfOpen
;
362 ULONG UserReference
= 0;
364 BOOLEAN PotentialVcbTeardown
= FALSE
;
368 ASSERT_IRP_CONTEXT( IrpContext
);
372 // If we were called with our file system device object instead of a
373 // volume device object, just complete this request with STATUS_SUCCESS.
376 if (IrpContext
->Vcb
== NULL
) {
378 CdCompleteRequest( IrpContext
, Irp
, STATUS_SUCCESS
);
379 return STATUS_SUCCESS
;
383 // Decode the file object to get the type of open and Fcb/Ccb.
386 TypeOfOpen
= CdDecodeFileObject( IrpContext
,
387 IoGetCurrentIrpStackLocation( Irp
)->FileObject
,
392 // No work to do for unopened file objects.
395 if (TypeOfOpen
== UnopenedFileObject
) {
397 CdCompleteRequest( IrpContext
, Irp
, STATUS_SUCCESS
);
399 return STATUS_SUCCESS
;
405 // Clean up any CCB associated with this open.
413 // We can always deallocate the Ccb if present.
416 CdDeleteCcb( IrpContext
, Ccb
);
420 // If this is the last reference to a user file or directory on a
421 // currently mounted volume, then post it to the delayed close queue. Note
422 // that the VcbCondition check is unsafe, but it doesn't really matter -
423 // we just might delay the volume teardown a little by posting this close.
426 if ((Vcb
->VcbCondition
== VcbMounted
) &&
427 (Fcb
->FcbReference
== 1) &&
428 ((TypeOfOpen
== UserFileOpen
) ||
429 (TypeOfOpen
== UserDirectoryOpen
))) {
431 CdQueueClose( IrpContext
, Fcb
, UserReference
, TRUE
);
435 // Otherwise try to process this close. Post to the async close queue
436 // if we can't acquire all of the resources.
443 // If we may be dismounting this volume then acquire the CdData
446 // Since we now must make volumes go away as soon as reasonable after
447 // the last user handles closes, key off of the cleanup count. It is
448 // OK to do this more than neccesary. Since this Fcb could be holding
449 // a number of other Fcbs (and thus their references), a simple check
450 // on reference count is not appropriate.
452 // Do an unsafe check first to avoid taking the (global) cddata lock in the
456 if ((Vcb
->VcbCleanup
== 0) &&
457 (Vcb
->VcbCondition
!= VcbMounted
)) {
460 // Possible dismount. Acquire CdData to synchronise with the remount path
461 // before looking at the vcb condition again.
464 CdAcquireCdData( IrpContext
);
466 if ((Vcb
->VcbCleanup
== 0) &&
467 (Vcb
->VcbCondition
!= VcbMounted
) &&
468 (Vcb
->VcbCondition
!= VcbMountInProgress
) &&
469 FlagOn( IrpContext
->Flags
, IRP_CONTEXT_FLAG_TOP_LEVEL_CDFS
)) {
471 PotentialVcbTeardown
= TRUE
;
476 // We can't dismount this volume now, there are other references or
477 // it's just been remounted.
482 // Drop the global lock if we don't need it anymore.
485 if (!PotentialVcbTeardown
) {
487 CdReleaseCdData( IrpContext
);
492 // Call the worker routine to perform the actual work. This routine
493 // should never raise except for a fatal error.
496 if (!CdCommonClosePrivate( IrpContext
, Vcb
, Fcb
, UserReference
, TRUE
)) {
499 // If we didn't complete the request then post the request as needed.
502 CdQueueClose( IrpContext
, Fcb
, UserReference
, FALSE
);
506 // Check whether we should be dismounting the volume and then complete
511 else if (PotentialVcbTeardown
) {
513 CdCheckForDismount( IrpContext
, Vcb
, FALSE
);
518 // Always complete this request with STATUS_SUCCESS.
521 CdCompleteRequest( IrpContext
, Irp
, STATUS_SUCCESS
);
523 if (PotentialVcbTeardown
) {
525 CdReleaseCdData( IrpContext
);
529 // Always return STATUS_SUCCESS for closes.
532 return STATUS_SUCCESS
;
537 // Local support routine
540 _Requires_lock_held_(_Global_critical_region_
)
542 CdCommonClosePrivate (
543 _In_ PIRP_CONTEXT IrpContext
,
546 _In_ ULONG UserReference
,
554 This is the worker routine for the close operation. We can be called in
555 an Fsd thread or from a worker Fsp thread. If called from the Fsd thread
556 then we acquire the resources without waiting. Otherwise we know it is
559 We check to see whether we should post this request to the delayed close
560 queue. If we are to process the close here then we acquire the Vcb and
561 Fcb. We will adjust the counts and call our teardown routine to see
562 if any of the structures should go away.
566 Vcb - Vcb for this volume.
568 Fcb - Fcb for this request.
570 UserReference - Number of user references for this file object. This is
571 zero for an internal stream.
573 FromFsd - This request was called from an Fsd thread. Indicates whether
574 we should wait to acquire resources.
576 DelayedClose - Address to store whether we should try to put this on
577 the delayed close queue. Ignored if this routine can process this
582 BOOLEAN - TRUE if this thread processed the close, FALSE otherwise.
591 ASSERT_IRP_CONTEXT( IrpContext
);
595 // Try to acquire the Vcb and Fcb. If we can't acquire them then return
596 // and let our caller know he should post the request to the async
600 if (CdAcquireVcbShared( IrpContext
, Vcb
, FromFsd
)) {
602 if (!CdAcquireFcbExclusive( IrpContext
, Fcb
, FromFsd
)) {
605 // We couldn't get the Fcb. Release the Vcb and let our caller
606 // know to post this request.
609 CdReleaseVcb( IrpContext
, Vcb
);
614 // We didn't get the Vcb. Let our caller know to post this request.
623 // Lock the Vcb and decrement the reference counts.
626 CdLockVcb( IrpContext
, Vcb
);
627 CdDecrementReferenceCounts( IrpContext
, Fcb
, 1, UserReference
);
628 CdUnlockVcb( IrpContext
, Vcb
);
631 // Call our teardown routine to see if this object can go away.
632 // If we don't remove the Fcb then release it.
635 CdTeardownStructures( IrpContext
, Fcb
, &RemovedFcb
);
639 CdReleaseFcb( IrpContext
, Fcb
);
642 _Analysis_assume_lock_not_held_(Fcb
->FcbNonpaged
->FcbResource
);
646 // Release the Vcb and return to our caller. Let him know we completed
650 CdReleaseVcb( IrpContext
, Vcb
);
656 NTAPI
/* ReactOS Change: GCC Does not support STDCALL by default */
658 _In_ PDEVICE_OBJECT DeviceObject
,
659 _In_opt_ PVOID Context
665 Worker routine to call CsFspClose.
669 DeviceObject - Filesystem registration device object
671 Context - Callers context
682 UNREFERENCED_PARAMETER( DeviceObject
);
683 UNREFERENCED_PARAMETER( Context
);
691 _In_ PIRP_CONTEXT IrpContext
,
693 _In_ ULONG UserReference
,
694 _In_ BOOLEAN DelayedClose
701 This routine is called to queue a request to either the async or delayed
702 close queue. For the delayed queue we need to allocate a smaller
703 structure to contain the information about the file object. We do
704 that so we don't put the larger IrpContext structures into this long
705 lived queue. If we can allocate this structure then we put this
706 on the async queue instead.
710 Fcb - Fcb for this file object.
712 UserReference - Number of user references for this file object. This is
713 zero for an internal stream.
715 DelayedClose - Indicates whether this should go on the async or delayed
725 PIRP_CONTEXT_LITE IrpContextLite
= NULL
;
726 BOOLEAN StartWorker
= FALSE
;
730 ASSERT_IRP_CONTEXT( IrpContext
);
734 // Start with the delayed queue request. We can move this to the async
735 // queue if there is an allocation failure.
741 // Try to allocate non-paged pool for the IRP_CONTEXT_LITE.
744 IrpContextLite
= CdCreateIrpContextLite( IrpContext
);
748 // We want to clear the top level context in this thread if
749 // necessary. Call our cleanup routine to do the work.
752 SetFlag( IrpContext
->Flags
, IRP_CONTEXT_FLAG_MORE_PROCESSING
);
753 CdCleanupIrpContext( IrpContext
, TRUE
);
756 // Synchronize with the CdData lock.
762 // If we have an IrpContext then put the request on the delayed close queue.
765 if (IrpContextLite
!= NULL
) {
768 // Initialize the IrpContextLite.
771 IrpContextLite
->NodeTypeCode
= CDFS_NTC_IRP_CONTEXT_LITE
;
772 IrpContextLite
->NodeByteSize
= sizeof( IRP_CONTEXT_LITE
);
773 IrpContextLite
->Fcb
= Fcb
;
774 IrpContextLite
->UserReference
= UserReference
;
775 IrpContextLite
->RealDevice
= IrpContext
->RealDevice
;
778 // Add this to the delayed close list and increment
782 InsertTailList( &CdData
.DelayedCloseQueue
,
783 &IrpContextLite
->DelayedCloseLinks
);
785 CdData
.DelayedCloseCount
+= 1;
788 // If we are above our threshold then start the delayed
792 if (CdData
.DelayedCloseCount
> CdData
.MaxDelayedCloseCount
) {
794 CdData
.ReduceDelayedClose
= TRUE
;
796 if (!CdData
.FspCloseActive
) {
798 CdData
.FspCloseActive
= TRUE
;
804 // Unlock the CdData.
810 // Cleanup the IrpContext.
813 CdCompleteRequest( IrpContext
, NULL
, STATUS_SUCCESS
);
816 // Otherwise drop into the async case below.
822 // Store the information about the file object into the IrpContext.
825 IrpContext
->Irp
= (PIRP
) Fcb
;
826 IrpContext
->ExceptionStatus
= (NTSTATUS
) UserReference
;
829 // Add this to the async close list and increment the count.
832 InsertTailList( &CdData
.AsyncCloseQueue
,
833 &IrpContext
->WorkQueueItem
.List
);
835 CdData
.AsyncCloseCount
+= 1;
838 // Remember to start the Fsp close thread if not currently started.
841 if (!CdData
.FspCloseActive
) {
843 CdData
.FspCloseActive
= TRUE
;
849 // Unlock the CdData.
856 // Start the FspClose thread if we need to.
861 IoQueueWorkItem( CdData
.CloseItem
, CdCloseWorker
, CriticalWorkQueue
, NULL
);
865 // Return to our caller.
873 // Local support routine
887 This routine is called to scan the async and delayed close queues looking
888 for a suitable entry. If the Vcb is specified then we scan both queues
889 looking for an entry with the same Vcb. Otherwise we will look in the
890 async queue first for any close item. If none found there then we look
891 in the delayed close queue provided that we have triggered the delayed
896 PIRP_CONTEXT - NULL if no work item found. Otherwise it is the pointer to
897 either the IrpContext or IrpContextLite for this request.
902 PIRP_CONTEXT IrpContext
= NULL
;
903 PIRP_CONTEXT NextIrpContext
;
904 PIRP_CONTEXT_LITE NextIrpContextLite
;
910 ASSERT_OPTIONAL_VCB( Vcb
);
913 // Lock the CdData to perform the scan.
919 // First check the list of async closes.
922 Entry
= CdData
.AsyncCloseQueue
.Flink
;
924 while (Entry
!= &CdData
.AsyncCloseQueue
) {
927 // Extract the IrpContext.
930 NextIrpContext
= CONTAINING_RECORD( Entry
,
932 WorkQueueItem
.List
);
935 // If no Vcb was specified or this Vcb is for our volume
936 // then perform the close.
939 if (!ARGUMENT_PRESENT( Vcb
) || (NextIrpContext
->Vcb
== Vcb
)) {
941 RemoveEntryList( Entry
);
942 CdData
.AsyncCloseCount
-= 1;
944 IrpContext
= NextIrpContext
;
949 // Move to the next entry.
952 Entry
= Entry
->Flink
;
956 // If we didn't find anything look through the delayed close
959 // We will only check the delayed close queue if we were given
960 // a Vcb or the delayed close operation is active.
963 if ((IrpContext
== NULL
) &&
964 (ARGUMENT_PRESENT( Vcb
) ||
965 (CdData
.ReduceDelayedClose
&&
966 (CdData
.DelayedCloseCount
> CdData
.MinDelayedCloseCount
)))) {
968 Entry
= CdData
.DelayedCloseQueue
.Flink
;
970 while (Entry
!= &CdData
.DelayedCloseQueue
) {
973 // Extract the IrpContext.
976 NextIrpContextLite
= CONTAINING_RECORD( Entry
,
981 // If no Vcb was specified or this Vcb is for our volume
982 // then perform the close.
985 if (!ARGUMENT_PRESENT( Vcb
) || (NextIrpContextLite
->Fcb
->Vcb
== Vcb
)) {
987 RemoveEntryList( Entry
);
988 CdData
.DelayedCloseCount
-= 1;
990 IrpContext
= (PIRP_CONTEXT
) NextIrpContextLite
;
995 // Move to the next entry.
998 Entry
= Entry
->Flink
;
1003 // If the Vcb wasn't specified and we couldn't find an entry
1004 // then turn off the Fsp thread.
1007 if (!ARGUMENT_PRESENT( Vcb
) && (IrpContext
== NULL
)) {
1009 CdData
.FspCloseActive
= FALSE
;
1010 CdData
.ReduceDelayedClose
= FALSE
;
1014 // Unlock the CdData.