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.

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