3 Copyright (c) 1989-2000 Microsoft Corporation
11 This module implements the File Cleanup routine for Cdfs called by the
20 // The Bug check file id for this module
23 #define BugCheckFileId (CDFS_BUG_CHECK_CLEANUP)
25 _Requires_lock_held_(_Global_critical_region_
)
28 _Inout_ PIRP_CONTEXT IrpContext
,
36 This is the common routine for cleanup of a file/directory called by both
37 the fsd and fsp threads.
39 Cleanup is invoked whenever the last handle to a file object is closed.
40 This is different than the Close operation which is invoked when the last
41 reference to a file object is deleted.
43 The function of cleanup is to essentially "cleanup" the file/directory
44 after a user is done with it. The Fcb/Dcb remains around (because MM
45 still has the file object referenced) but is now available for another
46 user to open (i.e., as far as the user is concerned the is now closed).
48 See close for a more complete description of what close does.
50 We do no synchronization in this routine until we get to the point
51 where we modify the counts, share access and volume lock field.
53 We need to update the Fcb and Vcb to show that a user handle has been closed.
54 The following structures and fields are affected.
58 VolumeLockFileObject - Did the user lock the volume with this file object.
59 VcbState - Check if we are unlocking the volume here.
60 VcbCleanup - Count of outstanding handles on the volume.
61 DirNotifyQueue - If this file object has pending DirNotify Irps.
65 ShareAccess - If this is a user handle.
66 FcbCleanup - Count of outstanding handles on this Fcb.
67 Oplock - Any outstanding oplocks on this file object.
68 FileLock - Any outstanding filelocks on this file object.
72 Irp - Supplies the Irp to process
76 NTSTATUS - The return status for the operation.
81 PFILE_OBJECT FileObject
;
82 TYPE_OF_OPEN TypeOfOpen
;
84 BOOLEAN SendUnlockNotification
= FALSE
;
85 BOOLEAN AttemptTeardown
= FALSE
;
86 BOOLEAN VcbAcquired
= FALSE
;
94 ASSERT_IRP_CONTEXT( IrpContext
);
98 // If we were called with our file system device object instead of a
99 // volume device object, just complete this request with STATUS_SUCCESS.
102 if (IrpContext
->Vcb
== NULL
) {
104 CdCompleteRequest( IrpContext
, Irp
, STATUS_SUCCESS
);
105 return STATUS_SUCCESS
;
109 // Get the file object out of the Irp and decode the type of open.
112 FileObject
= IoGetCurrentIrpStackLocation( Irp
)->FileObject
;
114 TypeOfOpen
= CdDecodeFileObject( IrpContext
,
120 // No work here for either an UnopenedFile object or a StreamFileObject.
123 if (TypeOfOpen
<= StreamFileOpen
) {
125 CdCompleteRequest( IrpContext
, Irp
, STATUS_SUCCESS
);
127 return STATUS_SUCCESS
;
131 // Keep a local pointer to the Vcb.
137 // Synchronise with reads while we set the cleanup complete
138 // flag on this fileobject. Once this flag is set, any further
139 // reads will be rejected (CdVerifyFcbOperation)
142 CdAcquireFileExclusive( IrpContext
, Fcb
);
145 // Set the flag in the FileObject to indicate that cleanup is complete.
148 SetFlag( FileObject
->Flags
, FO_CLEANUP_COMPLETE
);
150 CdReleaseFile( IrpContext
, Fcb
);
152 if (TypeOfOpen
== UserVolumeOpen
) {
155 // For a force dismount, physically disconnect this Vcb from the device so
156 // a new mount can occur. Vcb deletion cannot happen at this time since
157 // there is a reference on it associated with this very request, but we'll
158 // call check for dismount again later after we process this close.
161 if (FlagOn( Ccb
->Flags
, CCB_FLAG_DISMOUNT_ON_CLOSE
)) {
163 CdAcquireCdData( IrpContext
);
165 CdCheckForDismount( IrpContext
, Vcb
, TRUE
);
167 CdReleaseCdData( IrpContext
);
170 // If this handle actually wrote something, flush the device buffers,
171 // and then set the verify bit now just to be safe (in case there is no
175 } else if (FlagOn( FileObject
->Flags
, FO_FILE_MODIFIED
)) {
177 CdHijackIrpAndFlushDevice( IrpContext
, Irp
, Vcb
->TargetDeviceObject
);
179 CdMarkDevForVerifyIfVcbMounted( Vcb
);
184 // Acquire the current file.
187 CdAcquireFcbExclusive( IrpContext
, Fcb
, FALSE
);
190 // Use a try-finally to facilitate cleanup.
196 // Case on the type of open that we are trying to cleanup.
199 switch (TypeOfOpen
) {
201 case UserDirectoryOpen
:
204 // Check if we need to complete any dir notify Irps on this file object.
207 FsRtlNotifyCleanup( Vcb
->NotifySync
,
216 // Coordinate the cleanup operation with the oplock state.
217 // Oplock cleanup operations can always cleanup immediately so no
218 // need to check for STATUS_PENDING.
221 FsRtlCheckOplock( CdGetFcbOplock(Fcb
),
228 // Unlock all outstanding file locks.
231 if (Fcb
->FileLock
!= NULL
) {
233 FsRtlFastUnlockAll( Fcb
->FileLock
,
235 IoGetRequestorProcess( Irp
),
240 // Cleanup the cache map.
243 CcUninitializeCacheMap( FileObject
, NULL
, NULL
);
246 // Check the fast io state.
249 CdLockFcb( IrpContext
, Fcb
);
250 Fcb
->IsFastIoPossible
= CdIsFastIoPossible( Fcb
);
251 CdUnlockFcb( IrpContext
, Fcb
);
262 #pragma prefast( suppress:__WARNING_USE_OTHER_FUNCTION, "argument bogus" )
264 CdBugCheck( TypeOfOpen
, 0, 0 );
268 // Now lock the Vcb in order to modify the fields in the in-memory
272 CdLockVcb( IrpContext
, Vcb
);
275 // Decrement the cleanup counts in the Vcb and Fcb.
278 CdDecrementCleanupCounts( IrpContext
, Fcb
);
281 // If the cleanup count hit zero and the volume is not mounted, we
282 // will want to try to spark teardown.
285 AttemptTeardown
= (Vcb
->VcbCleanup
== 0 && Vcb
->VcbCondition
== VcbNotMounted
);
288 // If this file object has locked the volume then perform the unlock operation.
289 // We do this regardless of explicit or implicit (no share DASD open) lock.
292 if (FileObject
== Vcb
->VolumeLockFileObject
) {
294 NT_ASSERT( FlagOn( Vcb
->VcbState
, VCB_STATE_LOCKED
));
296 IoAcquireVpbSpinLock( &SavedIrql
);
298 ClearFlag( Vcb
->Vpb
->Flags
, VPB_LOCKED
);
299 ClearFlag( Vcb
->VcbState
, VCB_STATE_LOCKED
);
300 Vcb
->VolumeLockFileObject
= NULL
;
301 SendUnlockNotification
= TRUE
;
303 IoReleaseVpbSpinLock( SavedIrql
);
306 CdUnlockVcb( IrpContext
, Vcb
);
309 // We must clean up the share access at this time, since we may not
310 // get a Close call for awhile if the file was mapped through this
314 IoRemoveShareAccess( FileObject
, &Fcb
->ShareAccess
);
318 CdReleaseFcb( IrpContext
, Fcb
);
320 if (SendUnlockNotification
) {
322 FsRtlNotifyVolumeEvent( FileObject
, FSRTL_VOLUME_UNLOCK
);
327 // If appropriate, try to spark teardown by purging the volume. Should
328 // this very fileobject we were cleaning up be the last reason for the
329 // volume to remain, teardown will commence on completion of this Irp.
332 if (AttemptTeardown
) {
335 // Preacquire CdData here, since the purges will generate closes which
336 // may acquire CdData if there is a possibility of tearing the volume
340 CdAcquireCdData( IrpContext
);
344 CdAcquireVcbExclusive( IrpContext
, Vcb
, FALSE
);
347 CdPurgeVolume( IrpContext
, Vcb
, FALSE
);
351 if (VcbAcquired
) { CdReleaseVcb( IrpContext
, Vcb
); }
353 CdReleaseCdData( IrpContext
);
358 // If this is a normal termination then complete the request
361 CdCompleteRequest( IrpContext
, Irp
, STATUS_SUCCESS
);
363 return STATUS_SUCCESS
;