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)
28 IN 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
;
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
);
153 // Acquire the current file.
156 CdAcquireFcbExclusive( IrpContext
, Fcb
, FALSE
);
159 // Use a try-finally to facilitate cleanup.
162 //try { /* ReactOS Change: Manual removal of SEH since macros to hack around it don't allow multiple SEH usage within one function */
165 // Case on the type of open that we are trying to cleanup.
168 switch (TypeOfOpen
) {
170 case UserDirectoryOpen
:
173 // Check if we need to complete any dir notify Irps on this file object.
176 FsRtlNotifyCleanup( Vcb
->NotifySync
,
185 // Coordinate the cleanup operation with the oplock state.
186 // Oplock cleanup operations can always cleanup immediately so no
187 // need to check for STATUS_PENDING.
190 FsRtlCheckOplock( &Fcb
->Oplock
,
197 // Unlock all outstanding file locks.
200 if (Fcb
->FileLock
!= NULL
) {
202 FsRtlFastUnlockAll( Fcb
->FileLock
,
204 IoGetRequestorProcess( Irp
),
209 // Cleanup the cache map.
212 CcUninitializeCacheMap( FileObject
, NULL
, NULL
);
215 // Check the fast io state.
218 CdLockFcb( IrpContext
, Fcb
);
219 Fcb
->IsFastIoPossible
= CdIsFastIoPossible( Fcb
);
220 CdUnlockFcb( IrpContext
, Fcb
);
224 case UserVolumeOpen
:
230 CdBugCheck( TypeOfOpen
, 0, 0 );
234 // Now lock the Vcb in order to modify the fields in the in-memory
238 CdLockVcb( IrpContext
, Vcb
);
241 // Decrement the cleanup counts in the Vcb and Fcb.
244 CdDecrementCleanupCounts( IrpContext
, Fcb
);
247 // If the cleanup count hit zero and the volume is not mounted, we
248 // will want to try to spark teardown.
251 AttemptTeardown
= (Vcb
->VcbCleanup
== 0 && Vcb
->VcbCondition
== VcbNotMounted
);
254 // If this file object has locked the volume then perform the unlock operation.
255 // We do this regardless of explicit or implicit (no share DASD open) lock.
258 if (FileObject
== Vcb
->VolumeLockFileObject
) {
260 ASSERT( FlagOn( Vcb
->VcbState
, VCB_STATE_LOCKED
));
262 IoAcquireVpbSpinLock( &SavedIrql
);
264 ClearFlag( Vcb
->Vpb
->Flags
, VPB_LOCKED
);
265 ClearFlag( Vcb
->VcbState
, VCB_STATE_LOCKED
);
266 Vcb
->VolumeLockFileObject
= NULL
;
267 SendUnlockNotification
= TRUE
;
269 IoReleaseVpbSpinLock( SavedIrql
);
272 CdUnlockVcb( IrpContext
, Vcb
);
275 // We must clean up the share access at this time, since we may not
276 // get a Close call for awhile if the file was mapped through this
280 IoRemoveShareAccess( FileObject
, &Fcb
->ShareAccess
);
282 //} finally { /* ReactOS Change: Manual removal of SEH since macros to hack around it don't allow multiple SEH usage within one function */
284 CdReleaseFcb( IrpContext
, Fcb
);
286 if (SendUnlockNotification
) {
288 FsRtlNotifyVolumeEvent( FileObject
, FSRTL_VOLUME_UNLOCK
);
290 //} /* ReactOS Change: Manual removal of SEH since macros to hack around it don't allow multiple SEH usage within one function */
293 // If appropriate, try to spark teardown by purging the volume. Should
294 // this very fileobject we were cleaning up be the last reason for the
295 // volume to remain, teardown will commence on completion of this Irp.
298 if (AttemptTeardown
) {
301 // Preacquire CdData here, since the purges will generate closes which
302 // may acquire CdData if there is a possibility of tearing the volume
306 CdAcquireCdData( IrpContext
);
310 CdAcquireVcbExclusive( IrpContext
, Vcb
, FALSE
);
313 CdPurgeVolume( IrpContext
, Vcb
, FALSE
);
317 if (VcbAcquired
) { CdReleaseVcb( IrpContext
, Vcb
); }
319 CdReleaseCdData( IrpContext
);
324 // If this is a normal termination then complete the request
327 CdCompleteRequest( IrpContext
, Irp
, STATUS_SUCCESS
);
329 return STATUS_SUCCESS
;