Leaked source code of windows server 2003
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.

340 lines
8.6 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. BOOLEAN VcbAcquired = FALSE;
  61. PVCB Vcb;
  62. PFCB Fcb;
  63. PCCB Ccb;
  64. KIRQL SavedIrql;
  65. ASSERT_IRP_CONTEXT( IrpContext );
  66. ASSERT_IRP( Irp );
  67. //
  68. // If we were called with our file system device object instead of a
  69. // volume device object, just complete this request with STATUS_SUCCESS.
  70. //
  71. if (IrpContext->Vcb == NULL) {
  72. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  73. return STATUS_SUCCESS;
  74. }
  75. //
  76. // Get the file object out of the Irp and decode the type of open.
  77. //
  78. FileObject = IoGetCurrentIrpStackLocation( Irp )->FileObject;
  79. TypeOfOpen = CdDecodeFileObject( IrpContext,
  80. FileObject,
  81. &Fcb,
  82. &Ccb );
  83. //
  84. // No work here for either an UnopenedFile object or a StreamFileObject.
  85. //
  86. if (TypeOfOpen <= StreamFileOpen) {
  87. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  88. return STATUS_SUCCESS;
  89. }
  90. //
  91. // Keep a local pointer to the Vcb.
  92. //
  93. Vcb = Fcb->Vcb;
  94. //
  95. // Synchronise with reads while we set the cleanup complete
  96. // flag on this fileobject. Once this flag is set, any further
  97. // reads will be rejected (CdVerifyFcbOperation)
  98. //
  99. CdAcquireFileExclusive( IrpContext, Fcb);
  100. //
  101. // Set the flag in the FileObject to indicate that cleanup is complete.
  102. //
  103. SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );
  104. CdReleaseFile( IrpContext, Fcb);
  105. //
  106. // Acquire the current file.
  107. //
  108. CdAcquireFcbExclusive( IrpContext, Fcb, FALSE );
  109. //
  110. // Use a try-finally to facilitate cleanup.
  111. //
  112. try {
  113. //
  114. // Case on the type of open that we are trying to cleanup.
  115. //
  116. switch (TypeOfOpen) {
  117. case UserDirectoryOpen:
  118. //
  119. // Check if we need to complete any dir notify Irps on this file object.
  120. //
  121. FsRtlNotifyCleanup( Vcb->NotifySync,
  122. &Vcb->DirNotifyList,
  123. Ccb );
  124. break;
  125. case UserFileOpen:
  126. //
  127. // Coordinate the cleanup operation with the oplock state.
  128. // Oplock cleanup operations can always cleanup immediately so no
  129. // need to check for STATUS_PENDING.
  130. //
  131. FsRtlCheckOplock( &Fcb->Oplock,
  132. Irp,
  133. IrpContext,
  134. NULL,
  135. NULL );
  136. //
  137. // Unlock all outstanding file locks.
  138. //
  139. if (Fcb->FileLock != NULL) {
  140. FsRtlFastUnlockAll( Fcb->FileLock,
  141. FileObject,
  142. IoGetRequestorProcess( Irp ),
  143. NULL );
  144. }
  145. //
  146. // Cleanup the cache map.
  147. //
  148. CcUninitializeCacheMap( FileObject, NULL, NULL );
  149. //
  150. // Check the fast io state.
  151. //
  152. CdLockFcb( IrpContext, Fcb );
  153. Fcb->IsFastIoPossible = CdIsFastIoPossible( Fcb );
  154. CdUnlockFcb( IrpContext, Fcb );
  155. break;
  156. case UserVolumeOpen :
  157. break;
  158. default :
  159. CdBugCheck( TypeOfOpen, 0, 0 );
  160. }
  161. //
  162. // Now lock the Vcb in order to modify the fields in the in-memory
  163. // structures.
  164. //
  165. CdLockVcb( IrpContext, Vcb );
  166. //
  167. // Decrement the cleanup counts in the Vcb and Fcb.
  168. //
  169. CdDecrementCleanupCounts( IrpContext, Fcb );
  170. //
  171. // If the cleanup count hit zero and the volume is not mounted, we
  172. // will want to try to spark teardown.
  173. //
  174. AttemptTeardown = (Vcb->VcbCleanup == 0 && Vcb->VcbCondition == VcbNotMounted);
  175. //
  176. // If this file object has locked the volume then perform the unlock operation.
  177. // We do this regardless of explicit or implicit (no share DASD open) lock.
  178. //
  179. if (FileObject == Vcb->VolumeLockFileObject) {
  180. ASSERT( FlagOn( Vcb->VcbState, VCB_STATE_LOCKED));
  181. IoAcquireVpbSpinLock( &SavedIrql );
  182. ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED);
  183. ClearFlag( Vcb->VcbState, VCB_STATE_LOCKED );
  184. Vcb->VolumeLockFileObject = NULL;
  185. SendUnlockNotification = TRUE;
  186. IoReleaseVpbSpinLock( SavedIrql );
  187. }
  188. CdUnlockVcb( IrpContext, Vcb );
  189. //
  190. // We must clean up the share access at this time, since we may not
  191. // get a Close call for awhile if the file was mapped through this
  192. // File Object.
  193. //
  194. IoRemoveShareAccess( FileObject, &Fcb->ShareAccess );
  195. } finally {
  196. CdReleaseFcb( IrpContext, Fcb );
  197. if (SendUnlockNotification) {
  198. FsRtlNotifyVolumeEvent( FileObject, FSRTL_VOLUME_UNLOCK );
  199. }
  200. }
  201. //
  202. // If appropriate, try to spark teardown by purging the volume. Should
  203. // this very fileobject we were cleaning up be the last reason for the
  204. // volume to remain, teardown will commence on completion of this Irp.
  205. //
  206. if (AttemptTeardown) {
  207. //
  208. // Preacquire CdData here, since the purges will generate closes which
  209. // may acquire CdData if there is a possibility of tearing the volume
  210. // down.
  211. //
  212. CdAcquireCdData( IrpContext);
  213. try {
  214. CdAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  215. VcbAcquired = TRUE;
  216. CdPurgeVolume( IrpContext, Vcb, FALSE );
  217. } finally {
  218. if (VcbAcquired) { CdReleaseVcb( IrpContext, Vcb ); }
  219. CdReleaseCdData( IrpContext);
  220. }
  221. }
  222. //
  223. // If this is a normal termination then complete the request
  224. //
  225. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  226. return STATUS_SUCCESS;
  227. }