Source code of Windows XP (NT5)
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.

328 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. Cleanup.c
  5. Abstract:
  6. This module implements the File Cleanup routine for Cdfs called by the
  7. dispatch driver.
  8. // @@BEGIN_DDKSPLIT
  9. Author:
  10. Brian Andrew [BrianAn] 01-July-1995
  11. Revision History:
  12. // @@END_DDKSPLIT
  13. --*/
  14. #include "CdProcs.h"
  15. //
  16. // The Bug check file id for this module
  17. //
  18. #define BugCheckFileId (CDFS_BUG_CHECK_CLEANUP)
  19. NTSTATUS
  20. CdCommonCleanup (
  21. IN PIRP_CONTEXT IrpContext,
  22. IN PIRP Irp
  23. )
  24. /*++
  25. Routine Description:
  26. This is the common routine for cleanup of a file/directory called by both
  27. the fsd and fsp threads.
  28. Cleanup is invoked whenever the last handle to a file object is closed.
  29. This is different than the Close operation which is invoked when the last
  30. reference to a file object is deleted.
  31. The function of cleanup is to essentially "cleanup" the file/directory
  32. after a user is done with it. The Fcb/Dcb remains around (because MM
  33. still has the file object referenced) but is now available for another
  34. user to open (i.e., as far as the user is concerned the is now closed).
  35. See close for a more complete description of what close does.
  36. We do no synchronization in this routine until we get to the point
  37. where we modify the counts, share access and volume lock field.
  38. We need to update the Fcb and Vcb to show that a user handle has been closed.
  39. The following structures and fields are affected.
  40. Vcb:
  41. VolumeLockFileObject - Did the user lock the volume with this file object.
  42. VcbState - Check if we are unlocking the volume here.
  43. VcbCleanup - Count of outstanding handles on the volume.
  44. DirNotifyQueue - If this file object has pending DirNotify Irps.
  45. Fcb:
  46. ShareAccess - If this is a user handle.
  47. FcbCleanup - Count of outstanding handles on this Fcb.
  48. Oplock - Any outstanding oplocks on this file object.
  49. FileLock - Any outstanding filelocks on this file object.
  50. Arguments:
  51. Irp - Supplies the Irp to process
  52. Return Value:
  53. NTSTATUS - The return status for the operation.
  54. --*/
  55. {
  56. PFILE_OBJECT FileObject;
  57. TYPE_OF_OPEN TypeOfOpen;
  58. BOOLEAN SendUnlockNotification = FALSE;
  59. BOOLEAN AttemptTeardown;
  60. PVCB Vcb;
  61. PFCB Fcb;
  62. PCCB Ccb;
  63. KIRQL SavedIrql;
  64. ASSERT_IRP_CONTEXT( IrpContext );
  65. ASSERT_IRP( Irp );
  66. //
  67. // If we were called with our file system device object instead of a
  68. // volume device object, just complete this request with STATUS_SUCCESS.
  69. //
  70. if (IrpContext->Vcb == NULL) {
  71. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  72. return STATUS_SUCCESS;
  73. }
  74. //
  75. // Get the file object out of the Irp and decode the type of open.
  76. //
  77. FileObject = IoGetCurrentIrpStackLocation( Irp )->FileObject;
  78. TypeOfOpen = CdDecodeFileObject( IrpContext,
  79. FileObject,
  80. &Fcb,
  81. &Ccb );
  82. //
  83. // No work here for either an UnopenedFile object or a StreamFileObject.
  84. //
  85. if (TypeOfOpen <= StreamFileOpen) {
  86. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  87. return STATUS_SUCCESS;
  88. }
  89. //
  90. // Keep a local pointer to the Vcb.
  91. //
  92. Vcb = Fcb->Vcb;
  93. //
  94. // Synchronise with reads while we set the cleanup complete
  95. // flag on this fileobject. Once this flag is set, any further
  96. // reads will be rejected (CdVerifyFcbOperation)
  97. //
  98. CdAcquireFileExclusive( IrpContext, Fcb);
  99. //
  100. // Set the flag in the FileObject to indicate that cleanup is complete.
  101. //
  102. SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );
  103. CdReleaseFile( IrpContext, Fcb);
  104. //
  105. // Acquire the current file.
  106. //
  107. CdAcquireFcbExclusive( IrpContext, Fcb, FALSE );
  108. //
  109. // Use a try-finally to facilitate cleanup.
  110. //
  111. try {
  112. //
  113. // Case on the type of open that we are trying to cleanup.
  114. //
  115. switch (TypeOfOpen) {
  116. case UserDirectoryOpen:
  117. //
  118. // Check if we need to complete any dir notify Irps on this file object.
  119. //
  120. FsRtlNotifyCleanup( Vcb->NotifySync,
  121. &Vcb->DirNotifyList,
  122. Ccb );
  123. break;
  124. case UserFileOpen:
  125. //
  126. // Coordinate the cleanup operation with the oplock state.
  127. // Oplock cleanup operations can always cleanup immediately so no
  128. // need to check for STATUS_PENDING.
  129. //
  130. FsRtlCheckOplock( &Fcb->Oplock,
  131. Irp,
  132. IrpContext,
  133. NULL,
  134. NULL );
  135. //
  136. // Unlock all outstanding file locks.
  137. //
  138. if (Fcb->FileLock != NULL) {
  139. FsRtlFastUnlockAll( Fcb->FileLock,
  140. FileObject,
  141. IoGetRequestorProcess( Irp ),
  142. NULL );
  143. }
  144. //
  145. // Cleanup the cache map.
  146. //
  147. CcUninitializeCacheMap( FileObject, NULL, NULL );
  148. //
  149. // Check the fast io state.
  150. //
  151. CdLockFcb( IrpContext, Fcb );
  152. Fcb->IsFastIoPossible = CdIsFastIoPossible( Fcb );
  153. CdUnlockFcb( IrpContext, Fcb );
  154. break;
  155. case UserVolumeOpen :
  156. break;
  157. default :
  158. CdBugCheck( TypeOfOpen, 0, 0 );
  159. }
  160. //
  161. // Now lock the Vcb in order to modify the fields in the in-memory
  162. // structures.
  163. //
  164. CdLockVcb( IrpContext, Vcb );
  165. //
  166. // Decrement the cleanup counts in the Vcb and Fcb.
  167. //
  168. CdDecrementCleanupCounts( IrpContext, Fcb );
  169. //
  170. // If the cleanup count hit zero and the volume is not mounted, we
  171. // will want to try to spark teardown.
  172. //
  173. AttemptTeardown = (Vcb->VcbCleanup == 0 && Vcb->VcbCondition == VcbNotMounted);
  174. //
  175. // If this file object has locked the volume then perform the unlock operation.
  176. // We do this regardless of explicit or implicit (no share DASD open) lock.
  177. //
  178. if (FileObject == Vcb->VolumeLockFileObject) {
  179. ASSERT( FlagOn( Vcb->VcbState, VCB_STATE_LOCKED));
  180. IoAcquireVpbSpinLock( &SavedIrql );
  181. ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED);
  182. ClearFlag( Vcb->VcbState, VCB_STATE_LOCKED );
  183. Vcb->VolumeLockFileObject = NULL;
  184. SendUnlockNotification = TRUE;
  185. IoReleaseVpbSpinLock( SavedIrql );
  186. }
  187. CdUnlockVcb( IrpContext, Vcb );
  188. //
  189. // We must clean up the share access at this time, since we may not
  190. // get a Close call for awhile if the file was mapped through this
  191. // File Object.
  192. //
  193. IoRemoveShareAccess( FileObject, &Fcb->ShareAccess );
  194. } finally {
  195. CdReleaseFcb( IrpContext, Fcb );
  196. if (SendUnlockNotification) {
  197. FsRtlNotifyVolumeEvent( FileObject, FSRTL_VOLUME_UNLOCK );
  198. }
  199. }
  200. //
  201. // If appropriate, try to spark teardown by purging the volume. Should
  202. // this very fileobject we were cleaning up be the last reason for the
  203. // volume to remain, teardown will commence on completion of this Irp.
  204. //
  205. if (AttemptTeardown) {
  206. CdAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  207. try {
  208. CdPurgeVolume( IrpContext, Vcb, FALSE );
  209. } finally {
  210. CdReleaseVcb( IrpContext, Vcb );
  211. }
  212. }
  213. //
  214. // If this is a normal termination then complete the request
  215. //
  216. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  217. return STATUS_SUCCESS;
  218. }