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.

1947 lines
50 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. VerfySup.c
  5. Abstract:
  6. This module implements the Fat Verify volume and fcb/dcb support
  7. routines
  8. // @@BEGIN_DDKSPLIT
  9. Author:
  10. Gary Kimura [GaryKi] 01-Jun-1990
  11. Revision History:
  12. // @@END_DDKSPLIT
  13. --*/
  14. #include "FatProcs.h"
  15. //
  16. // The Bug check file id for this module
  17. //
  18. #define BugCheckFileId (FAT_BUG_CHECK_VERFYSUP)
  19. //
  20. // The Debug trace level for this module
  21. //
  22. #define Dbg (DEBUG_TRACE_VERFYSUP)
  23. //
  24. // Local procedure prototypes
  25. //
  26. VOID
  27. FatResetFcb (
  28. IN PIRP_CONTEXT IrpContext,
  29. IN PFCB Fcb
  30. );
  31. VOID
  32. FatDetermineAndMarkFcbCondition (
  33. IN PIRP_CONTEXT IrpContext,
  34. IN PFCB Fcb
  35. );
  36. VOID
  37. FatDeferredCleanVolume (
  38. PVOID Parameter
  39. );
  40. NTSTATUS
  41. FatMarkVolumeCompletionRoutine(
  42. IN PDEVICE_OBJECT DeviceObject,
  43. IN PIRP Irp,
  44. IN PVOID Contxt
  45. );
  46. #ifdef ALLOC_PRAGMA
  47. #pragma alloc_text(PAGE, FatCheckDirtyBit)
  48. #pragma alloc_text(PAGE, FatVerifyOperationIsLegal)
  49. #pragma alloc_text(PAGE, FatDeferredCleanVolume)
  50. #pragma alloc_text(PAGE, FatDetermineAndMarkFcbCondition)
  51. #pragma alloc_text(PAGE, FatQuickVerifyVcb)
  52. #pragma alloc_text(PAGE, FatPerformVerify)
  53. #pragma alloc_text(PAGE, FatMarkFcbCondition)
  54. #pragma alloc_text(PAGE, FatResetFcb)
  55. #pragma alloc_text(PAGE, FatVerifyVcb)
  56. #pragma alloc_text(PAGE, FatVerifyFcb)
  57. #endif
  58. VOID
  59. FatMarkFcbCondition (
  60. IN PIRP_CONTEXT IrpContext,
  61. IN PFCB Fcb,
  62. IN FCB_CONDITION FcbCondition,
  63. IN BOOLEAN Recursive
  64. )
  65. /*++
  66. Routine Description:
  67. This routines marks the entire Fcb/Dcb structure from Fcb down with
  68. FcbCondition.
  69. Arguments:
  70. Fcb - Supplies the Fcb/Dcb being marked
  71. FcbCondition - Supplies the setting to use for the Fcb Condition
  72. Recursive - Specifies whether this condition should be applied to
  73. all children (see the case where we are invalidating a live volume
  74. for a case where this is now desireable).
  75. Return Value:
  76. None.
  77. --*/
  78. {
  79. DebugTrace(+1, Dbg, "FatMarkFcbCondition, Fcb = %08lx\n", Fcb );
  80. //
  81. // If we are marking this Fcb something other than Good, we will need
  82. // to have the Vcb exclusive.
  83. //
  84. ASSERT( FcbCondition != FcbNeedsToBeVerified ? TRUE :
  85. FatVcbAcquiredExclusive(IrpContext, Fcb->Vcb) );
  86. //
  87. // If this is a PagingFile it has to be good.
  88. //
  89. if (FlagOn(Fcb->FcbState, FCB_STATE_PAGING_FILE)) {
  90. Fcb->FcbCondition = FcbGood;
  91. return;
  92. }
  93. //
  94. // Update the condition of the Fcb.
  95. //
  96. Fcb->FcbCondition = FcbCondition;
  97. DebugTrace(0, Dbg, "MarkFcb: %Z\n", &Fcb->FullFileName);
  98. //
  99. // This FastIo flag is based on FcbCondition, so update it now. This only
  100. // applies to regular FCBs, of course.
  101. //
  102. if (Fcb->Header.NodeTypeCode == FAT_NTC_FCB) {
  103. Fcb->Header.IsFastIoPossible = FatIsFastIoPossible( Fcb );
  104. }
  105. //
  106. // Now if we marked NeedsVerify or Bad a directory then we also need to
  107. // go and mark all of our children with the same condition.
  108. //
  109. if ( ((FcbCondition == FcbNeedsToBeVerified) ||
  110. (FcbCondition == FcbBad)) &&
  111. Recursive &&
  112. ((Fcb->Header.NodeTypeCode == FAT_NTC_DCB) ||
  113. (Fcb->Header.NodeTypeCode == FAT_NTC_ROOT_DCB)) ) {
  114. PFCB OriginalFcb = Fcb;
  115. while ( (Fcb = FatGetNextFcbTopDown(IrpContext, Fcb, OriginalFcb)) != NULL ) {
  116. DebugTrace(0, Dbg, "MarkFcb: %Z\n", &Fcb->FullFileName);
  117. Fcb->FcbCondition = FcbCondition;
  118. //
  119. // We already know that FastIo is not possible since we are propagating
  120. // a parent's bad/verify flag down the tree - IO to the children must
  121. // take the long route for now.
  122. //
  123. Fcb->Header.IsFastIoPossible = FastIoIsNotPossible;
  124. }
  125. }
  126. DebugTrace(-1, Dbg, "FatMarkFcbCondition -> VOID\n", 0);
  127. return;
  128. }
  129. BOOLEAN
  130. FatMarkDevForVerifyIfVcbMounted(
  131. IN PVCB Vcb
  132. )
  133. /*++
  134. Routine Description:
  135. This routine checks to see if the specified Vcb is currently mounted on
  136. the device or not. If it is, it sets the verify flag on the device, if
  137. not then the state is noted in the Vcb.
  138. Arguments:
  139. Vcb - This is the volume to check.
  140. Return Value:
  141. TRUE if the device has been marked for verify here, FALSE otherwise.
  142. --*/
  143. {
  144. BOOLEAN Marked = FALSE;
  145. KIRQL SavedIrql;
  146. IoAcquireVpbSpinLock( &SavedIrql );
  147. if (Vcb->Vpb->RealDevice->Vpb == Vcb->Vpb) {
  148. SetFlag( Vcb->Vpb->RealDevice->Flags, DO_VERIFY_VOLUME);
  149. Marked = TRUE;
  150. }
  151. else {
  152. //
  153. // Flag this to avoid the VPB spinlock in future passes.
  154. //
  155. SetFlag( Vcb->VcbState, VCB_STATE_VPB_NOT_ON_DEVICE);
  156. }
  157. IoReleaseVpbSpinLock( SavedIrql );
  158. return Marked;
  159. }
  160. VOID
  161. FatVerifyVcb (
  162. IN PIRP_CONTEXT IrpContext,
  163. IN PVCB Vcb
  164. )
  165. /*++
  166. Routine Description:
  167. This routines verifies that the Vcb still denotes a valid Volume
  168. If the Vcb is bad it raises an error condition.
  169. Arguments:
  170. Vcb - Supplies the Vcb being verified
  171. Return Value:
  172. None.
  173. --*/
  174. {
  175. ULONG ChangeCount = 0;
  176. BOOLEAN DevMarkedForVerify;
  177. NTSTATUS Status = STATUS_SUCCESS;
  178. IO_STATUS_BLOCK Iosb;
  179. DebugTrace(+1, Dbg, "FatVerifyVcb, Vcb = %08lx\n", Vcb );
  180. //
  181. // If the media is removable and the verify volume flag in the
  182. // device object is not set then we want to ping the device
  183. // to see if it needs to be verified.
  184. //
  185. // Note that we only force this ping for create operations.
  186. // For others we take a sporting chance. If in the end we
  187. // have to physically access the disk, the right thing will happen.
  188. //
  189. DevMarkedForVerify = BooleanFlagOn(Vcb->Vpb->RealDevice->Flags, DO_VERIFY_VOLUME);
  190. if (FlagOn(Vcb->VcbState, VCB_STATE_FLAG_REMOVABLE_MEDIA)) {
  191. Status = FatPerformDevIoCtrl( IrpContext,
  192. ( Vcb->Vpb->RealDevice->DeviceType == FILE_DEVICE_CD_ROM ?
  193. IOCTL_CDROM_CHECK_VERIFY :
  194. IOCTL_DISK_CHECK_VERIFY ),
  195. Vcb->TargetDeviceObject,
  196. &ChangeCount,
  197. sizeof(ULONG),
  198. FALSE,
  199. TRUE,
  200. &Iosb );
  201. if (Iosb.Information != sizeof(ULONG)) {
  202. //
  203. // Be safe about the count in case the driver didn't fill it in
  204. //
  205. ChangeCount = 0;
  206. }
  207. //
  208. // There are four cases when we want to do a verify. These are the
  209. // first three.
  210. //
  211. // 1. We are mounted, and the device has become empty
  212. // 2. The device has returned verify required (=> DO_VERIFY_VOL flag is
  213. // set, but could be due to hardware condition)
  214. // 3. Media change count doesn't match the one in the Vcb
  215. //
  216. if (((Vcb->VcbCondition == VcbGood) &&
  217. FatIsRawDevice( IrpContext, Status ))
  218. ||
  219. (Status == STATUS_VERIFY_REQUIRED)
  220. ||
  221. (NT_SUCCESS(Status) &&
  222. (Vcb->ChangeCount != ChangeCount))) {
  223. //
  224. // If we are currently the volume on the device then it is our
  225. // responsibility to set the verify flag. If we're not on the device,
  226. // then we shouldn't touch the flag.
  227. //
  228. if (!FlagOn( Vcb->VcbState, VCB_STATE_VPB_NOT_ON_DEVICE) &&
  229. !DevMarkedForVerify) {
  230. DevMarkedForVerify = FatMarkDevForVerifyIfVcbMounted( Vcb);
  231. }
  232. }
  233. }
  234. //
  235. // This is the 4th verify case.
  236. //
  237. // We ALWAYS force CREATE requests on unmounted volumes through the
  238. // verify path. These requests could have been in limbo between
  239. // IoCheckMountedVpb and us, when a verify/mount took place and caused
  240. // a completely different fs/volume to be mounted. In this case the
  241. // checks above may not have caught the condition, since we may already
  242. // have verified (wrong volume) and decided that we have nothing to do.
  243. // We want the requests to be re routed to the currently mounted volume,
  244. // since they were directed at the 'drive', not our volume. So we take
  245. // the verify path for synchronisation, and the request will eventually
  246. // be bounced back to IO with STATUS_REPARSE by our verify handler.
  247. //
  248. if (!DevMarkedForVerify && (IrpContext->MajorFunction == IRP_MJ_CREATE)) {
  249. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( IrpContext->OriginatingIrp);
  250. if ((IrpSp->FileObject->RelatedFileObject == NULL) &&
  251. (Vcb->VcbCondition == VcbNotMounted)) {
  252. DevMarkedForVerify = TRUE;
  253. }
  254. }
  255. //
  256. // Raise any error condition otherwise.
  257. //
  258. if (!NT_SUCCESS( Status ) || DevMarkedForVerify) {
  259. DebugTrace(0, Dbg, "The Vcb needs to be verified\n", 0);
  260. IoSetHardErrorOrVerifyDevice( IrpContext->OriginatingIrp,
  261. Vcb->Vpb->RealDevice );
  262. FatNormalizeAndRaiseStatus( IrpContext, DevMarkedForVerify
  263. ? STATUS_VERIFY_REQUIRED
  264. : Status );
  265. }
  266. //
  267. // Check the operation is legal for current Vcb state.
  268. //
  269. FatQuickVerifyVcb( IrpContext, Vcb );
  270. DebugTrace(-1, Dbg, "FatVerifyVcb -> VOID\n", 0);
  271. }
  272. VOID
  273. FatVerifyFcb (
  274. IN PIRP_CONTEXT IrpContext,
  275. IN PFCB Fcb
  276. )
  277. /*++
  278. Routine Description:
  279. This routines verifies that the Fcb still denotes the same file.
  280. If the Fcb is bad it raises a error condition.
  281. Arguments:
  282. Fcb - Supplies the Fcb being verified
  283. Return Value:
  284. None.
  285. --*/
  286. {
  287. PFCB CurrentFcb;
  288. DebugTrace(+1, Dbg, "FatVerifyFcb, Vcb = %08lx\n", Fcb );
  289. //
  290. // Always refuse operations on dismounted volumes.
  291. //
  292. if (FlagOn( Fcb->Vcb->VcbState, VCB_STATE_FLAG_VOLUME_DISMOUNTED )) {
  293. FatRaiseStatus( IrpContext, STATUS_VOLUME_DISMOUNTED );
  294. }
  295. //
  296. // If this is the Fcb of a deleted dirent or our parent is deleted,
  297. // no-op this call with the hope that the caller will do the right thing.
  298. // The only caller we really have to worry about is the AdvanceOnly
  299. // callback for setting valid data length from Cc, this will happen after
  300. // cleanup (and file deletion), just before the SCM is ripped down.
  301. //
  302. if (IsFileDeleted( IrpContext, Fcb ) ||
  303. ((NodeType(Fcb) != FAT_NTC_ROOT_DCB) &&
  304. IsFileDeleted( IrpContext, Fcb->ParentDcb ))) {
  305. return;
  306. }
  307. //
  308. // If we are not in the process of doing a verify,
  309. // first do a quick spot check on the Vcb.
  310. //
  311. if ( Fcb->Vcb->VerifyThread != KeGetCurrentThread() ) {
  312. FatQuickVerifyVcb( IrpContext, Fcb->Vcb );
  313. }
  314. //
  315. // Now based on the condition of the Fcb we'll either return
  316. // immediately to the caller, raise a condition, or do some work
  317. // to verify the Fcb.
  318. //
  319. switch (Fcb->FcbCondition) {
  320. case FcbGood:
  321. DebugTrace(0, Dbg, "The Fcb is good\n", 0);
  322. break;
  323. case FcbBad:
  324. FatRaiseStatus( IrpContext, STATUS_FILE_INVALID );
  325. break;
  326. case FcbNeedsToBeVerified:
  327. //
  328. // We loop here checking our ancestors until we hit an Fcb which
  329. // is either good or bad.
  330. //
  331. CurrentFcb = Fcb;
  332. while (CurrentFcb->FcbCondition == FcbNeedsToBeVerified) {
  333. FatDetermineAndMarkFcbCondition(IrpContext, CurrentFcb);
  334. //
  335. // If this Fcb didn't make it, or it was the Root Dcb, exit
  336. // the loop now, else continue with out parent.
  337. //
  338. if ( (CurrentFcb->FcbCondition != FcbGood) ||
  339. (NodeType(CurrentFcb) == FAT_NTC_ROOT_DCB) ) {
  340. break;
  341. }
  342. CurrentFcb = CurrentFcb->ParentDcb;
  343. }
  344. //
  345. // Now we can just look at ourselves to see how we did.
  346. //
  347. if (Fcb->FcbCondition != FcbGood) {
  348. FatRaiseStatus( IrpContext, STATUS_FILE_INVALID );
  349. }
  350. break;
  351. default:
  352. DebugDump("Invalid FcbCondition\n", 0, Fcb);
  353. FatBugCheck( Fcb->FcbCondition, 0, 0 );
  354. }
  355. DebugTrace(-1, Dbg, "FatVerifyFcb -> VOID\n", 0);
  356. return;
  357. }
  358. VOID
  359. FatDeferredCleanVolume (
  360. PVOID Parameter
  361. )
  362. /*++
  363. Routine Description:
  364. This is the routine that performs the actual FatMarkVolumeClean call.
  365. It assures that the target volume still exists as there ia a race
  366. condition between queueing the ExWorker item and volumes going away.
  367. Arguments:
  368. Parameter - Points to a clean volume packet that was allocated from pool
  369. Return Value:
  370. None.
  371. --*/
  372. {
  373. PCLEAN_AND_DIRTY_VOLUME_PACKET Packet;
  374. PLIST_ENTRY Links;
  375. PVCB Vcb;
  376. IRP_CONTEXT IrpContext;
  377. BOOLEAN VcbExists = FALSE;
  378. DebugTrace(+1, Dbg, "FatDeferredCleanVolume\n", 0);
  379. Packet = (PCLEAN_AND_DIRTY_VOLUME_PACKET)Parameter;
  380. Vcb = Packet->Vcb;
  381. //
  382. // Make us appear as a top level FSP request so that we will
  383. // receive any errors from the operation.
  384. //
  385. IoSetTopLevelIrp( (PIRP)FSRTL_FSP_TOP_LEVEL_IRP );
  386. //
  387. // Dummy up and Irp Context so we can call our worker routines
  388. //
  389. RtlZeroMemory( &IrpContext, sizeof(IRP_CONTEXT));
  390. SetFlag(IrpContext.Flags, IRP_CONTEXT_FLAG_WAIT);
  391. //
  392. // Acquire shared access to the global lock and make sure this volume
  393. // still exists.
  394. //
  395. FatAcquireSharedGlobal( &IrpContext );
  396. for (Links = FatData.VcbQueue.Flink;
  397. Links != &FatData.VcbQueue;
  398. Links = Links->Flink) {
  399. PVCB ExistingVcb;
  400. ExistingVcb = CONTAINING_RECORD(Links, VCB, VcbLinks);
  401. if ( Vcb == ExistingVcb ) {
  402. VcbExists = TRUE;
  403. break;
  404. }
  405. }
  406. //
  407. // If the vcb is good then mark it clean. Ignore any problems.
  408. //
  409. if ( VcbExists &&
  410. (Vcb->VcbCondition == VcbGood) &&
  411. !FlagOn(Vcb->VcbState, VCB_STATE_FLAG_SHUTDOWN) ) {
  412. try {
  413. if (!FlagOn(Vcb->VcbState, VCB_STATE_FLAG_MOUNTED_DIRTY)) {
  414. FatMarkVolume( &IrpContext, Vcb, VolumeClean );
  415. }
  416. //
  417. // Check for a pathological race condition, and fix it.
  418. //
  419. if (FlagOn(Vcb->VcbState, VCB_STATE_FLAG_VOLUME_DIRTY)) {
  420. FatMarkVolume( &IrpContext, Vcb, VolumeDirty );
  421. } else {
  422. //
  423. // Unlock the volume if it is removable.
  424. //
  425. if (FlagOn(Vcb->VcbState, VCB_STATE_FLAG_REMOVABLE_MEDIA) &&
  426. !FlagOn(Vcb->VcbState, VCB_STATE_FLAG_BOOT_OR_PAGING_FILE)) {
  427. FatToggleMediaEjectDisable( &IrpContext, Vcb, FALSE );
  428. }
  429. }
  430. } except( FsRtlIsNtstatusExpected(GetExceptionCode()) ?
  431. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH ) {
  432. NOTHING;
  433. }
  434. }
  435. //
  436. // Release the global resource, unpin and repinned Bcbs and return.
  437. //
  438. FatReleaseGlobal( &IrpContext );
  439. try {
  440. FatUnpinRepinnedBcbs( &IrpContext );
  441. } except( FsRtlIsNtstatusExpected(GetExceptionCode()) ?
  442. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH ) {
  443. NOTHING;
  444. }
  445. IoSetTopLevelIrp( NULL );
  446. //
  447. // and finally free the packet.
  448. //
  449. ExFreePool( Packet );
  450. return;
  451. }
  452. VOID
  453. FatCleanVolumeDpc (
  454. IN PKDPC Dpc,
  455. IN PVOID DeferredContext,
  456. IN PVOID SystemArgument1,
  457. IN PVOID SystemArgument2
  458. )
  459. /*++
  460. Routine Description:
  461. This routine is dispatched 5 seconds after the last disk structure was
  462. modified in a specific volume, and exqueues an execuative worker thread
  463. to perform the actual task of marking the volume dirty.
  464. Arguments:
  465. DefferedContext - Contains the Vcb to process.
  466. Return Value:
  467. None.
  468. --*/
  469. {
  470. PVCB Vcb;
  471. PCLEAN_AND_DIRTY_VOLUME_PACKET Packet;
  472. Vcb = (PVCB)DeferredContext;
  473. //
  474. // If there is still dirty data (highly unlikely), set the timer for a
  475. // second in the future.
  476. //
  477. if (CcIsThereDirtyData(Vcb->Vpb)) {
  478. LARGE_INTEGER TwoSecondsFromNow;
  479. TwoSecondsFromNow.QuadPart = (LONG)-2*1000*1000*10;
  480. KeSetTimer( &Vcb->CleanVolumeTimer,
  481. TwoSecondsFromNow,
  482. &Vcb->CleanVolumeDpc );
  483. return;
  484. }
  485. //
  486. // If we couldn't get pool, oh well....
  487. //
  488. Packet = ExAllocatePool(NonPagedPool, sizeof(CLEAN_AND_DIRTY_VOLUME_PACKET));
  489. if ( Packet ) {
  490. Packet->Vcb = Vcb;
  491. Packet->Irp = NULL;
  492. //
  493. // Clear the dirty flag now since we cannot synchronize after this point.
  494. //
  495. ClearFlag( Packet->Vcb->VcbState, VCB_STATE_FLAG_VOLUME_DIRTY );
  496. ExInitializeWorkItem( &Packet->Item, &FatDeferredCleanVolume, Packet );
  497. ExQueueWorkItem( &Packet->Item, CriticalWorkQueue );
  498. }
  499. return;
  500. }
  501. VOID
  502. FatMarkVolume (
  503. IN PIRP_CONTEXT IrpContext,
  504. IN PVCB Vcb,
  505. IN FAT_VOLUME_STATE VolumeState
  506. )
  507. /*++
  508. Routine Description:
  509. This routine moves the physically marked volume state between the clean
  510. and dirty states. For compatibility with Win9x, we manipulate both the
  511. historical DOS (on==clean in index 1 of the FAT) and NT (on==dirty in
  512. the CurrentHead field of the BPB) dirty bits.
  513. Arguments:
  514. Vcb - Supplies the Vcb being modified
  515. VolumeState - Supplies the state the volume is transitioning to
  516. Return Value:
  517. None.
  518. --*/
  519. {
  520. PCHAR Sector;
  521. PBCB Bcb = NULL;
  522. KEVENT Event;
  523. PIRP Irp = NULL;
  524. NTSTATUS Status;
  525. BOOLEAN FsInfoUpdate = FALSE;
  526. ULONG FsInfoOffset = 0;
  527. ULONG ThisPass;
  528. LARGE_INTEGER Offset;
  529. BOOLEAN abort = FALSE;
  530. DebugTrace(+1, Dbg, "FatMarkVolume, Vcb = %08lx\n", Vcb);
  531. //
  532. // We had best not be trying to scribble dirty/clean bits if the
  533. // volume is write protected. The responsibility lies with the
  534. // callers to make sure that operations that could cause a state
  535. // change cannot happen. There are a few, though, that show it
  536. // just doesn't make sense to force everyone to do the dinky
  537. // check.
  538. //
  539. //
  540. // If we were called for FAT12 or readonly media, return immediately.
  541. //
  542. if (FlagOn(Vcb->VcbState, VCB_STATE_FLAG_WRITE_PROTECTED) ||
  543. FatIsFat12( Vcb )) {
  544. return;
  545. }
  546. //
  547. // We have two possible additional tasks to do to mark a volume
  548. //
  549. // Pass 0) Flip the dirty bit in the Bpb
  550. // Pass 1) Rewrite the FsInfo sector for FAT32 if needed
  551. //
  552. // In most cases we can collapse these two either because the volume
  553. // is either not FAT32 or the FsInfo sector is adjacent to the boot sector.
  554. //
  555. for (ThisPass = 0; ThisPass < 2; ThisPass++) {
  556. //
  557. // If this volume is being dirtied, or isn't FAT32, or if it is and
  558. // we were able to perform the fast update, or the bpb lied to us
  559. // about where the FsInfo went, we're done - no FsInfo to update in
  560. // a seperate write.
  561. //
  562. if (ThisPass == 1 && (!FatIsFat32( Vcb ) ||
  563. VolumeState != VolumeClean ||
  564. FsInfoUpdate ||
  565. Vcb->Bpb.FsInfoSector == 0)) {
  566. break;
  567. }
  568. //
  569. // Bail if we get an IO error.
  570. //
  571. try {
  572. ULONG PinLength;
  573. ULONG WriteLength;
  574. //
  575. // If the FAT table is 12-bit then our strategy is to pin the entire
  576. // thing when any of it is modified. Here we're going to pin the
  577. // first page, so in the 12-bit case we also want to pin the rest
  578. // of the FAT table.
  579. //
  580. Offset.QuadPart = 0;
  581. if (Vcb->AllocationSupport.FatIndexBitSize == 12) {
  582. //
  583. // But we only write back the first sector.
  584. //
  585. PinLength = FatReservedBytes(&Vcb->Bpb) + FatBytesPerFat(&Vcb->Bpb);
  586. WriteLength = Vcb->Bpb.BytesPerSector;
  587. } else {
  588. WriteLength = PinLength = Vcb->Bpb.BytesPerSector;
  589. //
  590. // If this is a FAT32 volume going into the clean state,
  591. // see about doing the FsInfo sector.
  592. //
  593. if (FatIsFat32( Vcb ) && VolumeState == VolumeClean) {
  594. //
  595. // If the FsInfo sector immediately follows the boot sector,
  596. // we can do this in a single operation by rewriting both
  597. // sectors at once.
  598. //
  599. if (Vcb->Bpb.FsInfoSector == 1) {
  600. ASSERT( ThisPass == 0 );
  601. FsInfoUpdate = TRUE;
  602. FsInfoOffset = Vcb->Bpb.BytesPerSector;
  603. WriteLength = PinLength = Vcb->Bpb.BytesPerSector * 2;
  604. } else if (ThisPass == 1) {
  605. //
  606. // We are doing an explicit write to the FsInfo sector.
  607. //
  608. FsInfoUpdate = TRUE;
  609. FsInfoOffset = 0;
  610. Offset.QuadPart = Vcb->Bpb.BytesPerSector * Vcb->Bpb.FsInfoSector;
  611. }
  612. }
  613. }
  614. //
  615. // Call Cc directly here so that we can avoid overhead and push this
  616. // right down to the disk.
  617. //
  618. CcPinRead( Vcb->VirtualVolumeFile,
  619. &Offset,
  620. PinLength,
  621. TRUE,
  622. &Bcb,
  623. (PVOID *)&Sector );
  624. DbgDoit( IrpContext->PinCount += 1 )
  625. //
  626. // Set the Bpb on Pass 0 always
  627. //
  628. if (ThisPass == 0) {
  629. PCHAR CurrentHead;
  630. //
  631. // Before we do anything, doublecheck that this still looks like a
  632. // FAT bootsector. If it doesn't, something remarkable happened
  633. // and we should avoid touching the volume.
  634. //
  635. // THIS IS TEMPORARY (but may last a while)
  636. //
  637. if (!FatIsBootSectorFat( (PPACKED_BOOT_SECTOR) Sector )) {
  638. abort = TRUE;
  639. leave;
  640. }
  641. if (FatIsFat32( Vcb )) {
  642. CurrentHead = &((PPACKED_BOOT_SECTOR_EX) Sector)->CurrentHead;
  643. } else {
  644. CurrentHead = &((PPACKED_BOOT_SECTOR) Sector)->CurrentHead;
  645. }
  646. if (VolumeState == VolumeClean) {
  647. ClearFlag( *CurrentHead, FAT_BOOT_SECTOR_DIRTY );
  648. } else {
  649. SetFlag( *CurrentHead, FAT_BOOT_SECTOR_DIRTY );
  650. //
  651. // In addition, if this request received an error that may indicate
  652. // media corruption, have autochk perform a surface test.
  653. //
  654. if ( VolumeState == VolumeDirtyWithSurfaceTest ) {
  655. SetFlag( *CurrentHead, FAT_BOOT_SECTOR_TEST_SURFACE );
  656. }
  657. }
  658. }
  659. //
  660. // Update the FsInfo as appropriate.
  661. //
  662. if (FsInfoUpdate) {
  663. PFSINFO_SECTOR FsInfoSector = (PFSINFO_SECTOR) ((PCHAR)Sector + FsInfoOffset);
  664. //
  665. // We just rewrite all of the spec'd fields. Note that we don't
  666. // care to synchronize with the allocation package - this will be
  667. // quickly taken care of by a re-dirtying of the volume if a change
  668. // is racing with us. Remember that this is all a compatibility
  669. // deference for Win9x FAT32 - NT will never look at this information.
  670. //
  671. FsInfoSector->SectorBeginSignature = FSINFO_SECTOR_BEGIN_SIGNATURE;
  672. FsInfoSector->FsInfoSignature = FSINFO_SIGNATURE;
  673. FsInfoSector->FreeClusterCount = Vcb->AllocationSupport.NumberOfFreeClusters;
  674. FsInfoSector->NextFreeCluster = Vcb->ClusterHint;
  675. FsInfoSector->SectorEndSignature = FSINFO_SECTOR_END_SIGNATURE;
  676. }
  677. //
  678. // Initialize the event we're going to use
  679. //
  680. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  681. //
  682. // Build the irp for the operation and also set the override flag.
  683. // Note that we may be at APC level, so do this asyncrhonously and
  684. // use an event for synchronization as normal request completion
  685. // cannot occur at APC level.
  686. //
  687. Irp = IoBuildAsynchronousFsdRequest( IRP_MJ_WRITE,
  688. Vcb->TargetDeviceObject,
  689. (PVOID)Sector,
  690. WriteLength,
  691. &Offset,
  692. NULL );
  693. if ( Irp == NULL ) {
  694. try_return(NOTHING);
  695. }
  696. //
  697. // Make this operation write-through. It never hurts to try to be
  698. // safer about this, even though we aren't logged.
  699. //
  700. SetFlag( IoGetNextIrpStackLocation( Irp )->Flags, SL_WRITE_THROUGH );
  701. //
  702. // Set up the completion routine
  703. //
  704. IoSetCompletionRoutine( Irp,
  705. FatMarkVolumeCompletionRoutine,
  706. &Event,
  707. TRUE,
  708. TRUE,
  709. TRUE );
  710. //
  711. // Call the device to do the write and wait for it to finish.
  712. // Igmore any return status.
  713. //
  714. Status = IoCallDriver( Vcb->TargetDeviceObject, Irp );
  715. if (Status == STATUS_PENDING) {
  716. (VOID)KeWaitForSingleObject( &Event, Executive, KernelMode, FALSE, (PLARGE_INTEGER)NULL );
  717. }
  718. try_exit: NOTHING;
  719. } finally {
  720. //
  721. // Clean up the Irp and Mdl
  722. //
  723. if (Irp) {
  724. //
  725. // If there is an MDL (or MDLs) associated with this I/O
  726. // request, Free it (them) here. This is accomplished by
  727. // walking the MDL list hanging off of the IRP and deallocating
  728. // each MDL encountered.
  729. //
  730. while (Irp->MdlAddress != NULL) {
  731. PMDL NextMdl;
  732. NextMdl = Irp->MdlAddress->Next;
  733. MmUnlockPages( Irp->MdlAddress );
  734. IoFreeMdl( Irp->MdlAddress );
  735. Irp->MdlAddress = NextMdl;
  736. }
  737. IoFreeIrp( Irp );
  738. }
  739. if (Bcb != NULL) {
  740. FatUnpinBcb( IrpContext, Bcb );
  741. }
  742. }
  743. }
  744. if (!abort) {
  745. //
  746. // Flip the dirty bit in the FAT
  747. //
  748. if (VolumeState == VolumeDirty) {
  749. FatSetFatEntry( IrpContext, Vcb, FAT_DIRTY_BIT_INDEX, FAT_DIRTY_VOLUME);
  750. } else {
  751. FatSetFatEntry( IrpContext, Vcb, FAT_DIRTY_BIT_INDEX, FAT_CLEAN_VOLUME);
  752. }
  753. }
  754. DebugTrace(-1, Dbg, "FatMarkVolume -> VOID\n", 0);
  755. return;
  756. }
  757. VOID
  758. FatFspMarkVolumeDirtyWithRecover(
  759. PVOID Parameter
  760. )
  761. /*++
  762. Routine Description:
  763. This is the routine that performs the actual FatMarkVolume Dirty call
  764. on a paging file Io that encounters a media error. It is responsible
  765. for completing the PagingIo Irp as soon as this is done.
  766. Note: this routine (and thus FatMarkVolume()) must be resident as
  767. the paging file might be damaged at this point.
  768. Arguments:
  769. Parameter - Points to a dirty volume packet that was allocated from pool
  770. Return Value:
  771. None.
  772. --*/
  773. {
  774. PCLEAN_AND_DIRTY_VOLUME_PACKET Packet;
  775. PVCB Vcb;
  776. IRP_CONTEXT IrpContext;
  777. PIRP Irp;
  778. BOOLEAN VcbExists = FALSE;
  779. DebugTrace(+1, Dbg, "FatDeferredCleanVolume\n", 0);
  780. Packet = (PCLEAN_AND_DIRTY_VOLUME_PACKET)Parameter;
  781. Vcb = Packet->Vcb;
  782. Irp = Packet->Irp;
  783. //
  784. // Dummy up the IrpContext so we can call our worker routines
  785. //
  786. RtlZeroMemory( &IrpContext, sizeof(IRP_CONTEXT));
  787. SetFlag(IrpContext.Flags, IRP_CONTEXT_FLAG_WAIT);
  788. IrpContext.OriginatingIrp = Irp;
  789. //
  790. // Make us appear as a top level FSP request so that we will
  791. // receive any errors from the operation.
  792. //
  793. IoSetTopLevelIrp( (PIRP)FSRTL_FSP_TOP_LEVEL_IRP );
  794. //
  795. // Try to write out the dirty bit. If something goes wrong, we
  796. // tried.
  797. //
  798. try {
  799. SetFlag( Vcb->VcbState, VCB_STATE_FLAG_MOUNTED_DIRTY );
  800. FatMarkVolume( &IrpContext, Vcb, VolumeDirtyWithSurfaceTest );
  801. } except(FatExceptionFilter( &IrpContext, GetExceptionInformation() )) {
  802. NOTHING;
  803. }
  804. IoSetTopLevelIrp( NULL );
  805. //
  806. // Now complete the originating Irp or set the synchronous event.
  807. //
  808. if (Packet->Event) {
  809. KeSetEvent( Packet->Event, 0, FALSE );
  810. } else {
  811. IoCompleteRequest( Irp, IO_DISK_INCREMENT );
  812. }
  813. }
  814. VOID
  815. FatCheckDirtyBit (
  816. IN PIRP_CONTEXT IrpContext,
  817. IN PVCB Vcb
  818. )
  819. /*++
  820. Routine Description:
  821. This routine looks at the volume dirty bit, and depending on the state of
  822. VCB_STATE_FLAG_MOUNTED_DIRTY, the appropriate action is taken.
  823. Arguments:
  824. Vcb - Supplies the Vcb being queried.
  825. Return Value:
  826. None.
  827. --*/
  828. {
  829. BOOLEAN Dirty;
  830. PPACKED_BOOT_SECTOR BootSector;
  831. PBCB BootSectorBcb;
  832. UNICODE_STRING VolumeLabel;
  833. //
  834. // Look in the boot sector
  835. //
  836. FatReadVolumeFile( IrpContext,
  837. Vcb,
  838. 0,
  839. sizeof(PACKED_BOOT_SECTOR),
  840. &BootSectorBcb,
  841. (PVOID *)&BootSector );
  842. try {
  843. //
  844. // Check if the magic bit is set
  845. //
  846. if (IsBpbFat32(&BootSector->PackedBpb)) {
  847. Dirty = BooleanFlagOn( ((PPACKED_BOOT_SECTOR_EX)BootSector)->CurrentHead,
  848. FAT_BOOT_SECTOR_DIRTY );
  849. } else {
  850. Dirty = BooleanFlagOn( BootSector->CurrentHead, FAT_BOOT_SECTOR_DIRTY );
  851. }
  852. //
  853. // Setup the VolumeLabel string
  854. //
  855. VolumeLabel.Length = Vcb->Vpb->VolumeLabelLength;
  856. VolumeLabel.MaximumLength = MAXIMUM_VOLUME_LABEL_LENGTH;
  857. VolumeLabel.Buffer = &Vcb->Vpb->VolumeLabel[0];
  858. if ( Dirty ) {
  859. //
  860. // Do not trigger the mounted dirty bit if this is a verify
  861. // and the volume is a boot or paging device. We know that
  862. // a boot or paging device cannot leave the system, and thus
  863. // that on its mount we will have figured this out correctly.
  864. //
  865. // This logic is a reasonable hack-o-rama to make BillG happy
  866. // since his machine ran chkdsk after he installed Beta 3. Why?
  867. // 'cause setup cracked a non-exclusive DASD handle near the
  868. // end of setup, wrote some data, closed the handle and we
  869. // set the verify bit ... came back around and saw that other
  870. // arbitrary activity had left the volume in a temporarily dirty
  871. // state.
  872. //
  873. // Of course, the real problem is that we don't have a journal.
  874. //
  875. if (!(IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL &&
  876. IrpContext->MinorFunction == IRP_MN_VERIFY_VOLUME &&
  877. FlagOn( Vcb->VcbState, VCB_STATE_FLAG_BOOT_OR_PAGING_FILE))) {
  878. KdPrintEx((DPFLTR_FASTFAT_ID,
  879. DPFLTR_INFO_LEVEL,
  880. "FASTFAT: WARNING! Mounting Dirty Volume %Z\n",
  881. &VolumeLabel));
  882. SetFlag( Vcb->VcbState, VCB_STATE_FLAG_MOUNTED_DIRTY );
  883. }
  884. } else {
  885. if (FlagOn(Vcb->VcbState, VCB_STATE_FLAG_MOUNTED_DIRTY)) {
  886. KdPrintEx((DPFLTR_FASTFAT_ID,
  887. DPFLTR_INFO_LEVEL,
  888. "FASTFAT: Volume %Z has been cleaned.\n",
  889. &VolumeLabel));
  890. ClearFlag( Vcb->VcbState, VCB_STATE_FLAG_MOUNTED_DIRTY );
  891. } else {
  892. (VOID)FsRtlBalanceReads( Vcb->TargetDeviceObject );
  893. }
  894. }
  895. } finally {
  896. FatUnpinBcb( IrpContext, BootSectorBcb );
  897. }
  898. }
  899. VOID
  900. FatVerifyOperationIsLegal (
  901. IN PIRP_CONTEXT IrpContext
  902. )
  903. /*++
  904. Routine Description:
  905. This routine determines is the requested operation should be allowed to
  906. continue. It either returns to the user if the request is Okay, or
  907. raises an appropriate status.
  908. Arguments:
  909. Irp - Supplies the Irp to check
  910. Return Value:
  911. None.
  912. --*/
  913. {
  914. PIRP Irp;
  915. PFILE_OBJECT FileObject;
  916. Irp = IrpContext->OriginatingIrp;
  917. //
  918. // If the Irp is not present, then we got here via close.
  919. //
  920. //
  921. if ( Irp == NULL ) {
  922. return;
  923. }
  924. FileObject = IoGetCurrentIrpStackLocation(Irp)->FileObject;
  925. //
  926. // If there is not a file object, we cannot continue.
  927. //
  928. if ( FileObject == NULL ) {
  929. return;
  930. }
  931. //
  932. // If the file object has already been cleaned up, and
  933. //
  934. // A) This request is a paging io read or write, or
  935. // B) This request is a close operation, or
  936. // C) This request is a set or query info call (for Lou)
  937. // D) This is an MDL complete
  938. //
  939. // let it pass, otherwise return STATUS_FILE_CLOSED.
  940. //
  941. if ( FlagOn(FileObject->Flags, FO_CLEANUP_COMPLETE) ) {
  942. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
  943. if ( (FlagOn(Irp->Flags, IRP_PAGING_IO)) ||
  944. (IrpSp->MajorFunction == IRP_MJ_CLOSE ) ||
  945. (IrpSp->MajorFunction == IRP_MJ_SET_INFORMATION) ||
  946. (IrpSp->MajorFunction == IRP_MJ_QUERY_INFORMATION) ||
  947. ( ( (IrpSp->MajorFunction == IRP_MJ_READ) ||
  948. (IrpSp->MajorFunction == IRP_MJ_WRITE) ) &&
  949. FlagOn(IrpSp->MinorFunction, IRP_MN_COMPLETE) ) ) {
  950. NOTHING;
  951. } else {
  952. FatRaiseStatus( IrpContext, STATUS_FILE_CLOSED );
  953. }
  954. }
  955. return;
  956. }
  957. //
  958. // Internal support routine
  959. //
  960. VOID
  961. FatResetFcb (
  962. IN PIRP_CONTEXT IrpContext,
  963. IN PFCB Fcb
  964. )
  965. /*++
  966. Routine Description:
  967. This routine is called when an Fcb has been marked as needs to be verified.
  968. It does the following tasks:
  969. - Reset Mcb mapping information
  970. - For directories, reset dirent hints
  971. - Set allocation size to unknown
  972. Arguments:
  973. Fcb - Supplies the Fcb to reset
  974. Return Value:
  975. None.
  976. --*/
  977. {
  978. //
  979. // Don't do the two following operations for the Root Dcb
  980. // or paging files. Paging files!? Yes, if someone diddles
  981. // a volume we try to reverify all of the Fcbs just in case;
  982. // however, there is no safe way to chuck and retrieve the
  983. // mapping pair information for the paging file. Lose it and
  984. // die.
  985. //
  986. if ( NodeType(Fcb) != FAT_NTC_ROOT_DCB &&
  987. !FlagOn( Fcb->FcbState, FCB_STATE_PAGING_FILE )) {
  988. //
  989. // Reset the mcb mapping.
  990. //
  991. FsRtlRemoveLargeMcbEntry( &Fcb->Mcb, 0, 0xFFFFFFFF );
  992. //
  993. // Reset the allocation size to 0 or unknown
  994. //
  995. if ( Fcb->FirstClusterOfFile == 0 ) {
  996. Fcb->Header.AllocationSize.QuadPart = 0;
  997. } else {
  998. Fcb->Header.AllocationSize.QuadPart = FCB_LOOKUP_ALLOCATIONSIZE_HINT;
  999. }
  1000. }
  1001. //
  1002. // If this is a directory, reset the hints.
  1003. //
  1004. if ( (NodeType(Fcb) == FAT_NTC_DCB) ||
  1005. (NodeType(Fcb) == FAT_NTC_ROOT_DCB) ) {
  1006. //
  1007. // Force a rescan of the directory
  1008. //
  1009. Fcb->Specific.Dcb.UnusedDirentVbo = 0xffffffff;
  1010. Fcb->Specific.Dcb.DeletedDirentHint = 0xffffffff;
  1011. }
  1012. }
  1013. //
  1014. // Internal support routine
  1015. //
  1016. VOID
  1017. FatDetermineAndMarkFcbCondition (
  1018. IN PIRP_CONTEXT IrpContext,
  1019. IN PFCB Fcb
  1020. )
  1021. /*++
  1022. Routine Description:
  1023. This routine checks a specific Fcb to see if it is different from what's
  1024. on the disk. The following things are checked:
  1025. - File Name
  1026. - File Size (if not directory)
  1027. - First Cluster Of File
  1028. - Dirent Attributes
  1029. Arguments:
  1030. Fcb - Supplies the Fcb to examine
  1031. Return Value:
  1032. None.
  1033. --*/
  1034. {
  1035. PDIRENT Dirent;
  1036. PBCB DirentBcb;
  1037. ULONG FirstClusterOfFile;
  1038. OEM_STRING Name;
  1039. CHAR Buffer[16];
  1040. //
  1041. // If this is the Root Dcb, special case it. That is, we know
  1042. // by definition that it is good since it is fixed in the volume
  1043. // structure.
  1044. //
  1045. if ( NodeType(Fcb) == FAT_NTC_ROOT_DCB ) {
  1046. FatResetFcb( IrpContext, Fcb );
  1047. FatMarkFcbCondition( IrpContext, Fcb, FcbGood, FALSE );
  1048. return;
  1049. }
  1050. // The first thing we need to do to verify ourselves is
  1051. // locate the dirent on the disk.
  1052. //
  1053. FatGetDirentFromFcbOrDcb( IrpContext,
  1054. Fcb,
  1055. &Dirent,
  1056. &DirentBcb );
  1057. //
  1058. // If we couldn't get the dirent, this fcb must be bad (case of
  1059. // enclosing directory shrinking during the time it was ejected).
  1060. //
  1061. if (DirentBcb == NULL) {
  1062. FatMarkFcbCondition( IrpContext, Fcb, FcbBad, TRUE );
  1063. return;
  1064. }
  1065. //
  1066. // We located the dirent for ourselves now make sure it
  1067. // is really ours by comparing the Name and FatFlags.
  1068. // Then for a file we also check the file size.
  1069. //
  1070. // Note that we have to unpin the Bcb before calling FatResetFcb
  1071. // in order to avoid a deadlock in CcUninitializeCacheMap.
  1072. //
  1073. try {
  1074. Name.MaximumLength = 16;
  1075. Name.Buffer = &Buffer[0];
  1076. Fat8dot3ToString( IrpContext, Dirent, FALSE, &Name );
  1077. //
  1078. // We need to calculate the first cluster 'cause FAT32 splits
  1079. // this field across the dirent.
  1080. //
  1081. FirstClusterOfFile = Dirent->FirstClusterOfFile;
  1082. if (FatIsFat32( Fcb->Vcb )) {
  1083. FirstClusterOfFile += Dirent->FirstClusterOfFileHi << 16;
  1084. }
  1085. if (!RtlEqualString( &Name, &Fcb->ShortName.Name.Oem, TRUE )
  1086. ||
  1087. ( (NodeType(Fcb) == FAT_NTC_FCB) &&
  1088. (Fcb->Header.FileSize.LowPart != Dirent->FileSize) )
  1089. ||
  1090. (FirstClusterOfFile != Fcb->FirstClusterOfFile)
  1091. ||
  1092. (Dirent->Attributes != Fcb->DirentFatFlags) ) {
  1093. FatMarkFcbCondition( IrpContext, Fcb, FcbBad, TRUE );
  1094. } else {
  1095. //
  1096. // We passed. Get the Fcb ready to use again.
  1097. //
  1098. FatResetFcb( IrpContext, Fcb );
  1099. FatMarkFcbCondition( IrpContext, Fcb, FcbGood, FALSE );
  1100. }
  1101. } finally {
  1102. FatUnpinBcb( IrpContext, DirentBcb );
  1103. }
  1104. return;
  1105. }
  1106. //
  1107. // Internal support routine
  1108. //
  1109. VOID
  1110. FatQuickVerifyVcb (
  1111. IN PIRP_CONTEXT IrpContext,
  1112. IN PVCB Vcb
  1113. )
  1114. /*++
  1115. Routine Description:
  1116. This routines just checks the verify bit in the real device and the
  1117. Vcb condition and raises an appropriate exception if so warented.
  1118. It is called when verifying both Fcbs and Vcbs.
  1119. Arguments:
  1120. Vcb - Supplies the Vcb to check the condition of.
  1121. Return Value:
  1122. None.
  1123. --*/
  1124. {
  1125. //
  1126. // If the real device needs to be verified we'll set the
  1127. // DeviceToVerify to be our real device and raise VerifyRequired.
  1128. //
  1129. if (FlagOn(Vcb->Vpb->RealDevice->Flags, DO_VERIFY_VOLUME)) {
  1130. DebugTrace(0, Dbg, "The Vcb needs to be verified\n", 0);
  1131. IoSetHardErrorOrVerifyDevice( IrpContext->OriginatingIrp,
  1132. Vcb->Vpb->RealDevice );
  1133. FatRaiseStatus( IrpContext, STATUS_VERIFY_REQUIRED );
  1134. }
  1135. //
  1136. // Based on the condition of the Vcb we'll either return to our
  1137. // caller or raise an error condition
  1138. //
  1139. switch (Vcb->VcbCondition) {
  1140. case VcbGood:
  1141. DebugTrace(0, Dbg, "The Vcb is good\n", 0);
  1142. //
  1143. // Do a check here of an operation that would try to modify a
  1144. // write protected media.
  1145. //
  1146. if (FlagOn(Vcb->VcbState, VCB_STATE_FLAG_WRITE_PROTECTED) &&
  1147. ((IrpContext->MajorFunction == IRP_MJ_WRITE) ||
  1148. (IrpContext->MajorFunction == IRP_MJ_SET_INFORMATION) ||
  1149. (IrpContext->MajorFunction == IRP_MJ_SET_EA) ||
  1150. (IrpContext->MajorFunction == IRP_MJ_FLUSH_BUFFERS) ||
  1151. (IrpContext->MajorFunction == IRP_MJ_SET_VOLUME_INFORMATION) ||
  1152. (IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL &&
  1153. IrpContext->MinorFunction == IRP_MN_USER_FS_REQUEST &&
  1154. IoGetCurrentIrpStackLocation(IrpContext->OriginatingIrp)->Parameters.FileSystemControl.FsControlCode ==
  1155. FSCTL_MARK_VOLUME_DIRTY))) {
  1156. //
  1157. // Set the real device for the pop-up info, and set the verify
  1158. // bit in the device object, so that we will force a verify
  1159. // in case the user put the correct media back in.
  1160. //
  1161. IoSetHardErrorOrVerifyDevice( IrpContext->OriginatingIrp,
  1162. Vcb->Vpb->RealDevice );
  1163. FatMarkDevForVerifyIfVcbMounted(Vcb);
  1164. FatRaiseStatus( IrpContext, STATUS_MEDIA_WRITE_PROTECTED );
  1165. }
  1166. break;
  1167. case VcbNotMounted:
  1168. DebugTrace(0, Dbg, "The Vcb is not mounted\n", 0);
  1169. //
  1170. // Set the real device for the pop-up info, and set the verify
  1171. // bit in the device object, so that we will force a verify
  1172. // in case the user put the correct media back in.
  1173. //
  1174. IoSetHardErrorOrVerifyDevice( IrpContext->OriginatingIrp,
  1175. Vcb->Vpb->RealDevice );
  1176. FatRaiseStatus( IrpContext, STATUS_WRONG_VOLUME );
  1177. break;
  1178. case VcbBad:
  1179. DebugTrace(0, Dbg, "The Vcb is bad\n", 0);
  1180. if (FlagOn( Vcb->VcbState, VCB_STATE_FLAG_VOLUME_DISMOUNTED )) {
  1181. FatRaiseStatus( IrpContext, STATUS_VOLUME_DISMOUNTED );
  1182. } else {
  1183. FatRaiseStatus( IrpContext, STATUS_FILE_INVALID );
  1184. }
  1185. break;
  1186. default:
  1187. DebugDump("Invalid VcbCondition\n", 0, Vcb);
  1188. FatBugCheck( Vcb->VcbCondition, 0, 0 );
  1189. }
  1190. }
  1191. NTSTATUS
  1192. FatPerformVerify (
  1193. IN PIRP_CONTEXT IrpContext,
  1194. IN PIRP Irp,
  1195. IN PDEVICE_OBJECT Device
  1196. )
  1197. /*++
  1198. Routine Description:
  1199. This routines performs an IoVerifyVolume operation and takes the
  1200. appropriate action. After the Verify is complete the originating
  1201. Irp is sent off to an Ex Worker Thread. This routine is called
  1202. from the exception handler.
  1203. Arguments:
  1204. Irp - The irp to send off after all is well and done.
  1205. Device - The real device needing verification.
  1206. Return Value:
  1207. None.
  1208. --*/
  1209. {
  1210. PVCB Vcb;
  1211. NTSTATUS Status = STATUS_SUCCESS;
  1212. PIO_STACK_LOCATION IrpSp;
  1213. PFILE_OBJECT FileObject = IoGetCurrentIrpStackLocation(Irp)->FileObject;
  1214. BOOLEAN AllowRawMount = FALSE;
  1215. BOOLEAN VcbDeleted = FALSE;
  1216. //
  1217. // Check if this Irp has a status of Verify required and if it does
  1218. // then call the I/O system to do a verify.
  1219. //
  1220. // Skip the IoVerifyVolume if this is a mount or verify request
  1221. // itself. Trying a recursive mount will cause a deadlock with
  1222. // the DeviceObject->DeviceLock.
  1223. //
  1224. if ( (IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL) &&
  1225. ((IrpContext->MinorFunction == IRP_MN_MOUNT_VOLUME) ||
  1226. (IrpContext->MinorFunction == IRP_MN_VERIFY_VOLUME)) ) {
  1227. return FatFsdPostRequest( IrpContext, Irp );
  1228. }
  1229. DebugTrace(0, Dbg, "Verify Required, DeviceObject = %08lx\n", Device);
  1230. //
  1231. // Extract a pointer to the Vcb from the VolumeDeviceObject.
  1232. // Note that since we have specifically excluded mount,
  1233. // requests, we know that IrpSp->DeviceObject is indeed a
  1234. // volume device object.
  1235. //
  1236. IrpSp = IoGetCurrentIrpStackLocation(Irp);
  1237. Vcb = &CONTAINING_RECORD( IrpSp->DeviceObject,
  1238. VOLUME_DEVICE_OBJECT,
  1239. DeviceObject )->Vcb;
  1240. //
  1241. // Check if the volume still thinks it needs to be verified,
  1242. // if it doesn't then we can skip doing a verify because someone
  1243. // else beat us to it.
  1244. //
  1245. try {
  1246. //
  1247. // We will allow Raw to mount this volume if we were doing a
  1248. // a DASD open.
  1249. //
  1250. if ( (IrpContext->MajorFunction == IRP_MJ_CREATE) &&
  1251. (IrpSp->FileObject->FileName.Length == 0) &&
  1252. (IrpSp->FileObject->RelatedFileObject == NULL) ) {
  1253. AllowRawMount = TRUE;
  1254. }
  1255. //
  1256. // Send down the verify. This could be going to a different
  1257. // filesystem.
  1258. //
  1259. Status = IoVerifyVolume( Device, AllowRawMount );
  1260. //
  1261. // If the verify operation completed it will return
  1262. // either STATUS_SUCCESS or STATUS_WRONG_VOLUME, exactly.
  1263. //
  1264. // If FatVerifyVolume encountered an error during
  1265. // processing, it will return that error. If we got
  1266. // STATUS_WRONG_VOLUME from the verfy, and our volume
  1267. // is now mounted, commute the status to STATUS_SUCCESS.
  1268. //
  1269. // Acquire the Vcb so we're working with a stable Vcb condition.
  1270. //
  1271. FatAcquireSharedVcb(IrpContext, Vcb);
  1272. if ( (Status == STATUS_WRONG_VOLUME) &&
  1273. (Vcb->VcbCondition == VcbGood) ) {
  1274. Status = STATUS_SUCCESS;
  1275. }
  1276. else if ((STATUS_SUCCESS == Status) && (Vcb->VcbCondition != VcbGood)) {
  1277. Status = STATUS_WRONG_VOLUME;
  1278. }
  1279. //
  1280. // Do a quick unprotected check here. The routine will do
  1281. // a safe check. After here we can release the resource.
  1282. // Note that if the volume really went away, we will be taking
  1283. // the Reparse path.
  1284. //
  1285. if ((VcbGood != Vcb->VcbCondition) &&
  1286. (0 == Vcb->OpenFileCount) ) {
  1287. FatReleaseVcb( IrpContext, Vcb);
  1288. FatAcquireExclusiveGlobal( IrpContext );
  1289. FatCheckForDismount( IrpContext, Vcb, FALSE );
  1290. FatReleaseGlobal( IrpContext );
  1291. }
  1292. else {
  1293. FatReleaseVcb( IrpContext, Vcb);
  1294. }
  1295. //
  1296. // If the IopMount in IoVerifyVolume did something, and
  1297. // this is an absolute open, force a reparse.
  1298. //
  1299. if ((IrpContext->MajorFunction == IRP_MJ_CREATE) &&
  1300. (FileObject->RelatedFileObject == NULL) &&
  1301. ((Status == STATUS_SUCCESS) || (Status == STATUS_WRONG_VOLUME))) {
  1302. Irp->IoStatus.Information = IO_REMOUNT;
  1303. FatCompleteRequest( IrpContext, Irp, STATUS_REPARSE );
  1304. Status = STATUS_REPARSE;
  1305. Irp = NULL;
  1306. }
  1307. if ( (Irp != NULL) && !NT_SUCCESS(Status) ) {
  1308. //
  1309. // Fill in the device object if required.
  1310. //
  1311. if ( IoIsErrorUserInduced( Status ) ) {
  1312. IoSetHardErrorOrVerifyDevice( Irp, Device );
  1313. }
  1314. ASSERT( STATUS_VERIFY_REQUIRED != Status);
  1315. FatNormalizeAndRaiseStatus( IrpContext, Status );
  1316. }
  1317. //
  1318. // If there is still an Irp, send it off to an Ex Worker thread.
  1319. //
  1320. if ( Irp != NULL ) {
  1321. Status = FatFsdPostRequest( IrpContext, Irp );
  1322. }
  1323. }
  1324. except (FatExceptionFilter( IrpContext, GetExceptionInformation() )) {
  1325. //
  1326. // We had some trouble trying to perform the verify or raised
  1327. // an error ourselves. So we'll abort the I/O request with
  1328. // the error status that we get back from the execption code.
  1329. //
  1330. Status = FatProcessException( IrpContext, Irp, GetExceptionCode() );
  1331. }
  1332. return Status;
  1333. }
  1334. //
  1335. // Local support routine
  1336. //
  1337. NTSTATUS
  1338. FatMarkVolumeCompletionRoutine(
  1339. IN PDEVICE_OBJECT DeviceObject,
  1340. IN PIRP Irp,
  1341. IN PVOID Contxt
  1342. )
  1343. {
  1344. //
  1345. // Set the event so that our call will wake up.
  1346. //
  1347. KeSetEvent( (PKEVENT)Contxt, 0, FALSE );
  1348. UNREFERENCED_PARAMETER( DeviceObject );
  1349. UNREFERENCED_PARAMETER( Irp );
  1350. return STATUS_MORE_PROCESSING_REQUIRED;
  1351. }