[CDFS_NEW] Fix headers inclusion so that it can work on a *nix platform
[reactos.git] / drivers / filesystems / cdfs_new / verfysup.c
1 /*++
2
3 Copyright (c) 1989-2000 Microsoft Corporation
4
5 Module Name:
6
7 VerfySup.c
8
9 Abstract:
10
11 This module implements the Cdfs Verification routines.
12
13
14 --*/
15
16 #include "cdprocs.h"
17
18 //
19 // The Bug check file id for this module
20 //
21
22 #define BugCheckFileId (CDFS_BUG_CHECK_VERFYSUP)
23
24 #ifdef ALLOC_PRAGMA
25 #pragma alloc_text(PAGE, CdVerifyFcbOperation)
26 #pragma alloc_text(PAGE, CdVerifyVcb)
27 #endif
28
29
30 NTSTATUS
31 CdPerformVerify (
32 IN PIRP_CONTEXT IrpContext,
33 IN PIRP Irp,
34 IN PDEVICE_OBJECT DeviceToVerify
35 )
36
37 /*++
38
39 Routine Description:
40
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.
44
45 No file system resources are held when this routine is called.
46
47 Arguments:
48
49 Irp - The irp to send off after all is well and done.
50
51 Device - The real device needing verification.
52
53 Return Value:
54
55 None.
56
57 --*/
58
59 {
60 PVCB Vcb;
61 NTSTATUS Status = STATUS_SUCCESS;
62 PIO_STACK_LOCATION IrpSp;
63
64 ASSERT_IRP_CONTEXT( IrpContext );
65 ASSERT_IRP( Irp );
66
67 //
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.
70 //
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.
74 //
75
76 if ((IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL) &&
77 ((IrpContext->MinorFunction == IRP_MN_MOUNT_VOLUME) ||
78 (IrpContext->MinorFunction == IRP_MN_VERIFY_VOLUME))) {
79
80 return CdFsdPostRequest( IrpContext, Irp );
81 }
82
83 //
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.
88 //
89
90 IrpSp = IoGetCurrentIrpStackLocation( Irp );
91
92 Vcb = &CONTAINING_RECORD( IrpSp->DeviceObject,
93 VOLUME_DEVICE_OBJECT,
94 DeviceObject )->Vcb;
95 try {
96
97 //
98 // Send down the verify FSCTL. Note that this is sent to the
99 // currently mounted volume, which may not be this one.
100 //
101 // We will allow Raw to mount this volume if we were doing a
102 // an absolute DASD open.
103 //
104
105 Status = IoVerifyVolume( DeviceToVerify, CdOperationIsDasdOpen( IrpContext));
106
107 //
108 // Acquire the Vcb so we're working with a stable VcbCondition.
109 //
110
111 CdAcquireVcbShared( IrpContext, Vcb, FALSE);
112
113 //
114 // If the verify operation completed it will return
115 // either STATUS_SUCCESS or STATUS_WRONG_VOLUME, exactly.
116 //
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.
121 //
122
123 if ((Status == STATUS_WRONG_VOLUME) &&
124 (Vcb->VcbCondition == VcbMounted)) {
125
126 Status = STATUS_SUCCESS;
127 }
128 else if ((STATUS_SUCCESS == Status) && (Vcb->VcbCondition != VcbMounted)) {
129
130 //
131 // If the verify succeeded, but our volume is not mounted,
132 // then some other volume is on the device.
133 //
134
135 Status = STATUS_WRONG_VOLUME;
136 }
137
138 //
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
142 // the Reparse path.
143 //
144
145 //
146 // If the device might need to go away then call our dismount routine.
147 //
148
149 if (((Vcb->VcbCondition == VcbNotMounted) ||
150 (Vcb->VcbCondition == VcbInvalid) ||
151 (Vcb->VcbCondition == VcbDismountInProgress)) &&
152 (Vcb->VcbReference <= CDFS_RESIDUAL_REFERENCE)) {
153
154 CdReleaseVcb( IrpContext, Vcb);
155
156 CdAcquireCdData( IrpContext );
157 CdCheckForDismount( IrpContext, Vcb, FALSE );
158 CdReleaseCdData( IrpContext );
159 }
160 else {
161
162 CdReleaseVcb( IrpContext, Vcb);
163 }
164
165 //
166 // If this is a create and the verify succeeded then complete the
167 // request with a REPARSE status.
168 //
169
170 if ((IrpContext->MajorFunction == IRP_MJ_CREATE) &&
171 (IrpSp->FileObject->RelatedFileObject == NULL) &&
172 ((Status == STATUS_SUCCESS) || (Status == STATUS_WRONG_VOLUME))) {
173
174 Irp->IoStatus.Information = IO_REMOUNT;
175
176 CdCompleteRequest( IrpContext, Irp, STATUS_REPARSE );
177 Status = STATUS_REPARSE;
178 Irp = NULL;
179 IrpContext = NULL;
180
181 //
182 // If there is still an error to process then call the Io system
183 // for a popup.
184 //
185
186 } else if ((Irp != NULL) && !NT_SUCCESS( Status )) {
187
188 //
189 // Fill in the device object if required.
190 //
191
192 if (IoIsErrorUserInduced( Status ) ) {
193
194 IoSetHardErrorOrVerifyDevice( Irp, DeviceToVerify );
195 }
196
197 CdNormalizeAndRaiseStatus( IrpContext, Status );
198 }
199
200 //
201 // If there is still an Irp, send it off to an Ex Worker thread.
202 //
203
204 if (IrpContext != NULL) {
205
206 Status = CdFsdPostRequest( IrpContext, Irp );
207 }
208
209 } except(CdExceptionFilter( IrpContext, GetExceptionInformation() )) {
210
211 //
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.
215 //
216
217 Status = CdProcessException( IrpContext, Irp, GetExceptionCode() );
218 }
219
220 return Status;
221 }
222
223 \f
224 BOOLEAN
225 CdCheckForDismount (
226 IN PIRP_CONTEXT IrpContext,
227 IN PVCB Vcb,
228 IN BOOLEAN Force
229 )
230
231 /*++
232
233 Routine Description:
234
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.
237
238 If the dismount is not currently underway and the user reference count
239 has gone to zero then we can begin the dismount.
240
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
244 the Vcb.
245
246 Arguments:
247
248 Vcb - Vcb for the volume to try to dismount.
249
250 Force - Whether we will force this volume to be dismounted.
251
252 Return Value:
253
254 BOOLEAN - True if the Vcb was not gone by the time this function finished,
255 False if it was deleted.
256
257 This is only a trustworthy indication to the caller if it had the vcb
258 exclusive itself.
259
260 --*/
261
262 {
263 BOOLEAN UnlockVcb = TRUE;
264 BOOLEAN VcbPresent = TRUE;
265 KIRQL SavedIrql;
266
267 ASSERT_IRP_CONTEXT( IrpContext );
268 ASSERT_VCB( Vcb );
269
270 ASSERT_EXCLUSIVE_CDDATA;
271
272 //
273 // Acquire and lock this Vcb to check the dismount state.
274 //
275
276 CdAcquireVcbExclusive( IrpContext, Vcb, FALSE );
277
278 //
279 // Lets get rid of any pending closes for this volume.
280 //
281
282 CdFspClose( Vcb );
283
284 CdLockVcb( IrpContext, Vcb );
285
286 //
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.
290 //
291
292 if (Vcb->VcbCondition != VcbDismountInProgress) {
293
294 if (Vcb->VcbUserReference <= CDFS_RESIDUAL_USER_REFERENCE || Force) {
295
296 CdUnlockVcb( IrpContext, Vcb );
297 UnlockVcb = FALSE;
298 VcbPresent = CdDismountVcb( IrpContext, Vcb );
299 }
300
301 //
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.
305 //
306
307 } else if (Vcb->VcbReference == 0) {
308
309 IoAcquireVpbSpinLock( &SavedIrql );
310
311 //
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.
315 //
316
317 if (Vcb->Vpb->ReferenceCount == 1) {
318
319 IoReleaseVpbSpinLock( SavedIrql );
320 CdUnlockVcb( IrpContext, Vcb );
321 UnlockVcb = FALSE;
322 CdDeleteVcb( IrpContext, Vcb );
323 VcbPresent = FALSE;
324
325 } else {
326
327 IoReleaseVpbSpinLock( SavedIrql );
328 }
329 }
330
331 //
332 // Unlock the Vcb if still held.
333 //
334
335 if (UnlockVcb) {
336
337 CdUnlockVcb( IrpContext, Vcb );
338 }
339
340 //
341 // Release any resources still acquired.
342 //
343
344 if (VcbPresent) {
345
346 CdReleaseVcb( IrpContext, Vcb );
347 }
348
349 return VcbPresent;
350 }
351
352
353 BOOLEAN
354 CdMarkDevForVerifyIfVcbMounted(
355 IN PVCB Vcb
356 )
357
358 /*++
359
360 Routine Description:
361
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.
365
366 Arguments:
367
368 Vcb - This is the volume to check.
369
370 Return Value:
371
372 TRUE if the device has been marked for verify here, FALSE otherwise.
373
374 --*/
375
376 {
377 BOOLEAN Marked = FALSE;
378 KIRQL SavedIrql;
379
380 IoAcquireVpbSpinLock( &SavedIrql );
381
382 if (Vcb->Vpb->RealDevice->Vpb == Vcb->Vpb) {
383
384 CdMarkRealDevForVerify( Vcb->Vpb->RealDevice);
385 Marked = TRUE;
386 }
387 else {
388
389 //
390 // Flag this to avoid the VPB spinlock in future passes.
391 //
392
393 SetFlag( Vcb->VcbState, VCB_STATE_VPB_NOT_ON_DEVICE);
394 }
395
396 IoReleaseVpbSpinLock( SavedIrql );
397
398 return Marked;
399 }
400
401
402 VOID
403 CdVerifyVcb (
404 IN PIRP_CONTEXT IrpContext,
405 IN PVCB Vcb
406 )
407
408 /*++
409
410 Routine Description:
411
412 This routine checks that the current Vcb is valid and currently mounted
413 on the device. It will raise on an error condition.
414
415 We check whether the volume needs verification and the current state
416 of the Vcb.
417
418 Arguments:
419
420 Vcb - This is the volume to verify.
421
422 Return Value:
423
424 None
425
426 --*/
427
428 {
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 */
435
436 PAGED_CODE();
437
438 //
439 // Fail immediately if the volume is in the progress of being dismounted
440 // or has been marked invalid.
441 //
442
443 if ((Vcb->VcbCondition == VcbInvalid) ||
444 ((Vcb->VcbCondition == VcbDismountInProgress) &&
445 (IrpContext->MajorFunction != IRP_MJ_CREATE))) {
446
447 CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
448 }
449
450 if (FlagOn( Vcb->VcbState, VCB_STATE_REMOVABLE_MEDIA )) {
451
452 //
453 // Capture the real device verify state.
454 //
455
456 DevMarkedForVerify = CdRealDevNeedsVerify( Vcb->Vpb->RealDevice);
457
458 //
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.
462 //
463
464 if (Vcb->VcbCondition != VcbMountInProgress) {
465
466 Status = CdPerformDevIoCtrl( IrpContext,
467 IOCTL_CDROM_CHECK_VERIFY,
468 Vcb->TargetDeviceObject,
469 &MediaChangeCount,
470 sizeof(ULONG),
471 FALSE,
472 FALSE,
473 &Iosb );
474
475 if (Iosb.Information != sizeof(ULONG)) {
476
477 //
478 // Be safe about the count in case the driver didn't fill it in
479 //
480
481 MediaChangeCount = 0;
482 }
483
484 //
485 // There are four cases when we want to do a verify. These are the
486 // first three.
487 //
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
492 //
493
494 if (((Vcb->VcbCondition == VcbMounted) &&
495 CdIsRawDevice( IrpContext, Status ))
496 ||
497 (Status == STATUS_VERIFY_REQUIRED)
498 ||
499 (NT_SUCCESS(Status) &&
500 (Vcb->MediaChangeCount != MediaChangeCount))) {
501
502 //
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.
506 //
507
508 if (!FlagOn( Vcb->VcbState, VCB_STATE_VPB_NOT_ON_DEVICE) &&
509 !DevMarkedForVerify) {
510
511 DevMarkedForVerify = CdMarkDevForVerifyIfVcbMounted( Vcb);
512 }
513
514 ForceVerify = TRUE;
515
516 //
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.
520 //
521 }
522 }
523
524 //
525 // This is the 4th verify case.
526 //
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.
535 //
536
537 if (NT_SUCCESS( Status) && !ForceVerify &&
538 (IrpContext->MajorFunction == IRP_MJ_CREATE)) {
539
540 PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( IrpContext->Irp);
541
542 ForceVerify = (IrpSp->FileObject->RelatedFileObject == NULL) &&
543 ((Vcb->VcbCondition == VcbDismountInProgress) ||
544 (Vcb->VcbCondition == VcbNotMounted));
545
546 //
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.
549 //
550 }
551
552 //
553 // Raise the verify / error if necessary.
554 //
555
556 if (ForceVerify || !NT_SUCCESS( Status)) {
557
558 IoSetHardErrorOrVerifyDevice( IrpContext->Irp,
559 Vcb->Vpb->RealDevice );
560
561 CdRaiseStatus( IrpContext, ForceVerify ? STATUS_VERIFY_REQUIRED : Status);
562 }
563 }
564
565 //
566 // Based on the condition of the Vcb we'll either return to our
567 // caller or raise an error condition
568 //
569
570 switch (Vcb->VcbCondition) {
571
572 case VcbNotMounted:
573
574 IoSetHardErrorOrVerifyDevice( IrpContext->Irp, Vcb->Vpb->RealDevice );
575
576 CdRaiseStatus( IrpContext, STATUS_WRONG_VOLUME );
577 break;
578
579 case VcbInvalid:
580 case VcbDismountInProgress :
581
582 CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
583 break;
584
585 /* ReactOS Change: GCC "enumeration value not handled in switch" */
586 default: break;
587 }
588 }
589
590
591 BOOLEAN
592 CdVerifyFcbOperation (
593 IN PIRP_CONTEXT IrpContext OPTIONAL,
594 IN PFCB Fcb
595 )
596
597 /*++
598
599 Routine Description:
600
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.
604
605 Arguments:
606
607 IrpContext - IrpContext for the request. If not present then we
608 were called from the fast IO path.
609
610 Fcb - Fcb to perform the request on.
611
612 Return Value:
613
614 BOOLEAN - TRUE if the request can continue, FALSE otherwise.
615
616 --*/
617
618 {
619 //NTSTATUS Status = STATUS_SUCCESS; /* ReactOS Change: GCC Unused variable */
620 PVCB Vcb = Fcb->Vcb;
621 PDEVICE_OBJECT RealDevice = Vcb->Vpb->RealDevice;
622 PIRP Irp;
623
624 PAGED_CODE();
625
626 //
627 // Check that the fileobject has not been cleaned up.
628 //
629
630 if ( ARGUMENT_PRESENT( IrpContext )) {
631
632 PFILE_OBJECT FileObject;
633
634 Irp = IrpContext->Irp;
635 FileObject = IoGetCurrentIrpStackLocation( Irp)->FileObject;
636
637 if ( FileObject && FlagOn( FileObject->Flags, FO_CLEANUP_COMPLETE)) {
638
639 PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
640
641 //
642 // Following FAT, we allow certain operations even on cleaned up
643 // file objects. Everything else, we fail.
644 //
645
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) ) ) {
651
652 NOTHING;
653
654 } else {
655
656 CdRaiseStatus( IrpContext, STATUS_FILE_CLOSED );
657 }
658 }
659 }
660
661 //
662 // Fail immediately if the volume is in the progress of being dismounted
663 // or has been marked invalid.
664 //
665
666 if ((Vcb->VcbCondition == VcbInvalid) ||
667 (Vcb->VcbCondition == VcbDismountInProgress)) {
668
669 if (ARGUMENT_PRESENT( IrpContext )) {
670
671 CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
672 }
673
674 return FALSE;
675 }
676
677 //
678 // Always fail if the volume needs to be verified.
679 //
680
681 if (CdRealDevNeedsVerify( RealDevice)) {
682
683 if (ARGUMENT_PRESENT( IrpContext )) {
684
685 IoSetHardErrorOrVerifyDevice( IrpContext->Irp,
686 RealDevice );
687
688 CdRaiseStatus( IrpContext, STATUS_VERIFY_REQUIRED );
689 }
690
691 return FALSE;
692
693 //
694 //
695 // All operations are allowed on mounted.
696 //
697
698 } else if ((Vcb->VcbCondition == VcbMounted) ||
699 (Vcb->VcbCondition == VcbMountInProgress)) {
700
701 return TRUE;
702
703 //
704 // Fail all requests for fast Io on other Vcb conditions.
705 //
706
707 } else if (!ARGUMENT_PRESENT( IrpContext )) {
708
709 return FALSE;
710
711 //
712 // The remaining case is VcbNotMounted.
713 // Mark the device to be verified and raise WRONG_VOLUME.
714 //
715
716 } else if (Vcb->VcbCondition == VcbNotMounted) {
717
718 if (ARGUMENT_PRESENT( IrpContext )) {
719
720 IoSetHardErrorOrVerifyDevice( IrpContext->Irp, RealDevice );
721 CdRaiseStatus( IrpContext, STATUS_WRONG_VOLUME );
722 }
723
724 return FALSE;
725 }
726
727 return TRUE;
728 }
729
730 \f
731 BOOLEAN
732 CdDismountVcb (
733 IN PIRP_CONTEXT IrpContext,
734 IN PVCB Vcb
735 )
736
737 /*++
738
739 Routine Description:
740
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.
743
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.
748
749 Arguments:
750
751 Vcb - Vcb for the volume to dismount.
752
753 Return Value:
754
755 BOOLEAN - TRUE if we didn't delete the Vcb, FALSE otherwise.
756
757 --*/
758
759 {
760 PVPB OldVpb;
761 BOOLEAN VcbPresent = TRUE;
762 KIRQL SavedIrql;
763
764 BOOLEAN FinalReference;
765
766 ASSERT_EXCLUSIVE_CDDATA;
767 ASSERT_EXCLUSIVE_VCB( Vcb );
768
769 CdLockVcb( IrpContext, Vcb );
770
771 //
772 // We should only take this path once.
773 //
774
775 ASSERT( Vcb->VcbCondition != VcbDismountInProgress );
776
777 //
778 // Mark the Vcb as DismountInProgress.
779 //
780
781 Vcb->VcbCondition = VcbDismountInProgress;
782
783 if (Vcb->XASector != NULL) {
784
785 CdFreePool( &Vcb->XASector );
786 Vcb->XASector = 0;
787 Vcb->XADiskOffset = 0;
788 }
789
790 //
791 // Remove our reference to the internal Fcb's. The Fcb's will then
792 // be removed in the purge path below.
793 //
794
795 if (Vcb->RootIndexFcb != NULL) {
796
797 Vcb->RootIndexFcb->FcbReference -= 1;
798 Vcb->RootIndexFcb->FcbUserReference -= 1;
799 }
800
801 if (Vcb->PathTableFcb != NULL) {
802
803 Vcb->PathTableFcb->FcbReference -= 1;
804 Vcb->PathTableFcb->FcbUserReference -= 1;
805 }
806
807 if (Vcb->VolumeDasdFcb != NULL) {
808
809 Vcb->VolumeDasdFcb->FcbReference -= 1;
810 Vcb->VolumeDasdFcb->FcbUserReference -= 1;
811 }
812
813 CdUnlockVcb( IrpContext, Vcb );
814
815 //
816 // Purge the volume.
817 //
818
819 CdPurgeVolume( IrpContext, Vcb, TRUE );
820
821 //
822 // Empty the delayed and async close queues.
823 //
824
825 CdFspClose( Vcb );
826
827 OldVpb = Vcb->Vpb;
828
829 //
830 // Remove the mount volume reference.
831 //
832
833 CdLockVcb( IrpContext, Vcb );
834 Vcb->VcbReference -= 1;
835
836 //
837 // Acquire the Vpb spinlock to check for Vpb references.
838 //
839
840 IoAcquireVpbSpinLock( &SavedIrql );
841
842 //
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
847 // Vpb.
848 //
849
850 FinalReference = (BOOLEAN) ((Vcb->VcbReference == 0) &&
851 (OldVpb->ReferenceCount == 1));
852
853 //
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
858 // mount request.
859 //
860
861 if (OldVpb->RealDevice->Vpb == OldVpb) {
862
863 //
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.
867 //
868
869 if (!FinalReference) {
870
871 ASSERT( Vcb->SwapVpb != NULL );
872
873 Vcb->SwapVpb->Type = IO_TYPE_VPB;
874 Vcb->SwapVpb->Size = sizeof( VPB );
875 Vcb->SwapVpb->RealDevice = OldVpb->RealDevice;
876
877 Vcb->SwapVpb->RealDevice->Vpb = Vcb->SwapVpb;
878
879 Vcb->SwapVpb->Flags = FlagOn( OldVpb->Flags, VPB_REMOVE_PENDING );
880
881 IoReleaseVpbSpinLock( SavedIrql );
882
883 //
884 // Indicate we used up the swap.
885 //
886
887 Vcb->SwapVpb = NULL;
888
889 CdUnlockVcb( IrpContext, Vcb );
890
891 //
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
894 // well.
895 //
896
897 } else {
898
899 //
900 // Make sure to remove the last reference on the Vpb.
901 //
902
903 OldVpb->ReferenceCount -= 1;
904
905 OldVpb->DeviceObject = NULL;
906 ClearFlag( Vcb->Vpb->Flags, VPB_MOUNTED );
907 ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED );
908
909 //
910 // Clear the Vpb flag so we know not to delete it.
911 //
912
913 Vcb->Vpb = NULL;
914
915 IoReleaseVpbSpinLock( SavedIrql );
916 CdUnlockVcb( IrpContext, Vcb );
917 CdDeleteVcb( IrpContext, Vcb );
918 VcbPresent = FALSE;
919 }
920
921 //
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.
924 //
925
926 } else if (FinalReference) {
927
928 //
929 // Make sure to remove the last reference on the Vpb.
930 //
931
932 OldVpb->ReferenceCount -= 1;
933
934 IoReleaseVpbSpinLock( SavedIrql );
935 CdUnlockVcb( IrpContext, Vcb );
936 CdDeleteVcb( IrpContext, Vcb );
937 VcbPresent = FALSE;
938
939 //
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.
943 //
944
945 } else {
946
947 IoReleaseVpbSpinLock( SavedIrql );
948 CdUnlockVcb( IrpContext, Vcb );
949 }
950
951 //
952 // Let our caller know whether the Vcb is still present.
953 //
954
955 return VcbPresent;
956 }
957