1 /*++ 2 3 Copyright (c) 1989-2000 Microsoft Corporation 4 5 Module Name: 6 7 Cleanup.c 8 9 Abstract: 10 11 This module implements the File Cleanup routine for Cdfs called by the 12 dispatch driver. 13 14 15 --*/ 16 17 #include "cdprocs.h" 18 19 // 20 // The Bug check file id for this module 21 // 22 23 #define BugCheckFileId (CDFS_BUG_CHECK_CLEANUP) 24 25 _Requires_lock_held_(_Global_critical_region_) 26 NTSTATUS 27 CdCommonCleanup ( 28 _Inout_ PIRP_CONTEXT IrpContext, 29 _Inout_ PIRP Irp 30 ) 31 32 /*++ 33 34 Routine Description: 35 36 This is the common routine for cleanup of a file/directory called by both 37 the fsd and fsp threads. 38 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. 42 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). 47 48 See close for a more complete description of what close does. 49 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. 52 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. 55 56 Vcb: 57 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. 62 63 Fcb: 64 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. 69 70 Arguments: 71 72 Irp - Supplies the Irp to process 73 74 Return Value: 75 76 NTSTATUS - The return status for the operation. 77 78 --*/ 79 80 { 81 PFILE_OBJECT FileObject; 82 TYPE_OF_OPEN TypeOfOpen; 83 84 BOOLEAN SendUnlockNotification = FALSE; 85 BOOLEAN AttemptTeardown = FALSE; 86 BOOLEAN VcbAcquired = FALSE; 87 88 PVCB Vcb; 89 PFCB Fcb; 90 PCCB Ccb; 91 92 KIRQL SavedIrql; 93 94 ASSERT_IRP_CONTEXT( IrpContext ); 95 ASSERT_IRP( Irp ); 96 97 // 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. 100 // 101 102 if (IrpContext->Vcb == NULL) { 103 104 CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS ); 105 return STATUS_SUCCESS; 106 } 107 108 // 109 // Get the file object out of the Irp and decode the type of open. 110 // 111 112 FileObject = IoGetCurrentIrpStackLocation( Irp )->FileObject; 113 114 TypeOfOpen = CdDecodeFileObject( IrpContext, 115 FileObject, 116 &Fcb, 117 &Ccb ); 118 119 // 120 // No work here for either an UnopenedFile object or a StreamFileObject. 121 // 122 123 if (TypeOfOpen <= StreamFileOpen) { 124 125 CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS ); 126 127 return STATUS_SUCCESS; 128 } 129 130 // 131 // Keep a local pointer to the Vcb. 132 // 133 134 Vcb = Fcb->Vcb; 135 136 // 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) 140 // 141 142 CdAcquireFileExclusive( IrpContext, Fcb); 143 144 // 145 // Set the flag in the FileObject to indicate that cleanup is complete. 146 // 147 148 SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE ); 149 150 CdReleaseFile( IrpContext, Fcb); 151 152 if (TypeOfOpen == UserVolumeOpen) { 153 154 // 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. 159 // 160 161 if (FlagOn( Ccb->Flags, CCB_FLAG_DISMOUNT_ON_CLOSE )) { 162 163 CdAcquireCdData( IrpContext ); 164 165 CdCheckForDismount( IrpContext, Vcb, TRUE ); 166 167 CdReleaseCdData( IrpContext ); 168 169 // 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 172 // dismount). 173 // 174 175 } else if (FlagOn( FileObject->Flags, FO_FILE_MODIFIED )) { 176 177 CdHijackIrpAndFlushDevice( IrpContext, Irp, Vcb->TargetDeviceObject ); 178 179 CdMarkDevForVerifyIfVcbMounted( Vcb ); 180 } 181 } 182 183 // 184 // Acquire the current file. 185 // 186 187 CdAcquireFcbExclusive( IrpContext, Fcb, FALSE ); 188 189 // 190 // Use a try-finally to facilitate cleanup. 191 // 192 193 _SEH2_TRY { 194 195 // 196 // Case on the type of open that we are trying to cleanup. 197 // 198 199 switch (TypeOfOpen) { 200 201 case UserDirectoryOpen: 202 203 // 204 // Check if we need to complete any dir notify Irps on this file object. 205 // 206 207 FsRtlNotifyCleanup( Vcb->NotifySync, 208 &Vcb->DirNotifyList, 209 Ccb ); 210 211 break; 212 213 case UserFileOpen: 214 215 // 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. 219 // 220 221 FsRtlCheckOplock( CdGetFcbOplock(Fcb), 222 Irp, 223 IrpContext, 224 NULL, 225 NULL ); 226 227 // 228 // Unlock all outstanding file locks. 229 // 230 231 if (Fcb->FileLock != NULL) { 232 233 FsRtlFastUnlockAll( Fcb->FileLock, 234 FileObject, 235 IoGetRequestorProcess( Irp ), 236 NULL ); 237 } 238 239 // 240 // Cleanup the cache map. 241 // 242 243 CcUninitializeCacheMap( FileObject, NULL, NULL ); 244 245 // 246 // Check the fast io state. 247 // 248 249 CdLockFcb( IrpContext, Fcb ); 250 Fcb->IsFastIoPossible = CdIsFastIoPossible( Fcb ); 251 CdUnlockFcb( IrpContext, Fcb ); 252 253 break; 254 255 case UserVolumeOpen: 256 257 break; 258 259 default : 260 261 #ifdef _MSC_VER 262 #pragma prefast( suppress:__WARNING_USE_OTHER_FUNCTION, "argument bogus" ) 263 #endif 264 CdBugCheck( TypeOfOpen, 0, 0 ); 265 } 266 267 // 268 // Now lock the Vcb in order to modify the fields in the in-memory 269 // structures. 270 // 271 272 CdLockVcb( IrpContext, Vcb ); 273 274 // 275 // Decrement the cleanup counts in the Vcb and Fcb. 276 // 277 278 CdDecrementCleanupCounts( IrpContext, Fcb ); 279 280 // 281 // If the cleanup count hit zero and the volume is not mounted, we 282 // will want to try to spark teardown. 283 // 284 285 AttemptTeardown = (Vcb->VcbCleanup == 0 && Vcb->VcbCondition == VcbNotMounted); 286 287 // 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. 290 // 291 292 if (FileObject == Vcb->VolumeLockFileObject) { 293 294 NT_ASSERT( FlagOn( Vcb->VcbState, VCB_STATE_LOCKED)); 295 296 IoAcquireVpbSpinLock( &SavedIrql ); 297 298 ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED); 299 ClearFlag( Vcb->VcbState, VCB_STATE_LOCKED ); 300 Vcb->VolumeLockFileObject = NULL; 301 SendUnlockNotification = TRUE; 302 303 IoReleaseVpbSpinLock( SavedIrql ); 304 } 305 306 CdUnlockVcb( IrpContext, Vcb ); 307 308 // 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 311 // File Object. 312 // 313 314 IoRemoveShareAccess( FileObject, &Fcb->ShareAccess ); 315 316 } _SEH2_FINALLY { 317 318 CdReleaseFcb( IrpContext, Fcb ); 319 320 if (SendUnlockNotification) { 321 322 FsRtlNotifyVolumeEvent( FileObject, FSRTL_VOLUME_UNLOCK ); 323 } 324 } _SEH2_END; 325 326 // 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. 330 // 331 332 if (AttemptTeardown) { 333 334 // 335 // Preacquire CdData here, since the purges will generate closes which 336 // may acquire CdData if there is a possibility of tearing the volume 337 // down. 338 // 339 340 CdAcquireCdData( IrpContext); 341 342 _SEH2_TRY { 343 344 CdAcquireVcbExclusive( IrpContext, Vcb, FALSE ); 345 VcbAcquired = TRUE; 346 347 CdPurgeVolume( IrpContext, Vcb, FALSE ); 348 349 } _SEH2_FINALLY { 350 351 if (VcbAcquired) { CdReleaseVcb( IrpContext, Vcb ); } 352 353 CdReleaseCdData( IrpContext); 354 } _SEH2_END; 355 } 356 357 // 358 // If this is a normal termination then complete the request 359 // 360 361 CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS ); 362 363 return STATUS_SUCCESS; 364 } 365 366 367