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.

368 lines
11 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. Shutdown.c
  5. Abstract:
  6. This module implements the file system shutdown routine for Ntfs
  7. Author:
  8. Gary Kimura [GaryKi] 19-Aug-1991
  9. Revision History:
  10. --*/
  11. #include "NtfsProc.h"
  12. //
  13. // Interal support routine
  14. //
  15. VOID
  16. NtfsCheckpointVolumeUntilDone (
  17. IN PIRP_CONTEXT IrpContext,
  18. IN PVCB Vcb
  19. );
  20. //
  21. // Local debug trace level
  22. //
  23. #define Dbg (DEBUG_TRACE_SHUTDOWN)
  24. NTSTATUS
  25. NtfsFsdShutdown (
  26. IN PVOLUME_DEVICE_OBJECT VolumeDeviceObject,
  27. IN PIRP Irp
  28. )
  29. /*++
  30. Routine Description:
  31. This routine implements the FSD part of shutdown. Note that Shutdown will
  32. never be done asynchronously so we will never need the Fsp counterpart
  33. to shutdown.
  34. This is the shutdown routine for the Ntfs file system device driver.
  35. This routine locks the global file system lock and then syncs all the
  36. mounted volumes.
  37. Arguments:
  38. VolumeDeviceObject - Supplies the volume device object where the
  39. file exists
  40. Irp - Supplies the Irp being processed
  41. Return Value:
  42. NTSTATUS - Always STATUS_SUCCESS
  43. --*/
  44. {
  45. TOP_LEVEL_CONTEXT TopLevelContext;
  46. PTOP_LEVEL_CONTEXT ThreadTopLevelContext;
  47. IRP_CONTEXT LocalIrpContext;
  48. PIRP_CONTEXT IrpContext = &LocalIrpContext;
  49. PLIST_ENTRY Links;
  50. PVCB Vcb;
  51. PIRP NewIrp;
  52. KEVENT Event;
  53. UNREFERENCED_PARAMETER( VolumeDeviceObject );
  54. DebugTrace( +1, Dbg, ("NtfsFsdShutdown\n") );
  55. FsRtlEnterFileSystem();
  56. //
  57. // Allocate an Irp Context that we can use in our procedure calls
  58. // and we know that shutdown will always be synchronous
  59. //
  60. ThreadTopLevelContext = NtfsInitializeTopLevelIrp( &TopLevelContext, FALSE, FALSE );
  61. NtfsInitializeIrpContext( Irp, TRUE, &IrpContext );
  62. NtfsUpdateIrpContextWithTopLevel( IrpContext, ThreadTopLevelContext );
  63. //
  64. // Get everyone else out of the way
  65. //
  66. if (!NtfsAcquireExclusiveGlobal( IrpContext, BooleanFlagOn( IrpContext->State, IRP_CONTEXT_STATE_WAIT ))) {
  67. NtfsRaiseStatus( IrpContext, STATUS_CANT_WAIT, NULL, NULL );
  68. }
  69. try {
  70. BOOLEAN AcquiredFiles;
  71. BOOLEAN AcquiredCheckpoint;
  72. //
  73. // Initialize an event for doing calls down to
  74. // our target device objects
  75. //
  76. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  77. //
  78. // For every volume that is mounted we will flush the
  79. // volume and then shutdown the target device objects.
  80. //
  81. for (Links = NtfsData.VcbQueue.Flink;
  82. Links != &NtfsData.VcbQueue;
  83. Links = Links->Flink) {
  84. ASSERT( FlagOn( IrpContext->State, IRP_CONTEXT_STATE_OWNS_TOP_LEVEL ));
  85. //
  86. // Get the Vcb and put it in the IrpContext.
  87. //
  88. Vcb = CONTAINING_RECORD(Links, VCB, VcbLinks);
  89. IrpContext->Vcb = Vcb;
  90. //
  91. // If we have already been called before for this volume
  92. // (and yes this does happen), skip this volume as no writes
  93. // have been allowed since the first shutdown.
  94. //
  95. if ( FlagOn( Vcb->VcbState, VCB_STATE_FLAG_SHUTDOWN ) ) {
  96. continue;
  97. }
  98. //
  99. // Clear the Mft defrag flag to stop any actions behind our backs.
  100. //
  101. NtfsAcquireCheckpoint( IrpContext, Vcb );
  102. ClearFlag( Vcb->MftDefragState, VCB_MFT_DEFRAG_PERMITTED );
  103. NtfsReleaseCheckpoint( IrpContext, Vcb );
  104. AcquiredFiles = FALSE;
  105. AcquiredCheckpoint = FALSE;
  106. try {
  107. if (FlagOn( Vcb->VcbState, VCB_STATE_VOLUME_MOUNTED )) {
  108. //
  109. // Start by locking out all other checkpoint
  110. // operations.
  111. //
  112. NtfsAcquireCheckpoint( IrpContext, Vcb );
  113. while (FlagOn( Vcb->CheckpointFlags, VCB_CHECKPOINT_SYNC_FLAGS )) {
  114. //
  115. // Release the checkpoint event because we cannot checkpoint now.
  116. //
  117. NtfsReleaseCheckpoint( IrpContext, Vcb );
  118. NtfsWaitOnCheckpointNotify( IrpContext, Vcb );
  119. NtfsAcquireCheckpoint( IrpContext, Vcb );
  120. }
  121. SetFlag( Vcb->CheckpointFlags, VCB_CHECKPOINT_SYNC_FLAGS );
  122. NtfsResetCheckpointNotify( IrpContext, Vcb );
  123. NtfsReleaseCheckpoint( IrpContext, Vcb );
  124. AcquiredCheckpoint = TRUE;
  125. NtfsAcquireAllFiles( IrpContext, Vcb, TRUE, TRUE, FALSE );
  126. AcquiredFiles = TRUE;
  127. SetFlag( Vcb->VcbState, VCB_STATE_VOL_PURGE_IN_PROGRESS );
  128. if (!FlagOn( Vcb->VcbState, VCB_STATE_LOCKED)) {
  129. NtfsCheckpointVolumeUntilDone( IrpContext, Vcb );
  130. }
  131. NtfsCommitCurrentTransaction( IrpContext );
  132. //
  133. // Bug 308819. We find that transactions continue to happen at times even after shutdown
  134. // has been flagged. If we stop the log file, then currently we don't check for
  135. // NULL LSNs getting returned by NtfsWriteLog. As a result our metadata can get
  136. // corrupted. Until we rectify this, let's just not stop the log file in shutdown.
  137. //
  138. // NtfsStopLogFile( Vcb );
  139. //
  140. NtfsAcquireCheckpoint( IrpContext, Vcb );
  141. ClearFlag( Vcb->CheckpointFlags,
  142. VCB_CHECKPOINT_SYNC_FLAGS | VCB_DUMMY_CHECKPOINT_POSTED);
  143. NtfsSetCheckpointNotify( IrpContext, Vcb );
  144. NtfsReleaseCheckpoint( IrpContext, Vcb );
  145. AcquiredCheckpoint = FALSE;
  146. NewIrp = IoBuildSynchronousFsdRequest( IRP_MJ_SHUTDOWN,
  147. Vcb->TargetDeviceObject,
  148. NULL,
  149. 0,
  150. NULL,
  151. &Event,
  152. NULL );
  153. if (NewIrp == NULL) {
  154. NtfsRaiseStatus( IrpContext, STATUS_INSUFFICIENT_RESOURCES, NULL, NULL );
  155. }
  156. if (NT_SUCCESS(IoCallDriver( Vcb->TargetDeviceObject, NewIrp ))) {
  157. (VOID) KeWaitForSingleObject( &Event,
  158. Executive,
  159. KernelMode,
  160. FALSE,
  161. NULL );
  162. KeClearEvent( &Event );
  163. }
  164. }
  165. } except( EXCEPTION_EXECUTE_HANDLER ) {
  166. NtfsMinimumExceptionProcessing( IrpContext );
  167. }
  168. if (AcquiredCheckpoint) {
  169. NtfsAcquireCheckpoint( IrpContext, Vcb );
  170. ClearFlag( Vcb->CheckpointFlags,
  171. VCB_CHECKPOINT_SYNC_FLAGS | VCB_DUMMY_CHECKPOINT_POSTED);
  172. NtfsSetCheckpointNotify( IrpContext, Vcb );
  173. NtfsReleaseCheckpoint( IrpContext, Vcb );
  174. }
  175. SetFlag( Vcb->VcbState, VCB_STATE_FLAG_SHUTDOWN );
  176. ClearFlag( Vcb->VcbState, VCB_STATE_VOL_PURGE_IN_PROGRESS );
  177. if (AcquiredFiles) {
  178. NtfsReleaseAllFiles( IrpContext, Vcb, TRUE );
  179. }
  180. }
  181. } finally {
  182. NtfsReleaseGlobal( IrpContext );
  183. NtfsCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  184. ASSERT( IoGetTopLevelIrp() != (PIRP) &TopLevelContext );
  185. }
  186. DebugTrace( -1, Dbg, ("NtfsFsdShutdown -> STATUS_SUCCESS\n") );
  187. FsRtlExitFileSystem();
  188. return STATUS_SUCCESS;
  189. }
  190. VOID
  191. NtfsCheckpointVolumeUntilDone (
  192. IN PIRP_CONTEXT IrpContext,
  193. IN PVCB Vcb
  194. )
  195. /*++
  196. Routine Description:
  197. This routine keeps trying to checkpoint/flush a volume until it
  198. works. Doing clean checkpoints and looping back to retry on log file full.
  199. Arguments:
  200. Vcb - Vcb to checkpoint til done
  201. Return Value:
  202. None
  203. --*/
  204. {
  205. NTSTATUS Status;
  206. do {
  207. Status = STATUS_SUCCESS;
  208. try {
  209. #ifdef PERF_STATS
  210. IrpContext->LogFullReason = LF_DISMOUNT;
  211. #endif
  212. NtfsCheckpointVolume( IrpContext,
  213. Vcb,
  214. TRUE,
  215. TRUE,
  216. TRUE,
  217. 0,
  218. Vcb->LastRestartArea );
  219. } except( (Status = GetExceptionCode()), EXCEPTION_EXECUTE_HANDLER ) {
  220. NtfsMinimumExceptionProcessing( IrpContext );
  221. }
  222. if (!NT_SUCCESS(Status)) {
  223. //
  224. // To make sure that we can access all of our streams correctly,
  225. // we first restore all of the higher sizes before aborting the
  226. // transaction. Then we restore all of the lower sizes after
  227. // the abort, so that all Scbs are finally restored.
  228. //
  229. NtfsRestoreScbSnapshots( IrpContext, TRUE );
  230. NtfsAbortTransaction( IrpContext, IrpContext->Vcb, NULL );
  231. NtfsRestoreScbSnapshots( IrpContext, FALSE );
  232. //
  233. // A clean volume checkpoint should never get log file full
  234. //
  235. if (Status == STATUS_LOG_FILE_FULL) {
  236. //
  237. // Make sure we don't leave the error code in the top-level
  238. // IrpContext field.
  239. //
  240. ASSERT( IrpContext->TransactionId == 0 );
  241. IrpContext->ExceptionStatus = STATUS_SUCCESS;
  242. #ifdef PERF_STATS
  243. IrpContext->LogFullReason = LF_DISMOUNT;
  244. #endif
  245. NtfsCheckpointVolume( IrpContext,
  246. Vcb,
  247. TRUE,
  248. TRUE,
  249. FALSE,
  250. 0,
  251. Vcb->LastRestartArea );
  252. }
  253. }
  254. } while (Status == STATUS_LOG_FILE_FULL);
  255. }