3 Copyright (c) 1989-2000 Microsoft Corporation
11 This module implements the Cdfs Verification routines.
19 // The Bug check file id for this module
22 #define BugCheckFileId (CDFS_BUG_CHECK_VERFYSUP)
25 #pragma alloc_text(PAGE, CdVerifyFcbOperation)
26 #pragma alloc_text(PAGE, CdVerifyVcb)
32 IN PIRP_CONTEXT IrpContext
,
34 IN PDEVICE_OBJECT DeviceToVerify
41 This routines performs an IoVerifyVolume operation and takes the
42 appropriate action. If the verify is successful then we send the originating
43 Irp off to an Ex Worker Thread. This routine is called from the exception handler.
45 No file system resources are held when this routine is called.
49 Irp - The irp to send off after all is well and done.
51 Device - The real device needing verification.
61 NTSTATUS Status
= STATUS_SUCCESS
;
62 PIO_STACK_LOCATION IrpSp
;
64 ASSERT_IRP_CONTEXT( IrpContext
);
68 // Check if this Irp has a status of Verify required and if it does
69 // then call the I/O system to do a verify.
71 // Skip the IoVerifyVolume if this is a mount or verify request
72 // itself. Trying a recursive mount will cause a deadlock with
73 // the DeviceObject->DeviceLock.
76 if ((IrpContext
->MajorFunction
== IRP_MJ_FILE_SYSTEM_CONTROL
) &&
77 ((IrpContext
->MinorFunction
== IRP_MN_MOUNT_VOLUME
) ||
78 (IrpContext
->MinorFunction
== IRP_MN_VERIFY_VOLUME
))) {
80 return CdFsdPostRequest( IrpContext
, Irp
);
84 // Extract a pointer to the Vcb from the VolumeDeviceObject.
85 // Note that since we have specifically excluded mount,
86 // requests, we know that IrpSp->DeviceObject is indeed a
87 // volume device object.
90 IrpSp
= IoGetCurrentIrpStackLocation( Irp
);
92 Vcb
= &CONTAINING_RECORD( IrpSp
->DeviceObject
,
98 // Send down the verify FSCTL. Note that this is sent to the
99 // currently mounted volume, which may not be this one.
101 // We will allow Raw to mount this volume if we were doing a
102 // an absolute DASD open.
105 Status
= IoVerifyVolume( DeviceToVerify
, CdOperationIsDasdOpen( IrpContext
));
108 // Acquire the Vcb so we're working with a stable VcbCondition.
111 CdAcquireVcbShared( IrpContext
, Vcb
, FALSE
);
114 // If the verify operation completed it will return
115 // either STATUS_SUCCESS or STATUS_WRONG_VOLUME, exactly.
117 // If CdVerifyVolume encountered an error during
118 // processing, it will return that error. If we got
119 // STATUS_WRONG_VOLUME from the verify, and our volume
120 // is now mounted, commute the status to STATUS_SUCCESS.
123 if ((Status
== STATUS_WRONG_VOLUME
) &&
124 (Vcb
->VcbCondition
== VcbMounted
)) {
126 Status
= STATUS_SUCCESS
;
128 else if ((STATUS_SUCCESS
== Status
) && (Vcb
->VcbCondition
!= VcbMounted
)) {
131 // If the verify succeeded, but our volume is not mounted,
132 // then some other volume is on the device.
135 Status
= STATUS_WRONG_VOLUME
;
139 // Do a quick unprotected check here. The routine will do
140 // a safe check. After here we can release the resource.
141 // Note that if the volume really went away, we will be taking
146 // If the device might need to go away then call our dismount routine.
149 if (((Vcb
->VcbCondition
== VcbNotMounted
) ||
150 (Vcb
->VcbCondition
== VcbInvalid
) ||
151 (Vcb
->VcbCondition
== VcbDismountInProgress
)) &&
152 (Vcb
->VcbReference
<= CDFS_RESIDUAL_REFERENCE
)) {
154 CdReleaseVcb( IrpContext
, Vcb
);
156 CdAcquireCdData( IrpContext
);
157 CdCheckForDismount( IrpContext
, Vcb
, FALSE
);
158 CdReleaseCdData( IrpContext
);
162 CdReleaseVcb( IrpContext
, Vcb
);
166 // If this is a create and the verify succeeded then complete the
167 // request with a REPARSE status.
170 if ((IrpContext
->MajorFunction
== IRP_MJ_CREATE
) &&
171 (IrpSp
->FileObject
->RelatedFileObject
== NULL
) &&
172 ((Status
== STATUS_SUCCESS
) || (Status
== STATUS_WRONG_VOLUME
))) {
174 Irp
->IoStatus
.Information
= IO_REMOUNT
;
176 CdCompleteRequest( IrpContext
, Irp
, STATUS_REPARSE
);
177 Status
= STATUS_REPARSE
;
182 // If there is still an error to process then call the Io system
186 } else if ((Irp
!= NULL
) && !NT_SUCCESS( Status
)) {
189 // Fill in the device object if required.
192 if (IoIsErrorUserInduced( Status
) ) {
194 IoSetHardErrorOrVerifyDevice( Irp
, DeviceToVerify
);
197 CdNormalizeAndRaiseStatus( IrpContext
, Status
);
201 // If there is still an Irp, send it off to an Ex Worker thread.
204 if (IrpContext
!= NULL
) {
206 Status
= CdFsdPostRequest( IrpContext
, Irp
);
209 } except(CdExceptionFilter( IrpContext
, GetExceptionInformation() )) {
212 // We had some trouble trying to perform the verify or raised
213 // an error ourselves. So we'll abort the I/O request with
214 // the error status that we get back from the exception code.
217 Status
= CdProcessException( IrpContext
, Irp
, GetExceptionCode() );
226 IN PIRP_CONTEXT IrpContext
,
235 This routine is called to check if a volume is ready for dismount. This
236 occurs when only file system references are left on the volume.
238 If the dismount is not currently underway and the user reference count
239 has gone to zero then we can begin the dismount.
241 If the dismount is in progress and there are no references left on the
242 volume (we check the Vpb for outstanding references as well to catch
243 any create calls dispatched to the file system) then we can delete
248 Vcb - Vcb for the volume to try to dismount.
250 Force - Whether we will force this volume to be dismounted.
254 BOOLEAN - True if the Vcb was not gone by the time this function finished,
255 False if it was deleted.
257 This is only a trustworthy indication to the caller if it had the vcb
263 BOOLEAN UnlockVcb
= TRUE
;
264 BOOLEAN VcbPresent
= TRUE
;
267 ASSERT_IRP_CONTEXT( IrpContext
);
270 ASSERT_EXCLUSIVE_CDDATA
;
273 // Acquire and lock this Vcb to check the dismount state.
276 CdAcquireVcbExclusive( IrpContext
, Vcb
, FALSE
);
279 // Lets get rid of any pending closes for this volume.
284 CdLockVcb( IrpContext
, Vcb
);
287 // If the dismount is not already underway then check if the
288 // user reference count has gone to zero or we are being forced
289 // to disconnect. If so start the teardown on the Vcb.
292 if (Vcb
->VcbCondition
!= VcbDismountInProgress
) {
294 if (Vcb
->VcbUserReference
<= CDFS_RESIDUAL_USER_REFERENCE
|| Force
) {
296 CdUnlockVcb( IrpContext
, Vcb
);
298 VcbPresent
= CdDismountVcb( IrpContext
, Vcb
);
302 // If the teardown is underway and there are absolutely no references
303 // remaining then delete the Vcb. References here include the
304 // references in the Vcb and Vpb.
307 } else if (Vcb
->VcbReference
== 0) {
309 IoAcquireVpbSpinLock( &SavedIrql
);
312 // If there are no file objects and no reference counts in the
313 // Vpb we can delete the Vcb. Don't forget that we have the
314 // last reference in the Vpb.
317 if (Vcb
->Vpb
->ReferenceCount
== 1) {
319 IoReleaseVpbSpinLock( SavedIrql
);
320 CdUnlockVcb( IrpContext
, Vcb
);
322 CdDeleteVcb( IrpContext
, Vcb
);
327 IoReleaseVpbSpinLock( SavedIrql
);
332 // Unlock the Vcb if still held.
337 CdUnlockVcb( IrpContext
, Vcb
);
341 // Release any resources still acquired.
346 CdReleaseVcb( IrpContext
, Vcb
);
354 CdMarkDevForVerifyIfVcbMounted(
362 This routine checks to see if the specified Vcb is currently mounted on
363 the device or not. If it is, it sets the verify flag on the device, if
364 not then the state is noted in the Vcb.
368 Vcb - This is the volume to check.
372 TRUE if the device has been marked for verify here, FALSE otherwise.
377 BOOLEAN Marked
= FALSE
;
380 IoAcquireVpbSpinLock( &SavedIrql
);
382 if (Vcb
->Vpb
->RealDevice
->Vpb
== Vcb
->Vpb
) {
384 CdMarkRealDevForVerify( Vcb
->Vpb
->RealDevice
);
390 // Flag this to avoid the VPB spinlock in future passes.
393 SetFlag( Vcb
->VcbState
, VCB_STATE_VPB_NOT_ON_DEVICE
);
396 IoReleaseVpbSpinLock( SavedIrql
);
404 IN PIRP_CONTEXT IrpContext
,
412 This routine checks that the current Vcb is valid and currently mounted
413 on the device. It will raise on an error condition.
415 We check whether the volume needs verification and the current state
420 Vcb - This is the volume to verify.
429 NTSTATUS Status
= STATUS_SUCCESS
;
430 IO_STATUS_BLOCK Iosb
;
431 ULONG MediaChangeCount
= 0;
432 BOOLEAN ForceVerify
= FALSE
;
433 BOOLEAN DevMarkedForVerify
;
434 //KIRQL SavedIrql; /* ReactOS Change: GCC Unused variable */
439 // Fail immediately if the volume is in the progress of being dismounted
440 // or has been marked invalid.
443 if ((Vcb
->VcbCondition
== VcbInvalid
) ||
444 ((Vcb
->VcbCondition
== VcbDismountInProgress
) &&
445 (IrpContext
->MajorFunction
!= IRP_MJ_CREATE
))) {
447 CdRaiseStatus( IrpContext
, STATUS_FILE_INVALID
);
450 if (FlagOn( Vcb
->VcbState
, VCB_STATE_REMOVABLE_MEDIA
)) {
453 // Capture the real device verify state.
456 DevMarkedForVerify
= CdRealDevNeedsVerify( Vcb
->Vpb
->RealDevice
);
459 // If the media is removable and the verify volume flag in the
460 // device object is not set then we want to ping the device
461 // to see if it needs to be verified.
464 if (Vcb
->VcbCondition
!= VcbMountInProgress
) {
466 Status
= CdPerformDevIoCtrl( IrpContext
,
467 IOCTL_CDROM_CHECK_VERIFY
,
468 Vcb
->TargetDeviceObject
,
475 if (Iosb
.Information
!= sizeof(ULONG
)) {
478 // Be safe about the count in case the driver didn't fill it in
481 MediaChangeCount
= 0;
485 // There are four cases when we want to do a verify. These are the
488 // 1. We are mounted, and the device has become empty
489 // 2. The device has returned verify required (=> DO_VERIFY_VOL flag is
490 // set, but could be due to hardware condition)
491 // 3. Media change count doesn't match the one in the Vcb
494 if (((Vcb
->VcbCondition
== VcbMounted
) &&
495 CdIsRawDevice( IrpContext
, Status
))
497 (Status
== STATUS_VERIFY_REQUIRED
)
499 (NT_SUCCESS(Status
) &&
500 (Vcb
->MediaChangeCount
!= MediaChangeCount
))) {
503 // If we are currently the volume on the device then it is our
504 // responsibility to set the verify flag. If we're not on the device,
505 // then we shouldn't touch the flag.
508 if (!FlagOn( Vcb
->VcbState
, VCB_STATE_VPB_NOT_ON_DEVICE
) &&
509 !DevMarkedForVerify
) {
511 DevMarkedForVerify
= CdMarkDevForVerifyIfVcbMounted( Vcb
);
517 // NOTE that we no longer update the media change count here. We
518 // do so only when we've actually completed a verify at a particular
519 // change count value.
525 // This is the 4th verify case.
527 // We ALWAYS force CREATE requests on unmounted volumes through the
528 // verify path. These requests could have been in limbo between
529 // IoCheckMountedVpb and us when a verify/mount took place and caused
530 // a completely different fs/volume to be mounted. In this case the
531 // checks above may not have caught the condition, since we may already
532 // have verified (wrong volume) and decided that we have nothing to do.
533 // We want the requests to be re routed to the currently mounted volume,
534 // since they were directed at the 'drive', not our volume.
537 if (NT_SUCCESS( Status
) && !ForceVerify
&&
538 (IrpContext
->MajorFunction
== IRP_MJ_CREATE
)) {
540 PIO_STACK_LOCATION IrpSp
= IoGetCurrentIrpStackLocation( IrpContext
->Irp
);
542 ForceVerify
= (IrpSp
->FileObject
->RelatedFileObject
== NULL
) &&
543 ((Vcb
->VcbCondition
== VcbDismountInProgress
) ||
544 (Vcb
->VcbCondition
== VcbNotMounted
));
547 // Note that we don't touch the device verify flag here. It required
548 // it would have been caught and set by the first set of checks.
553 // Raise the verify / error if necessary.
556 if (ForceVerify
|| !NT_SUCCESS( Status
)) {
558 IoSetHardErrorOrVerifyDevice( IrpContext
->Irp
,
559 Vcb
->Vpb
->RealDevice
);
561 CdRaiseStatus( IrpContext
, ForceVerify
? STATUS_VERIFY_REQUIRED
: Status
);
566 // Based on the condition of the Vcb we'll either return to our
567 // caller or raise an error condition
570 switch (Vcb
->VcbCondition
) {
574 IoSetHardErrorOrVerifyDevice( IrpContext
->Irp
, Vcb
->Vpb
->RealDevice
);
576 CdRaiseStatus( IrpContext
, STATUS_WRONG_VOLUME
);
580 case VcbDismountInProgress
:
582 CdRaiseStatus( IrpContext
, STATUS_FILE_INVALID
);
585 /* ReactOS Change: GCC "enumeration value not handled in switch" */
592 CdVerifyFcbOperation (
593 IN PIRP_CONTEXT IrpContext OPTIONAL
,
601 This routine is called to verify that the state of the Fcb is valid
602 to allow the current operation to continue. We use the state of the
603 Vcb, target device and type of operation to determine this.
607 IrpContext - IrpContext for the request. If not present then we
608 were called from the fast IO path.
610 Fcb - Fcb to perform the request on.
614 BOOLEAN - TRUE if the request can continue, FALSE otherwise.
619 //NTSTATUS Status = STATUS_SUCCESS; /* ReactOS Change: GCC Unused variable */
621 PDEVICE_OBJECT RealDevice
= Vcb
->Vpb
->RealDevice
;
627 // Check that the fileobject has not been cleaned up.
630 if ( ARGUMENT_PRESENT( IrpContext
)) {
632 PFILE_OBJECT FileObject
;
634 Irp
= IrpContext
->Irp
;
635 FileObject
= IoGetCurrentIrpStackLocation( Irp
)->FileObject
;
637 if ( FileObject
&& FlagOn( FileObject
->Flags
, FO_CLEANUP_COMPLETE
)) {
639 PIO_STACK_LOCATION IrpSp
= IoGetCurrentIrpStackLocation( Irp
);
642 // Following FAT, we allow certain operations even on cleaned up
643 // file objects. Everything else, we fail.
646 if ( (FlagOn(Irp
->Flags
, IRP_PAGING_IO
)) ||
647 (IrpSp
->MajorFunction
== IRP_MJ_CLOSE
) ||
648 (IrpSp
->MajorFunction
== IRP_MJ_QUERY_INFORMATION
) ||
649 ( (IrpSp
->MajorFunction
== IRP_MJ_READ
) &&
650 FlagOn(IrpSp
->MinorFunction
, IRP_MN_COMPLETE
) ) ) {
656 CdRaiseStatus( IrpContext
, STATUS_FILE_CLOSED
);
662 // Fail immediately if the volume is in the progress of being dismounted
663 // or has been marked invalid.
666 if ((Vcb
->VcbCondition
== VcbInvalid
) ||
667 (Vcb
->VcbCondition
== VcbDismountInProgress
)) {
669 if (ARGUMENT_PRESENT( IrpContext
)) {
671 CdRaiseStatus( IrpContext
, STATUS_FILE_INVALID
);
678 // Always fail if the volume needs to be verified.
681 if (CdRealDevNeedsVerify( RealDevice
)) {
683 if (ARGUMENT_PRESENT( IrpContext
)) {
685 IoSetHardErrorOrVerifyDevice( IrpContext
->Irp
,
688 CdRaiseStatus( IrpContext
, STATUS_VERIFY_REQUIRED
);
695 // All operations are allowed on mounted.
698 } else if ((Vcb
->VcbCondition
== VcbMounted
) ||
699 (Vcb
->VcbCondition
== VcbMountInProgress
)) {
704 // Fail all requests for fast Io on other Vcb conditions.
707 } else if (!ARGUMENT_PRESENT( IrpContext
)) {
712 // The remaining case is VcbNotMounted.
713 // Mark the device to be verified and raise WRONG_VOLUME.
716 } else if (Vcb
->VcbCondition
== VcbNotMounted
) {
718 if (ARGUMENT_PRESENT( IrpContext
)) {
720 IoSetHardErrorOrVerifyDevice( IrpContext
->Irp
, RealDevice
);
721 CdRaiseStatus( IrpContext
, STATUS_WRONG_VOLUME
);
733 IN PIRP_CONTEXT IrpContext
,
741 This routine is called when all of the user references to a volume are
742 gone. We will initiate all of the teardown any system resources.
744 If all of the references to this volume are gone at the end of this routine
745 then we will complete the teardown of this Vcb and mark the current Vpb
746 as not mounted. Otherwise we will allocated a new Vpb for this device
747 and keep the current Vpb attached to the Vcb.
751 Vcb - Vcb for the volume to dismount.
755 BOOLEAN - TRUE if we didn't delete the Vcb, FALSE otherwise.
761 BOOLEAN VcbPresent
= TRUE
;
764 BOOLEAN FinalReference
;
766 ASSERT_EXCLUSIVE_CDDATA
;
767 ASSERT_EXCLUSIVE_VCB( Vcb
);
769 CdLockVcb( IrpContext
, Vcb
);
772 // We should only take this path once.
775 ASSERT( Vcb
->VcbCondition
!= VcbDismountInProgress
);
778 // Mark the Vcb as DismountInProgress.
781 Vcb
->VcbCondition
= VcbDismountInProgress
;
783 if (Vcb
->XASector
!= NULL
) {
785 CdFreePool( &Vcb
->XASector
);
787 Vcb
->XADiskOffset
= 0;
791 // Remove our reference to the internal Fcb's. The Fcb's will then
792 // be removed in the purge path below.
795 if (Vcb
->RootIndexFcb
!= NULL
) {
797 Vcb
->RootIndexFcb
->FcbReference
-= 1;
798 Vcb
->RootIndexFcb
->FcbUserReference
-= 1;
801 if (Vcb
->PathTableFcb
!= NULL
) {
803 Vcb
->PathTableFcb
->FcbReference
-= 1;
804 Vcb
->PathTableFcb
->FcbUserReference
-= 1;
807 if (Vcb
->VolumeDasdFcb
!= NULL
) {
809 Vcb
->VolumeDasdFcb
->FcbReference
-= 1;
810 Vcb
->VolumeDasdFcb
->FcbUserReference
-= 1;
813 CdUnlockVcb( IrpContext
, Vcb
);
819 CdPurgeVolume( IrpContext
, Vcb
, TRUE
);
822 // Empty the delayed and async close queues.
830 // Remove the mount volume reference.
833 CdLockVcb( IrpContext
, Vcb
);
834 Vcb
->VcbReference
-= 1;
837 // Acquire the Vpb spinlock to check for Vpb references.
840 IoAcquireVpbSpinLock( &SavedIrql
);
843 // Remember if this is the last reference on this Vcb. We incremented
844 // the count on the Vpb earlier so we get one last crack it. If our
845 // reference has gone to zero but the vpb reference count is greater
846 // than zero then the Io system will be responsible for deleting the
850 FinalReference
= (BOOLEAN
) ((Vcb
->VcbReference
== 0) &&
851 (OldVpb
->ReferenceCount
== 1));
854 // There is a reference count in the Vpb and in the Vcb. We have
855 // incremented the reference count in the Vpb to make sure that
856 // we have last crack at it. If this is a failed mount then we
857 // want to return the Vpb to the IO system to use for the next
861 if (OldVpb
->RealDevice
->Vpb
== OldVpb
) {
864 // If not the final reference then swap out the Vpb. We must
865 // preserve the REMOVE_PENDING flag so that the device is
866 // not remounted in the middle of a PnP remove operation.
869 if (!FinalReference
) {
871 ASSERT( Vcb
->SwapVpb
!= NULL
);
873 Vcb
->SwapVpb
->Type
= IO_TYPE_VPB
;
874 Vcb
->SwapVpb
->Size
= sizeof( VPB
);
875 Vcb
->SwapVpb
->RealDevice
= OldVpb
->RealDevice
;
877 Vcb
->SwapVpb
->RealDevice
->Vpb
= Vcb
->SwapVpb
;
879 Vcb
->SwapVpb
->Flags
= FlagOn( OldVpb
->Flags
, VPB_REMOVE_PENDING
);
881 IoReleaseVpbSpinLock( SavedIrql
);
884 // Indicate we used up the swap.
889 CdUnlockVcb( IrpContext
, Vcb
);
892 // We want to leave the Vpb for the IO system. Mark it
893 // as being not mounted. Go ahead and delete the Vcb as
900 // Make sure to remove the last reference on the Vpb.
903 OldVpb
->ReferenceCount
-= 1;
905 OldVpb
->DeviceObject
= NULL
;
906 ClearFlag( Vcb
->Vpb
->Flags
, VPB_MOUNTED
);
907 ClearFlag( Vcb
->Vpb
->Flags
, VPB_LOCKED
);
910 // Clear the Vpb flag so we know not to delete it.
915 IoReleaseVpbSpinLock( SavedIrql
);
916 CdUnlockVcb( IrpContext
, Vcb
);
917 CdDeleteVcb( IrpContext
, Vcb
);
922 // Someone has already swapped in a new Vpb. If this is the final reference
923 // then the file system is responsible for deleting the Vpb.
926 } else if (FinalReference
) {
929 // Make sure to remove the last reference on the Vpb.
932 OldVpb
->ReferenceCount
-= 1;
934 IoReleaseVpbSpinLock( SavedIrql
);
935 CdUnlockVcb( IrpContext
, Vcb
);
936 CdDeleteVcb( IrpContext
, Vcb
);
940 // The current Vpb is no longer the Vpb for the device (the IO system
941 // has already allocated a new one). We leave our reference in the
942 // Vpb and will be responsible for deleting it at a later time.
947 IoReleaseVpbSpinLock( SavedIrql
);
948 CdUnlockVcb( IrpContext
, Vcb
);
952 // Let our caller know whether the Vcb is still present.