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.

388 lines
9.7 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 Udfs called by the
  7. dispatch driver.
  8. // @@BEGIN_DDKSPLIT
  9. Author:
  10. Dan Lovinger [DanLo] 31-Oct-1996
  11. Revision History:
  12. // @@END_DDKSPLIT
  13. --*/
  14. #include "UdfProcs.h"
  15. //
  16. // The Bug check file id for this module
  17. //
  18. #define BugCheckFileId (UDFS_BUG_CHECK_CLEANUP)
  19. //
  20. // The local debug trace level
  21. //
  22. #define Dbg (UDFS_DEBUG_LEVEL_CLEANUP)
  23. NTSTATUS
  24. UdfCommonCleanup (
  25. IN PIRP_CONTEXT IrpContext,
  26. IN PIRP Irp
  27. )
  28. /*++
  29. Routine Description:
  30. This is the common routine for cleanup of a file/directory called by both
  31. the fsd and fsp threads.
  32. Cleanup is invoked whenever the last handle to a file object is closed.
  33. This is different than the Close operation which is invoked when the last
  34. reference to a file object is deleted.
  35. The function of cleanup is to essentially "cleanup" the file/directory
  36. after a user is done with it. The Fcb/Dcb remains around (because MM
  37. still has the file object referenced) but is now available for another
  38. user to open (i.e., as far as the user is concerned the is now closed).
  39. See close for a more complete description of what close does.
  40. We do no synchronization in this routine until we get to the point
  41. where we modify the counts, share access and volume lock field.
  42. We need to update the Fcb and Vcb to show that a user handle has been closed.
  43. The following structures and fields are affected.
  44. Vcb:
  45. VolumeLockFileObject - Did the user lock the volume with this file object.
  46. VcbState - Check if we are unlocking the volume here.
  47. VcbCleanup - Count of outstanding handles on the volume.
  48. DirNotifyQueue - If this file object has pending DirNotify Irps.
  49. Fcb:
  50. ShareAccess - If this is a user handle.
  51. FcbCleanup - Count of outstanding handles on this Fcb.
  52. Oplock - Any outstanding oplocks on this file object.
  53. FileLock - Any outstanding filelocks on this file object.
  54. Arguments:
  55. Irp - Supplies the Irp to process
  56. Return Value:
  57. NTSTATUS - The return status for the operation.
  58. --*/
  59. {
  60. PFILE_OBJECT FileObject;
  61. TYPE_OF_OPEN TypeOfOpen;
  62. BOOLEAN SendUnlockNotification = FALSE;
  63. BOOLEAN AttemptTeardown;
  64. BOOLEAN VcbAcquired = FALSE;
  65. PVCB Vcb;
  66. PFCB Fcb;
  67. PCCB Ccb;
  68. KIRQL SavedIrql;
  69. ASSERT_IRP_CONTEXT( IrpContext );
  70. ASSERT_IRP( Irp );
  71. //
  72. // If we were called with our file system device object instead of a
  73. // volume device object, just complete this request with STATUS_SUCCESS.
  74. //
  75. if (IrpContext->Vcb == NULL) {
  76. UdfCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  77. return STATUS_SUCCESS;
  78. }
  79. //
  80. // Get the file object out of the Irp and decode the type of open.
  81. //
  82. FileObject = IoGetCurrentIrpStackLocation( Irp )->FileObject;
  83. TypeOfOpen = UdfDecodeFileObject( FileObject,
  84. &Fcb,
  85. &Ccb );
  86. //
  87. // No work here for either an UnopenedFile object or a StreamFileObject.
  88. //
  89. if (TypeOfOpen <= StreamFileOpen) {
  90. UdfCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  91. return STATUS_SUCCESS;
  92. }
  93. //
  94. // Keep a local pointer to the Vcb.
  95. //
  96. Vcb = Fcb->Vcb;
  97. //
  98. // If we're closing a volume handle, and writes were made,
  99. // hold the Vcb exclusive
  100. //
  101. if ((TypeOfOpen == UserVolumeOpen) &&
  102. FlagOn(FileObject->Flags, FO_FILE_MODIFIED)) {
  103. UdfAcquireVcbExclusive( IrpContext, Vcb, FALSE);
  104. VcbAcquired = TRUE;
  105. }
  106. //
  107. // Synchronise with reads while we set the cleanup complete
  108. // flag on this fileobject. Once this flag is set, any further
  109. // reads will be rejected (CdVerifyFcbOperation)
  110. //
  111. UdfAcquireFileExclusive( IrpContext, Fcb);
  112. //
  113. // Set the flag in the FileObject to indicate that cleanup is complete.
  114. //
  115. SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );
  116. UdfReleaseFile( IrpContext, Fcb);
  117. //
  118. // Acquire the current file.
  119. //
  120. UdfAcquireFcbExclusive( IrpContext, Fcb, FALSE );
  121. //
  122. // Use a try-finally to facilitate cleanup.
  123. //
  124. try {
  125. //
  126. // Case on the type of open that we are trying to cleanup.
  127. //
  128. switch (TypeOfOpen) {
  129. case UserDirectoryOpen:
  130. DebugTrace(( +1, Dbg,
  131. "UdfCommonCleanup, Fcb %08x FO %08x DIR\n",
  132. Fcb,
  133. FileObject ));
  134. //
  135. // Check if we need to complete any dir notify Irps on this file object.
  136. //
  137. FsRtlNotifyCleanup( Vcb->NotifySync,
  138. &Vcb->DirNotifyList,
  139. Ccb );
  140. break;
  141. case UserFileOpen:
  142. DebugTrace(( +1, Dbg,
  143. "UdfCommonCleanup, Fcb %08x FO %08x FILE\n",
  144. Fcb,
  145. FileObject ));
  146. //
  147. // Coordinate the cleanup operation with the oplock state.
  148. // Oplock cleanup operations can always cleanup immediately so no
  149. // need to check for STATUS_PENDING.
  150. //
  151. FsRtlCheckOplock( &Fcb->Oplock,
  152. Irp,
  153. IrpContext,
  154. NULL,
  155. NULL );
  156. //
  157. // Unlock all outstanding file locks.
  158. //
  159. if (Fcb->FileLock != NULL) {
  160. FsRtlFastUnlockAll( Fcb->FileLock,
  161. FileObject,
  162. IoGetRequestorProcess( Irp ),
  163. NULL );
  164. }
  165. //
  166. // Cleanup the cache map.
  167. //
  168. CcUninitializeCacheMap( FileObject, NULL, NULL );
  169. //
  170. // Check the fast io state.
  171. //
  172. UdfLockFcb( IrpContext, Fcb );
  173. Fcb->IsFastIoPossible = UdfIsFastIoPossible( Fcb );
  174. UdfUnlockFcb( IrpContext, Fcb );
  175. break;
  176. case UserVolumeOpen :
  177. DebugTrace(( +1, Dbg,
  178. "UdfCommonCleanup, Fcb %08x FO %08x VOL\n",
  179. Fcb,
  180. FileObject ));
  181. //
  182. // If this handle had write access, and actually wrote something,
  183. // flush the device buffers, and then set the verify bit now
  184. // just to be safe (in case there is no dismount).
  185. //
  186. if (FileObject->WriteAccess &&
  187. FlagOn(FileObject->Flags, FO_FILE_MODIFIED)) {
  188. (VOID)UdfHijackIrpAndFlushDevice( IrpContext,
  189. Irp,
  190. Vcb->TargetDeviceObject );
  191. UdfMarkRealDevForVerify( Vcb->Vpb->RealDevice);
  192. }
  193. break;
  194. default :
  195. UdfBugCheck( TypeOfOpen, 0, 0 );
  196. }
  197. //
  198. // Now lock the Vcb in order to modify the fields in the in-memory
  199. // structures.
  200. //
  201. UdfLockVcb( IrpContext, Vcb );
  202. //
  203. // Decrement the cleanup counts in the Vcb and Fcb.
  204. //
  205. UdfDecrementCleanupCounts( IrpContext, Fcb );
  206. //
  207. // If the cleanup count hit zero and the volume is not mounted, we
  208. // will want to try to spark teardown.
  209. //
  210. AttemptTeardown = (Vcb->VcbCleanup == 0 && Vcb->VcbCondition == VcbNotMounted);
  211. //
  212. // If this file object has locked the volume then perform the unlock operation.
  213. // We do this regardless of explicit or implicit (no share DASD open) lock.
  214. //
  215. if (FileObject == Vcb->VolumeLockFileObject) {
  216. ASSERT( FlagOn( Vcb->VcbState, VCB_STATE_LOCKED));
  217. IoAcquireVpbSpinLock( &SavedIrql );
  218. ClearFlag( Vcb->VcbState, VCB_STATE_LOCKED );
  219. ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED );
  220. Vcb->VolumeLockFileObject = NULL;
  221. SendUnlockNotification = TRUE;
  222. IoReleaseVpbSpinLock( SavedIrql );
  223. }
  224. UdfUnlockVcb( IrpContext, Vcb );
  225. //
  226. // We must clean up the share access at this time, since we may not
  227. // get a Close call for awhile if the file was mapped through this
  228. // File Object.
  229. //
  230. IoRemoveShareAccess( FileObject, &Fcb->ShareAccess );
  231. }
  232. finally {
  233. UdfReleaseFcb( IrpContext, Fcb );
  234. if (SendUnlockNotification) {
  235. FsRtlNotifyVolumeEvent( FileObject, FSRTL_VOLUME_UNLOCK );
  236. }
  237. if (VcbAcquired) {
  238. UdfReleaseVcb( IrpContext, Vcb);
  239. }
  240. }
  241. DebugTrace(( -1, Dbg,
  242. "UdfCommonCleanup, Fcb %08x FO %08x -> SUCCESS\n",
  243. Fcb,
  244. FileObject ));
  245. //
  246. // If appropriate, try to spark teardown by purging the volume. Should
  247. // this very fileobject we were cleaning up be the last reason for the
  248. // volume to remain, teardown will commence on completion of this Irp.
  249. //
  250. if (AttemptTeardown) {
  251. UdfAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  252. try {
  253. UdfPurgeVolume( IrpContext, Vcb, FALSE );
  254. } finally {
  255. UdfReleaseVcb( IrpContext, Vcb );
  256. }
  257. }
  258. //
  259. // If this is a normal termination then complete the request
  260. //
  261. UdfCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  262. return STATUS_SUCCESS;
  263. }