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.

973 lines
25 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. VerfySup.c
  5. Abstract:
  6. This module implements the Cdfs Verification routines.
  7. // @@BEGIN_DDKSPLIT
  8. Author:
  9. Brian Andrew [BrianAn] 01-July-1995
  10. Revision History:
  11. // @@END_DDKSPLIT
  12. --*/
  13. #include "CdProcs.h"
  14. //
  15. // The Bug check file id for this module
  16. //
  17. #define BugCheckFileId (CDFS_BUG_CHECK_VERFYSUP)
  18. #ifdef ALLOC_PRAGMA
  19. #pragma alloc_text(PAGE, CdVerifyFcbOperation)
  20. #pragma alloc_text(PAGE, CdVerifyVcb)
  21. #endif
  22. NTSTATUS
  23. CdPerformVerify (
  24. IN PIRP_CONTEXT IrpContext,
  25. IN PIRP Irp,
  26. IN PDEVICE_OBJECT DeviceToVerify
  27. )
  28. /*++
  29. Routine Description:
  30. This routines performs an IoVerifyVolume operation and takes the
  31. appropriate action. If the verify is successful then we send the originating
  32. Irp off to an Ex Worker Thread. This routine is called from the exception handler.
  33. No file system resources are held when this routine is called.
  34. Arguments:
  35. Irp - The irp to send off after all is well and done.
  36. Device - The real device needing verification.
  37. Return Value:
  38. None.
  39. --*/
  40. {
  41. PVCB Vcb;
  42. NTSTATUS Status = STATUS_SUCCESS;
  43. PIO_STACK_LOCATION IrpSp;
  44. ASSERT_IRP_CONTEXT( IrpContext );
  45. ASSERT_IRP( Irp );
  46. //
  47. // Check if this Irp has a status of Verify required and if it does
  48. // then call the I/O system to do a verify.
  49. //
  50. // Skip the IoVerifyVolume if this is a mount or verify request
  51. // itself. Trying a recursive mount will cause a deadlock with
  52. // the DeviceObject->DeviceLock.
  53. //
  54. if ((IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL) &&
  55. ((IrpContext->MinorFunction == IRP_MN_MOUNT_VOLUME) ||
  56. (IrpContext->MinorFunction == IRP_MN_VERIFY_VOLUME))) {
  57. return CdFsdPostRequest( IrpContext, Irp );
  58. }
  59. //
  60. // Extract a pointer to the Vcb from the VolumeDeviceObject.
  61. // Note that since we have specifically excluded mount,
  62. // requests, we know that IrpSp->DeviceObject is indeed a
  63. // volume device object.
  64. //
  65. IrpSp = IoGetCurrentIrpStackLocation( Irp );
  66. Vcb = &CONTAINING_RECORD( IrpSp->DeviceObject,
  67. VOLUME_DEVICE_OBJECT,
  68. DeviceObject )->Vcb;
  69. try {
  70. BOOLEAN AllowRawMount = FALSE;
  71. //
  72. // We will allow Raw to mount this volume if we were doing a
  73. // an absolute DASD open.
  74. //
  75. if ((IrpContext->MajorFunction == IRP_MJ_CREATE) &&
  76. (IrpSp->FileObject->FileName.Length == 0) &&
  77. (IrpSp->FileObject->RelatedFileObject == NULL)) {
  78. AllowRawMount = TRUE;
  79. }
  80. //
  81. // Send down the verify FSCTL. Note that this is sent to the
  82. // currently mounted volume, which may not be this one.
  83. //
  84. Status = IoVerifyVolume( DeviceToVerify, AllowRawMount );
  85. //
  86. // Acquire the Vcb so we're working with a stable VcbCondition.
  87. //
  88. CdAcquireVcbShared( IrpContext, Vcb, FALSE);
  89. //
  90. // If the verify operation completed it will return
  91. // either STATUS_SUCCESS or STATUS_WRONG_VOLUME, exactly.
  92. //
  93. // If CdVerifyVolume encountered an error during
  94. // processing, it will return that error. If we got
  95. // STATUS_WRONG_VOLUME from the verify, and our volume
  96. // is now mounted, commute the status to STATUS_SUCCESS.
  97. //
  98. if ((Status == STATUS_WRONG_VOLUME) &&
  99. (Vcb->VcbCondition == VcbMounted)) {
  100. Status = STATUS_SUCCESS;
  101. }
  102. else if ((STATUS_SUCCESS == Status) && (Vcb->VcbCondition != VcbMounted)) {
  103. //
  104. // If the verify succeeded, but our volume is not mounted,
  105. // then some other volume is on the device.
  106. //
  107. Status = STATUS_WRONG_VOLUME;
  108. }
  109. //
  110. // Do a quick unprotected check here. The routine will do
  111. // a safe check. After here we can release the resource.
  112. // Note that if the volume really went away, we will be taking
  113. // the Reparse path.
  114. //
  115. //
  116. // If the device might need to go away then call our dismount routine.
  117. //
  118. if (((Vcb->VcbCondition == VcbNotMounted) ||
  119. (Vcb->VcbCondition == VcbInvalid) ||
  120. (Vcb->VcbCondition == VcbDismountInProgress)) &&
  121. (Vcb->VcbReference <= CDFS_RESIDUAL_REFERENCE)) {
  122. CdReleaseVcb( IrpContext, Vcb);
  123. CdAcquireCdData( IrpContext );
  124. CdCheckForDismount( IrpContext, Vcb, FALSE );
  125. CdReleaseCdData( IrpContext );
  126. }
  127. else {
  128. CdReleaseVcb( IrpContext, Vcb);
  129. }
  130. //
  131. // If this is a create and the verify succeeded then complete the
  132. // request with a REPARSE status.
  133. //
  134. if ((IrpContext->MajorFunction == IRP_MJ_CREATE) &&
  135. (IrpSp->FileObject->RelatedFileObject == NULL) &&
  136. ((Status == STATUS_SUCCESS) || (Status == STATUS_WRONG_VOLUME))) {
  137. Irp->IoStatus.Information = IO_REMOUNT;
  138. CdCompleteRequest( IrpContext, Irp, STATUS_REPARSE );
  139. Status = STATUS_REPARSE;
  140. Irp = NULL;
  141. IrpContext = NULL;
  142. //
  143. // If there is still an error to process then call the Io system
  144. // for a popup.
  145. //
  146. } else if ((Irp != NULL) && !NT_SUCCESS( Status )) {
  147. //
  148. // Fill in the device object if required.
  149. //
  150. if (IoIsErrorUserInduced( Status ) ) {
  151. IoSetHardErrorOrVerifyDevice( Irp, DeviceToVerify );
  152. }
  153. CdNormalizeAndRaiseStatus( IrpContext, Status );
  154. }
  155. //
  156. // If there is still an Irp, send it off to an Ex Worker thread.
  157. //
  158. if (IrpContext != NULL) {
  159. Status = CdFsdPostRequest( IrpContext, Irp );
  160. }
  161. } except(CdExceptionFilter( IrpContext, GetExceptionInformation() )) {
  162. //
  163. // We had some trouble trying to perform the verify or raised
  164. // an error ourselves. So we'll abort the I/O request with
  165. // the error status that we get back from the execption code.
  166. //
  167. Status = CdProcessException( IrpContext, Irp, GetExceptionCode() );
  168. }
  169. return Status;
  170. }
  171. BOOLEAN
  172. CdCheckForDismount (
  173. IN PIRP_CONTEXT IrpContext,
  174. IN PVCB Vcb,
  175. IN BOOLEAN Force
  176. )
  177. /*++
  178. Routine Description:
  179. This routine is called to check if a volume is ready for dismount. This
  180. occurs when only file system references are left on the volume.
  181. If the dismount is not currently underway and the user reference count
  182. has gone to zero then we can begin the dismount.
  183. If the dismount is in progress and there are no references left on the
  184. volume (we check the Vpb for outstanding references as well to catch
  185. any create calls dispatched to the file system) then we can delete
  186. the Vcb.
  187. Arguments:
  188. Vcb - Vcb for the volume to try to dismount.
  189. Force - Whether we will force this volume to be dismounted.
  190. Return Value:
  191. BOOLEAN - True if the Vcb was not gone by the time this function finished,
  192. False if it was deleted.
  193. This is only a trustworthy indication to the caller if it had the vcb
  194. exclusive itself.
  195. --*/
  196. {
  197. BOOLEAN UnlockVcb = TRUE;
  198. BOOLEAN VcbPresent = TRUE;
  199. KIRQL SavedIrql;
  200. ASSERT_IRP_CONTEXT( IrpContext );
  201. ASSERT_VCB( Vcb );
  202. ASSERT_EXCLUSIVE_CDDATA;
  203. //
  204. // Acquire and lock this Vcb to check the dismount state.
  205. //
  206. CdAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  207. //
  208. // Lets get rid of any pending closes for this volume.
  209. //
  210. CdFspClose( Vcb );
  211. CdLockVcb( IrpContext, Vcb );
  212. //
  213. // If the dismount is not already underway then check if the
  214. // user reference count has gone to zero or we are being forced
  215. // to disconnect. If so start the teardown on the Vcb.
  216. //
  217. if (Vcb->VcbCondition != VcbDismountInProgress) {
  218. if (Vcb->VcbUserReference <= CDFS_RESIDUAL_USER_REFERENCE || Force) {
  219. CdUnlockVcb( IrpContext, Vcb );
  220. UnlockVcb = FALSE;
  221. VcbPresent = CdDismountVcb( IrpContext, Vcb );
  222. }
  223. //
  224. // If the teardown is underway and there are absolutely no references
  225. // remaining then delete the Vcb. References here include the
  226. // references in the Vcb and Vpb.
  227. //
  228. } else if (Vcb->VcbReference == 0) {
  229. IoAcquireVpbSpinLock( &SavedIrql );
  230. //
  231. // If there are no file objects and no reference counts in the
  232. // Vpb we can delete the Vcb. Don't forget that we have the
  233. // last reference in the Vpb.
  234. //
  235. if (Vcb->Vpb->ReferenceCount == 1) {
  236. IoReleaseVpbSpinLock( SavedIrql );
  237. CdUnlockVcb( IrpContext, Vcb );
  238. UnlockVcb = FALSE;
  239. CdDeleteVcb( IrpContext, Vcb );
  240. VcbPresent = FALSE;
  241. } else {
  242. IoReleaseVpbSpinLock( SavedIrql );
  243. }
  244. }
  245. //
  246. // Unlock the Vcb if still held.
  247. //
  248. if (UnlockVcb) {
  249. CdUnlockVcb( IrpContext, Vcb );
  250. }
  251. //
  252. // Release any resources still acquired.
  253. //
  254. if (VcbPresent) {
  255. CdReleaseVcb( IrpContext, Vcb );
  256. }
  257. return VcbPresent;
  258. }
  259. BOOLEAN
  260. CdMarkDevForVerifyIfVcbMounted(
  261. IN PVCB Vcb
  262. )
  263. /*++
  264. Routine Description:
  265. This routine checks to see if the specified Vcb is currently mounted on
  266. the device or not. If it is, it sets the verify flag on the device, if
  267. not then the state is noted in the Vcb.
  268. Arguments:
  269. Vcb - This is the volume to check.
  270. Return Value:
  271. TRUE if the device has been marked for verify here, FALSE otherwise.
  272. --*/
  273. {
  274. BOOLEAN Marked = FALSE;
  275. KIRQL SavedIrql;
  276. IoAcquireVpbSpinLock( &SavedIrql );
  277. if (Vcb->Vpb->RealDevice->Vpb == Vcb->Vpb) {
  278. CdMarkRealDevForVerify( Vcb->Vpb->RealDevice);
  279. Marked = TRUE;
  280. }
  281. else {
  282. //
  283. // Flag this to avoid the VPB spinlock in future passes.
  284. //
  285. SetFlag( Vcb->VcbState, VCB_STATE_VPB_NOT_ON_DEVICE);
  286. }
  287. IoReleaseVpbSpinLock( SavedIrql );
  288. return Marked;
  289. }
  290. VOID
  291. CdVerifyVcb (
  292. IN PIRP_CONTEXT IrpContext,
  293. IN PVCB Vcb
  294. )
  295. /*++
  296. Routine Description:
  297. This routine checks that the current Vcb is valid and currently mounted
  298. on the device. It will raise on an error condition.
  299. We check whether the volume needs verification and the current state
  300. of the Vcb.
  301. Arguments:
  302. Vcb - This is the volume to verify.
  303. Return Value:
  304. None
  305. --*/
  306. {
  307. NTSTATUS Status = STATUS_SUCCESS;
  308. IO_STATUS_BLOCK Iosb;
  309. ULONG MediaChangeCount = 0;
  310. BOOLEAN ForceVerify = FALSE;
  311. BOOLEAN DevMarkedForVerify;
  312. KIRQL SavedIrql;
  313. PAGED_CODE();
  314. //
  315. // Fail immediately if the volume is in the progress of being dismounted
  316. // or has been marked invalid.
  317. //
  318. if ((Vcb->VcbCondition == VcbInvalid) ||
  319. ((Vcb->VcbCondition == VcbDismountInProgress) &&
  320. (IrpContext->MajorFunction != IRP_MJ_CREATE))) {
  321. CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
  322. }
  323. if (FlagOn( Vcb->VcbState, VCB_STATE_REMOVABLE_MEDIA )) {
  324. //
  325. // Capture the real device verify state.
  326. //
  327. DevMarkedForVerify = CdRealDevNeedsVerify( Vcb->Vpb->RealDevice);
  328. //
  329. // If the media is removable and the verify volume flag in the
  330. // device object is not set then we want to ping the device
  331. // to see if it needs to be verified.
  332. //
  333. if (Vcb->VcbCondition != VcbMountInProgress) {
  334. Status = CdPerformDevIoCtrl( IrpContext,
  335. IOCTL_CDROM_CHECK_VERIFY,
  336. Vcb->TargetDeviceObject,
  337. &MediaChangeCount,
  338. sizeof(ULONG),
  339. FALSE,
  340. FALSE,
  341. &Iosb );
  342. if (Iosb.Information != sizeof(ULONG)) {
  343. //
  344. // Be safe about the count in case the driver didn't fill it in
  345. //
  346. MediaChangeCount = 0;
  347. }
  348. //
  349. // There are four cases when we want to do a verify. These are the
  350. // first three.
  351. //
  352. // 1. We are mounted, and the device has become empty
  353. // 2. The device has returned verify required (=> DO_VERIFY_VOL flag is
  354. // set, but could be due to hardware condition)
  355. // 3. Media change count doesn't match the one in the Vcb
  356. //
  357. if (((Vcb->VcbCondition == VcbMounted) &&
  358. CdIsRawDevice( IrpContext, Status ))
  359. ||
  360. (Status == STATUS_VERIFY_REQUIRED)
  361. ||
  362. (NT_SUCCESS(Status) &&
  363. (Vcb->MediaChangeCount != MediaChangeCount))) {
  364. //
  365. // If we are currently the volume on the device then it is our
  366. // responsibility to set the verify flag. If we're not on the device,
  367. // then we shouldn't touch the flag.
  368. //
  369. if (!FlagOn( Vcb->VcbState, VCB_STATE_VPB_NOT_ON_DEVICE) &&
  370. !DevMarkedForVerify) {
  371. DevMarkedForVerify = CdMarkDevForVerifyIfVcbMounted( Vcb);
  372. }
  373. ForceVerify = TRUE;
  374. //
  375. // NOTE that we no longer update the media change count here. We
  376. // do so only when we've actually completed a verify at a particular
  377. // change count value.
  378. //
  379. }
  380. }
  381. //
  382. // This is the 4th verify case.
  383. //
  384. // We ALWAYS force CREATE requests on unmounted volumes through the
  385. // verify path. These requests could have been in limbo between
  386. // IoCheckMountedVpb and us when a verify/mount took place and caused
  387. // a completely different fs/volume to be mounted. In this case the
  388. // checks above may not have caught the condition, since we may already
  389. // have verified (wrong volume) and decided that we have nothing to do.
  390. // We want the requests to be re routed to the currently mounted volume,
  391. // since they were directed at the 'drive', not our volume.
  392. //
  393. if (NT_SUCCESS( Status) && !ForceVerify &&
  394. (IrpContext->MajorFunction == IRP_MJ_CREATE)) {
  395. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( IrpContext->Irp);
  396. ForceVerify = (IrpSp->FileObject->RelatedFileObject == NULL) &&
  397. ((Vcb->VcbCondition == VcbDismountInProgress) ||
  398. (Vcb->VcbCondition == VcbNotMounted));
  399. //
  400. // Note that we don't touch the device verify flag here. If required
  401. // it would have been caught and set by the first set of checks.
  402. //
  403. }
  404. //
  405. // Raise the verify / error if neccessary.
  406. //
  407. if (ForceVerify || !NT_SUCCESS( Status)) {
  408. IoSetHardErrorOrVerifyDevice( IrpContext->Irp,
  409. Vcb->Vpb->RealDevice );
  410. CdRaiseStatus( IrpContext, ForceVerify ? STATUS_VERIFY_REQUIRED : Status);
  411. }
  412. }
  413. //
  414. // Based on the condition of the Vcb we'll either return to our
  415. // caller or raise an error condition
  416. //
  417. switch (Vcb->VcbCondition) {
  418. case VcbNotMounted:
  419. IoSetHardErrorOrVerifyDevice( IrpContext->Irp, Vcb->Vpb->RealDevice );
  420. CdRaiseStatus( IrpContext, STATUS_WRONG_VOLUME );
  421. break;
  422. case VcbInvalid:
  423. case VcbDismountInProgress :
  424. CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
  425. break;
  426. }
  427. }
  428. BOOLEAN
  429. CdVerifyFcbOperation (
  430. IN PIRP_CONTEXT IrpContext OPTIONAL,
  431. IN PFCB Fcb
  432. )
  433. /*++
  434. Routine Description:
  435. This routine is called to verify that the state of the Fcb is valid
  436. to allow the current operation to continue. We use the state of the
  437. Vcb, target device and type of operation to determine this.
  438. Arguments:
  439. IrpContext - IrpContext for the request. If not present then we
  440. were called from the fast IO path.
  441. Fcb - Fcb to perform the request on.
  442. Return Value:
  443. BOOLEAN - TRUE if the request can continue, FALSE otherwise.
  444. --*/
  445. {
  446. NTSTATUS Status = STATUS_SUCCESS;
  447. PVCB Vcb = Fcb->Vcb;
  448. PDEVICE_OBJECT RealDevice = Vcb->Vpb->RealDevice;
  449. PIRP Irp;
  450. PAGED_CODE();
  451. //
  452. // Check that the fileobject has not been cleaned up.
  453. //
  454. if ( ARGUMENT_PRESENT( IrpContext )) {
  455. PFILE_OBJECT FileObject;
  456. Irp = IrpContext->Irp;
  457. FileObject = IoGetCurrentIrpStackLocation( Irp)->FileObject;
  458. if ( FileObject && FlagOn( FileObject->Flags, FO_CLEANUP_COMPLETE)) {
  459. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
  460. //
  461. // Following FAT, we allow certain operations even on cleaned up
  462. // file objects. Everything else, we fail.
  463. //
  464. if ( (FlagOn(Irp->Flags, IRP_PAGING_IO)) ||
  465. (IrpSp->MajorFunction == IRP_MJ_CLOSE ) ||
  466. (IrpSp->MajorFunction == IRP_MJ_QUERY_INFORMATION) ||
  467. ( (IrpSp->MajorFunction == IRP_MJ_READ) &&
  468. FlagOn(IrpSp->MinorFunction, IRP_MN_COMPLETE) ) ) {
  469. NOTHING;
  470. } else {
  471. CdRaiseStatus( IrpContext, STATUS_FILE_CLOSED );
  472. }
  473. }
  474. }
  475. //
  476. // Fail immediately if the volume is in the progress of being dismounted
  477. // or has been marked invalid.
  478. //
  479. if ((Vcb->VcbCondition == VcbInvalid) ||
  480. (Vcb->VcbCondition == VcbDismountInProgress)) {
  481. if (ARGUMENT_PRESENT( IrpContext )) {
  482. CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
  483. }
  484. return FALSE;
  485. }
  486. //
  487. // Always fail if the volume needs to be verified.
  488. //
  489. if (CdRealDevNeedsVerify( RealDevice)) {
  490. if (ARGUMENT_PRESENT( IrpContext )) {
  491. IoSetHardErrorOrVerifyDevice( IrpContext->Irp,
  492. RealDevice );
  493. CdRaiseStatus( IrpContext, STATUS_VERIFY_REQUIRED );
  494. }
  495. return FALSE;
  496. //
  497. //
  498. // All operations are allowed on mounted.
  499. //
  500. } else if ((Vcb->VcbCondition == VcbMounted) ||
  501. (Vcb->VcbCondition == VcbMountInProgress)) {
  502. return TRUE;
  503. //
  504. // Fail all requests for fast Io on other Vcb conditions.
  505. //
  506. } else if (!ARGUMENT_PRESENT( IrpContext )) {
  507. return FALSE;
  508. //
  509. // The remaining case is VcbNotMounted.
  510. // Mark the device to be verified and raise WRONG_VOLUME.
  511. //
  512. } else if (Vcb->VcbCondition == VcbNotMounted) {
  513. if (ARGUMENT_PRESENT( IrpContext )) {
  514. IoSetHardErrorOrVerifyDevice( IrpContext->Irp, RealDevice );
  515. CdRaiseStatus( IrpContext, STATUS_WRONG_VOLUME );
  516. }
  517. return FALSE;
  518. }
  519. return TRUE;
  520. }
  521. BOOLEAN
  522. CdDismountVcb (
  523. IN PIRP_CONTEXT IrpContext,
  524. IN PVCB Vcb
  525. )
  526. /*++
  527. Routine Description:
  528. This routine is called when all of the user references to a volume are
  529. gone. We will initiate all of the teardown any system resources.
  530. If all of the references to this volume are gone at the end of this routine
  531. then we will complete the teardown of this Vcb and mark the current Vpb
  532. as not mounted. Otherwise we will allocated a new Vpb for this device
  533. and keep the current Vpb attached to the Vcb.
  534. Arguments:
  535. Vcb - Vcb for the volume to dismount.
  536. Return Value:
  537. BOOLEAN - TRUE if we didn't delete the Vcb, FALSE otherwise.
  538. --*/
  539. {
  540. PVPB OldVpb;
  541. BOOLEAN VcbPresent = TRUE;
  542. KIRQL SavedIrql;
  543. BOOLEAN FinalReference;
  544. ASSERT_EXCLUSIVE_CDDATA;
  545. ASSERT_EXCLUSIVE_VCB( Vcb );
  546. CdLockVcb( IrpContext, Vcb );
  547. //
  548. // We should only take this path once.
  549. //
  550. ASSERT( Vcb->VcbCondition != VcbDismountInProgress );
  551. //
  552. // Mark the Vcb as DismountInProgress.
  553. //
  554. Vcb->VcbCondition = VcbDismountInProgress;
  555. if (Vcb->XASector != NULL) {
  556. ExFreePool( Vcb->XASector );
  557. Vcb->XASector = 0;
  558. Vcb->XADiskOffset = 0;
  559. }
  560. //
  561. // Remove our reference to the internal Fcb's. The Fcb's will then
  562. // be removed in the purge path below.
  563. //
  564. if (Vcb->RootIndexFcb != NULL) {
  565. Vcb->RootIndexFcb->FcbReference -= 1;
  566. Vcb->RootIndexFcb->FcbUserReference -= 1;
  567. }
  568. if (Vcb->PathTableFcb != NULL) {
  569. Vcb->PathTableFcb->FcbReference -= 1;
  570. Vcb->PathTableFcb->FcbUserReference -= 1;
  571. }
  572. if (Vcb->VolumeDasdFcb != NULL) {
  573. Vcb->VolumeDasdFcb->FcbReference -= 1;
  574. Vcb->VolumeDasdFcb->FcbUserReference -= 1;
  575. }
  576. CdUnlockVcb( IrpContext, Vcb );
  577. //
  578. // Purge the volume.
  579. //
  580. CdPurgeVolume( IrpContext, Vcb, TRUE );
  581. //
  582. // Empty the delayed and async close queues.
  583. //
  584. CdFspClose( Vcb );
  585. OldVpb = Vcb->Vpb;
  586. //
  587. // Remove the mount volume reference.
  588. //
  589. CdLockVcb( IrpContext, Vcb );
  590. Vcb->VcbReference -= 1;
  591. //
  592. // Acquire the Vpb spinlock to check for Vpb references.
  593. //
  594. IoAcquireVpbSpinLock( &SavedIrql );
  595. //
  596. // Remember if this is the last reference on this Vcb. We incremented
  597. // the count on the Vpb earlier so we get one last crack it. If our
  598. // reference has gone to zero but the vpb reference count is greater
  599. // than zero then the Io system will be responsible for deleting the
  600. // Vpb.
  601. //
  602. FinalReference = (BOOLEAN) ((Vcb->VcbReference == 0) &&
  603. (OldVpb->ReferenceCount == 1));
  604. //
  605. // There is a reference count in the Vpb and in the Vcb. We have
  606. // incremented the reference count in the Vpb to make sure that
  607. // we have last crack at it. If this is a failed mount then we
  608. // want to return the Vpb to the IO system to use for the next
  609. // mount request.
  610. //
  611. if (OldVpb->RealDevice->Vpb == OldVpb) {
  612. //
  613. // If not the final reference then swap out the Vpb. We must
  614. // preserve the REMOVE_PENDING flag so that the device is
  615. // not remounted in the middle of a PnP remove operation.
  616. //
  617. if (!FinalReference) {
  618. ASSERT( Vcb->SwapVpb != NULL );
  619. Vcb->SwapVpb->Type = IO_TYPE_VPB;
  620. Vcb->SwapVpb->Size = sizeof( VPB );
  621. Vcb->SwapVpb->RealDevice = OldVpb->RealDevice;
  622. Vcb->SwapVpb->RealDevice->Vpb = Vcb->SwapVpb;
  623. Vcb->SwapVpb->Flags = FlagOn( OldVpb->Flags, VPB_REMOVE_PENDING );
  624. IoReleaseVpbSpinLock( SavedIrql );
  625. //
  626. // Indicate we used up the swap.
  627. //
  628. Vcb->SwapVpb = NULL;
  629. CdUnlockVcb( IrpContext, Vcb );
  630. //
  631. // We want to leave the Vpb for the IO system. Mark it
  632. // as being not mounted. Go ahead and delete the Vcb as
  633. // well.
  634. //
  635. } else {
  636. //
  637. // Make sure to remove the last reference on the Vpb.
  638. //
  639. OldVpb->ReferenceCount -= 1;
  640. OldVpb->DeviceObject = NULL;
  641. ClearFlag( Vcb->Vpb->Flags, VPB_MOUNTED );
  642. ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED );
  643. //
  644. // Clear the Vpb flag so we know not to delete it.
  645. //
  646. Vcb->Vpb = NULL;
  647. IoReleaseVpbSpinLock( SavedIrql );
  648. CdUnlockVcb( IrpContext, Vcb );
  649. CdDeleteVcb( IrpContext, Vcb );
  650. VcbPresent = FALSE;
  651. }
  652. //
  653. // Someone has already swapped in a new Vpb. If this is the final reference
  654. // then the file system is responsible for deleting the Vpb.
  655. //
  656. } else if (FinalReference) {
  657. //
  658. // Make sure to remove the last reference on the Vpb.
  659. //
  660. OldVpb->ReferenceCount -= 1;
  661. IoReleaseVpbSpinLock( SavedIrql );
  662. CdUnlockVcb( IrpContext, Vcb );
  663. CdDeleteVcb( IrpContext, Vcb );
  664. VcbPresent = FALSE;
  665. //
  666. // The current Vpb is no longer the Vpb for the device (the IO system
  667. // has already allocated a new one). We leave our reference in the
  668. // Vpb and will be responsible for deleting it at a later time.
  669. //
  670. } else {
  671. IoReleaseVpbSpinLock( SavedIrql );
  672. CdUnlockVcb( IrpContext, Vcb );
  673. }
  674. //
  675. // Let our caller know whether the Vcb is still present.
  676. //
  677. return VcbPresent;
  678. }