Sample Code

windows driver samples/ cdfs file system driver/ C++/ verfysup.c/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*++
 
Copyright (c) 1989-2000 Microsoft Corporation
 
Module Name:
 
    VerfySup.c
 
Abstract:
 
    This module implements the Cdfs Verification routines.
 
 
--*/
 
#include "CdProcs.h"
 
//
//  The Bug check file id for this module
//
 
#define BugCheckFileId                   (CDFS_BUG_CHECK_VERFYSUP)
 
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, CdVerifyFcbOperation)
#pragma alloc_text(PAGE, CdVerifyVcb)
#endif
 
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
CdPerformVerify (
    _Inout_ PIRP_CONTEXT IrpContext,
    _Inout_ PIRP Irp,
    _In_ PDEVICE_OBJECT DeviceToVerify
    )
 
/*++
 
Routine Description:
 
    This routines performs an IoVerifyVolume operation and takes the
    appropriate action.  If the verify is successful then we send the originating
    Irp off to an Ex Worker Thread.  This routine is called from the exception handler.
 
    No file system resources are held when this routine is called.
 
Arguments:
 
    Irp - The irp to send off after all is well and done.
 
    Device - The real device needing verification.
 
Return Value:
 
    None.
 
--*/
 
{
    PVCB Vcb;
    NTSTATUS Status = STATUS_SUCCESS;
    PIO_STACK_LOCATION IrpSp;
 
    ASSERT_IRP_CONTEXT( IrpContext );
    ASSERT_IRP( Irp );
 
    //
    //  Check if this Irp has a status of Verify required and if it does
    //  then call the I/O system to do a verify.
    //
    //  Skip the IoVerifyVolume if this is a mount or verify request
    //  itself.  Trying a recursive mount will cause a deadlock with
    //  the DeviceObject->DeviceLock.
    //
 
    if ((IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL) &&
        ((IrpContext->MinorFunction == IRP_MN_MOUNT_VOLUME) ||
         (IrpContext->MinorFunction == IRP_MN_VERIFY_VOLUME))) {
 
        return CdFsdPostRequest( IrpContext, Irp );
    }
 
    //
    //  Extract a pointer to the Vcb from the VolumeDeviceObject.
    //  Note that since we have specifically excluded mount,
    //  requests, we know that IrpSp->DeviceObject is indeed a
    //  volume device object.
    //
 
    IrpSp = IoGetCurrentIrpStackLocation( Irp );
 
    Vcb = &CONTAINING_RECORD( IrpSp->DeviceObject,
                              VOLUME_DEVICE_OBJECT,
                              DeviceObject )->Vcb;
    try {
 
        //
        //  Send down the verify FSCTL.  Note that this is sent to the
        //  currently mounted volume,  which may not be this one.
        //
        //  We will allow Raw to mount this volume if we were doing a
        //  an absolute DASD open.
        //
 
        Status = IoVerifyVolume( DeviceToVerify, CdOperationIsDasdOpen( IrpContext));
 
        //
        //  Acquire the Vcb so we're working with a stable VcbCondition.
        //
         
        CdAcquireVcbShared( IrpContext, Vcb, FALSE);
         
        //
        //  If the verify operation completed it will return
        //  either STATUS_SUCCESS or STATUS_WRONG_VOLUME, exactly.
        //
        //  If CdVerifyVolume encountered an error during
        //  processing, it will return that error.  If we got
        //  STATUS_WRONG_VOLUME from the verify, and our volume
        //  is now mounted, commute the status to STATUS_SUCCESS.
        //
 
        if ((Status == STATUS_WRONG_VOLUME) &&
            (Vcb->VcbCondition == VcbMounted)) {
 
            Status = STATUS_SUCCESS;
        }
        else if ((STATUS_SUCCESS == Status) && (Vcb->VcbCondition != VcbMounted))  {
 
            //
            //  If the verify succeeded,  but our volume is not mounted,
            //  then some other volume is on the device.
            //
 
            Status = STATUS_WRONG_VOLUME;
        }
         
        //
        //  Do a quick unprotected check here.  The routine will do
        //  a safe check.  After here we can release the resource.
        //  Note that if the volume really went away, we will be taking
        //  the Reparse path.
        //
 
        //
        //  If the device might need to go away then call our dismount routine.
        //
 
        if (((Vcb->VcbCondition == VcbNotMounted) ||
             (Vcb->VcbCondition == VcbInvalid) ||
             (Vcb->VcbCondition == VcbDismountInProgress)) &&
            (Vcb->VcbReference <= CDFS_RESIDUAL_REFERENCE)) {
 
            CdReleaseVcb( IrpContext, Vcb);
 
            CdAcquireCdData( IrpContext );
            CdCheckForDismount( IrpContext, Vcb, FALSE );
            CdReleaseCdData( IrpContext );
        }
        else {
 
            CdReleaseVcb( IrpContext, Vcb);
        }
 
        //
        //  If this is a create and the verify succeeded then complete the
        //  request with a REPARSE status.
        //
 
        if ((IrpContext->MajorFunction == IRP_MJ_CREATE) &&
            (IrpSp->FileObject->RelatedFileObject == NULL) &&
            ((Status == STATUS_SUCCESS) || (Status == STATUS_WRONG_VOLUME))) {
 
            Irp->IoStatus.Information = IO_REMOUNT;
 
            CdCompleteRequest( IrpContext, Irp, STATUS_REPARSE );
            Status = STATUS_REPARSE;
            Irp = NULL;
            IrpContext = NULL;
 
        //
        //  If there is still an error to process then call the Io system
        //  for a popup.
        //
 
        } else if ((Irp != NULL) && !NT_SUCCESS( Status )) {
 
            //
            //  Fill in the device object if required.
            //
             
            if (IoIsErrorUserInduced( Status ) ) {
 
                IoSetHardErrorOrVerifyDevice( Irp, DeviceToVerify );
            }
 
            CdNormalizeAndRaiseStatus( IrpContext, Status );
        }
 
        //
        //  If there is still an Irp, send it off to an Ex Worker thread.
        //
 
        if (IrpContext != NULL) {
 
            Status = CdFsdPostRequest( IrpContext, Irp );
        }
 
    } except(CdExceptionFilter( IrpContext, GetExceptionInformation() )) {
 
        //
        //  We had some trouble trying to perform the verify or raised
        //  an error ourselves.  So we'll abort the I/O request with
        //  the error status that we get back from the execption code.
        //
 
        Status = CdProcessException( IrpContext, Irp, GetExceptionCode() );
    }
 
    return Status;
}
 
 
_Requires_lock_held_(_Global_critical_region_)
BOOLEAN
CdCheckForDismount (
    _In_ PIRP_CONTEXT IrpContext,
    _Inout_ PVCB Vcb,
    _In_ BOOLEAN Force
    )
 
/*++
 
Routine Description:
 
    This routine is called to check if a volume is ready for dismount.  This
    occurs when only file system references are left on the volume.
 
    If the dismount is not currently underway and the user reference count
    has gone to zero then we can begin the dismount.
 
    If the dismount is in progress and there are no references left on the
    volume (we check the Vpb for outstanding references as well to catch
    any create calls dispatched to the file system) then we can delete
    the Vcb.
 
Arguments:
 
    Vcb - Vcb for the volume to try to dismount.
     
    Force - Whether we will force this volume to be dismounted.
 
Return Value:
 
    BOOLEAN - True if the Vcb was not gone by the time this function finished,
        False if it was deleted.
         
    This is only a trustworthy indication to the caller if it had the vcb
    exclusive itself.
 
--*/
 
{
    BOOLEAN UnlockVcb = TRUE;
    BOOLEAN VcbPresent = TRUE;
    KIRQL SavedIrql;
 
    ASSERT_IRP_CONTEXT( IrpContext );
    ASSERT_VCB( Vcb );
 
    ASSERT_EXCLUSIVE_CDDATA;
 
    //
    //  Acquire and lock this Vcb to check the dismount state.
    //
 
    CdAcquireVcbExclusive( IrpContext, Vcb, FALSE );
 
    //
    //  Lets get rid of any pending closes for this volume.
    //
 
    CdFspClose( Vcb );
 
    CdLockVcb( IrpContext, Vcb );
 
    //
    //  If the dismount is not already underway then check if the
    //  user reference count has gone to zero or we are being forced
    //  to disconnect.  If so start the teardown on the Vcb.
    //
 
    if (Vcb->VcbCondition != VcbDismountInProgress) {
 
        if (Vcb->VcbUserReference <= CDFS_RESIDUAL_USER_REFERENCE || Force) {
 
            CdUnlockVcb( IrpContext, Vcb );
            UnlockVcb = FALSE;
            VcbPresent = CdDismountVcb( IrpContext, Vcb );
        }
 
    //
    //  If the teardown is underway and there are absolutely no references
    //  remaining then delete the Vcb.  References here include the
    //  references in the Vcb and Vpb.
    //
 
    } else if (Vcb->VcbReference == 0) {
 
        IoAcquireVpbSpinLock( &SavedIrql );
 
        //
        //  If there are no file objects and no reference counts in the
        //  Vpb we can delete the Vcb.  Don't forget that we have the
        //  last reference in the Vpb.
        //
 
        if (Vcb->Vpb->ReferenceCount == 1) {
 
            IoReleaseVpbSpinLock( SavedIrql );
            CdUnlockVcb( IrpContext, Vcb );
            UnlockVcb = FALSE;
            CdDeleteVcb( IrpContext, Vcb );
            VcbPresent = FALSE;
 
        } else {
 
            IoReleaseVpbSpinLock( SavedIrql );
        }
    }
 
    //
    //  Unlock the Vcb if still held.
    //
 
    if (UnlockVcb) {
 
        CdUnlockVcb( IrpContext, Vcb );
    }
 
    //
    //  Release any resources still acquired.
    //
 
    if (VcbPresent) {
 
        CdReleaseVcb( IrpContext, Vcb );
    }
    else {
        _Analysis_assume_lock_not_held_(Vcb->VcbResource);
    }
 
    return VcbPresent;
}
 
 
BOOLEAN
CdMarkDevForVerifyIfVcbMounted (
    _Inout_ PVCB Vcb
    )
 
/*++
 
Routine Description:
 
    This routine checks to see if the specified Vcb is currently mounted on
    the device or not.  If it is,  it sets the verify flag on the device, if
    not then the state is noted in the Vcb.
 
Arguments:
 
    Vcb - This is the volume to check.
 
Return Value:
 
    TRUE if the device has been marked for verify here,  FALSE otherwise.
 
--*/
 
{
    BOOLEAN Marked = FALSE;
    KIRQL SavedIrql;
 
    IoAcquireVpbSpinLock( &SavedIrql );
 
#pragma prefast(suppress: 28175, "this is a filesystem driver, touching the vpb is allowed")
    if (Vcb->Vpb->RealDevice->Vpb == Vcb->Vpb)  {
 
        CdMarkRealDevForVerify( Vcb->Vpb->RealDevice);
        Marked = TRUE;
    }
    else {
 
        //
        //  Flag this to avoid the VPB spinlock in future passes.
        //
         
        SetFlag( Vcb->VcbState, VCB_STATE_VPB_NOT_ON_DEVICE);
    }
     
    IoReleaseVpbSpinLock( SavedIrql );
 
    return Marked;
}
 
 
VOID
CdVerifyVcb (
    _In_ PIRP_CONTEXT IrpContext,
    _Inout_ PVCB Vcb
    )
 
/*++
 
Routine Description:
 
    This routine checks that the current Vcb is valid and currently mounted
    on the device.  It will raise on an error condition.
 
    We check whether the volume needs verification and the current state
    of the Vcb.
 
Arguments:
 
    Vcb - This is the volume to verify.
 
Return Value:
 
    None
 
--*/
 
{
    NTSTATUS Status = STATUS_SUCCESS;
    IO_STATUS_BLOCK Iosb;
    ULONG MediaChangeCount = 0;
    BOOLEAN ForceVerify = FALSE;
    BOOLEAN DevMarkedForVerify;
 
    PAGED_CODE();
 
    //
    //  Fail immediately if the volume is in the progress of being dismounted
    //  or has been marked invalid.
    //
 
    if ((Vcb->VcbCondition == VcbInvalid) ||
        ((Vcb->VcbCondition == VcbDismountInProgress) &&
         (IrpContext->MajorFunction != IRP_MJ_CREATE))) {
 
        if (FlagOn( Vcb->VcbState, VCB_STATE_DISMOUNTED )) {
 
            CdRaiseStatus( IrpContext, STATUS_VOLUME_DISMOUNTED );
 
        } else {
 
            CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
        }
    }
     
    //
    //  Capture the real device verify state.
    //
     
    DevMarkedForVerify = CdRealDevNeedsVerify( Vcb->Vpb->RealDevice);
     
    if (FlagOn( Vcb->VcbState, VCB_STATE_REMOVABLE_MEDIA ) && !DevMarkedForVerify) {
         
        //
        //  If the media is removable and the verify volume flag in the
        //  device object is not set then we want to ping the device
        //  to see if it needs to be verified.
        //
 
        if (Vcb->VcbCondition != VcbMountInProgress) {
 
            Status = CdPerformDevIoCtrl( IrpContext,
                                         IOCTL_CDROM_CHECK_VERIFY,
                                         Vcb->TargetDeviceObject,
                                         &MediaChangeCount,
                                         sizeof(ULONG),
                                         FALSE,
                                         FALSE,
                                         &Iosb );
 
            if (Iosb.Information != sizeof(ULONG)) {
         
                //
                //  Be safe about the count in case the driver didn't fill it in
                //
         
                MediaChangeCount = 0;
            }
 
            //
            //  There are four cases when we want to do a verify.  These are the
            //  first three.
            //
            //  1. We are mounted,  and the device has become empty
            //  2. The device has returned verify required (=> DO_VERIFY_VOL flag is
            //     set, but could be due to hardware condition)
            //  3. Media change count doesn't match the one in the Vcb
            //
             
            if (((Vcb->VcbCondition == VcbMounted) &&
                 CdIsRawDevice( IrpContext, Status ))
                ||
                (Status == STATUS_VERIFY_REQUIRED)
                ||
                (NT_SUCCESS(Status) &&
                 (Vcb->MediaChangeCount != MediaChangeCount))) {
 
                //
                //  If we are currently the volume on the device then it is our
                //  responsibility to set the verify flag.  If we're not on the device,
                //  then we shouldn't touch the flag.
                //
 
                if (!FlagOn( Vcb->VcbState, VCB_STATE_VPB_NOT_ON_DEVICE) &&
                    !DevMarkedForVerify)  {
 
                     DevMarkedForVerify = CdMarkDevForVerifyIfVcbMounted( Vcb);
                }
 
                ForceVerify = TRUE;
 
                //
                //  NOTE that we no longer update the media change count here. We
                //  do so only when we've actually completed a verify at a particular
                //  change count value.
                //
            }
        }
 
        //
        //  This is the 4th verify case.
        //
        //  We ALWAYS force CREATE requests on unmounted volumes through the
        //  verify path.  These requests could have been in limbo between
        //  IoCheckMountedVpb and us when a verify/mount took place and caused
        //  a completely different fs/volume to be mounted.  In this case the
        //  checks above may not have caught the condition,  since we may already
        //  have verified (wrong volume) and decided that we have nothing to do.
        //  We want the requests to be re routed to the currently mounted volume,
        //  since they were directed at the 'drive',  not our volume.
        //
 
        if (NT_SUCCESS( Status) && !ForceVerify && !DevMarkedForVerify &&
            (IrpContext->MajorFunction == IRP_MJ_CREATE))  {
 
            PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( IrpContext->Irp);
 
            ForceVerify = (IrpSp->FileObject->RelatedFileObject == NULL) &&
                          ((Vcb->VcbCondition == VcbDismountInProgress) ||
                           (Vcb->VcbCondition == VcbNotMounted));
 
            //
            //  Note that we don't touch the device verify flag here.  It required
            //  it would have been caught and set by the first set of checks.
            //
        }
    }
 
    //
    //  Raise the verify / error if neccessary.
    //
     
    if (ForceVerify || DevMarkedForVerify || !NT_SUCCESS( Status)) {
     
        IoSetHardErrorOrVerifyDevice( IrpContext->Irp,
                                      Vcb->Vpb->RealDevice );
        
        CdRaiseStatus( IrpContext, (ForceVerify || DevMarkedForVerify)
                                   ? STATUS_VERIFY_REQUIRED
                                   : Status);
    }
 
    //
    //  Based on the condition of the Vcb we'll either return to our
    //  caller or raise an error condition
    //
 
    switch (Vcb->VcbCondition) {
 
    case VcbNotMounted:
 
        IoSetHardErrorOrVerifyDevice( IrpContext->Irp, Vcb->Vpb->RealDevice );
 
        CdRaiseStatus( IrpContext, STATUS_WRONG_VOLUME );
        break;
 
    case VcbInvalid:
    case VcbDismountInProgress:
 
        if (FlagOn( Vcb->VcbState, VCB_STATE_DISMOUNTED )) {
 
            CdRaiseStatus( IrpContext, STATUS_VOLUME_DISMOUNTED );
 
        } else {
 
            CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
        }
        break;
    }
}
 
 
BOOLEAN
CdVerifyFcbOperation (
    _In_opt_ PIRP_CONTEXT IrpContext,
    _In_ PFCB Fcb
    )
 
/*++
 
Routine Description:
 
    This routine is called to verify that the state of the Fcb is valid
    to allow the current operation to continue.  We use the state of the
    Vcb, target device and type of operation to determine this.
 
Arguments:
 
    IrpContext - IrpContext for the request.  If not present then we
        were called from the fast IO path.
 
    Fcb - Fcb to perform the request on.
 
Return Value:
 
    BOOLEAN - TRUE if the request can continue, FALSE otherwise.
 
--*/
 
{
    PVCB Vcb = Fcb->Vcb;
    PDEVICE_OBJECT RealDevice = Vcb->Vpb->RealDevice;
    PIRP Irp;
 
    PAGED_CODE();
     
    //
    //  Check that the fileobject has not been cleaned up.
    //
     
    if ( ARGUMENT_PRESENT( IrpContext ))  {
 
        PFILE_OBJECT FileObject;
 
        Irp = IrpContext->Irp;
        FileObject = IoGetCurrentIrpStackLocation( Irp)->FileObject;
         
        if ( FileObject && FlagOn( FileObject->Flags, FO_CLEANUP_COMPLETE))  {
 
            PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
 
            //
            //  Following FAT,  we allow certain operations even on cleaned up
            //  file objects.  Everything else,  we fail.
            //
             
            if ( (FlagOn(Irp->Flags, IRP_PAGING_IO)) ||
                 (IrpSp->MajorFunction == IRP_MJ_CLOSE ) ||
                 (IrpSp->MajorFunction == IRP_MJ_QUERY_INFORMATION) ||
                 ( (IrpSp->MajorFunction == IRP_MJ_READ) &&
                   FlagOn(IrpSp->MinorFunction, IRP_MN_COMPLETE) ) ) {
 
                NOTHING;
 
            } else {
 
                CdRaiseStatus( IrpContext, STATUS_FILE_CLOSED );
            }
        }
    }
 
    //
    //  Fail immediately if the volume is in the progress of being dismounted
    //  or has been marked invalid.
    //
 
    if ((Vcb->VcbCondition == VcbInvalid) ||
        (Vcb->VcbCondition == VcbDismountInProgress)) {
 
        if (ARGUMENT_PRESENT( IrpContext )) {
 
            if (FlagOn( Vcb->VcbState, VCB_STATE_DISMOUNTED )) {
 
                CdRaiseStatus( IrpContext, STATUS_VOLUME_DISMOUNTED );
 
            } else {
             
                CdRaiseStatus( IrpContext, STATUS_FILE_INVALID );
            }
        }
 
        return FALSE;
    }
 
    //
    //  Always fail if the volume needs to be verified.
    //
 
    if (CdRealDevNeedsVerify( RealDevice)) {
 
        if (ARGUMENT_PRESENT( IrpContext )) {
 
            IoSetHardErrorOrVerifyDevice( IrpContext->Irp,
                                          RealDevice );
 
            CdRaiseStatus( IrpContext, STATUS_VERIFY_REQUIRED );
        }
 
        return FALSE;
 
    //
    //
    //  All operations are allowed on mounted.
    //
 
    } else if ((Vcb->VcbCondition == VcbMounted) ||
               (Vcb->VcbCondition == VcbMountInProgress)) {
 
        return TRUE;
 
    //
    //  Fail all requests for fast Io on other Vcb conditions.
    //
 
    } else if (!ARGUMENT_PRESENT( IrpContext )) {
 
        return FALSE;
 
    //
    //  The remaining case is VcbNotMounted.
    //  Mark the device to be verified and raise WRONG_VOLUME.
    //
 
    } else if (Vcb->VcbCondition == VcbNotMounted) {
 
        IoSetHardErrorOrVerifyDevice( IrpContext->Irp, RealDevice );
        CdRaiseStatus( IrpContext, STATUS_WRONG_VOLUME );
 
//        return FALSE; // unreachable code
    }
 
    return TRUE;
}
 
 
_Requires_lock_held_(_Global_critical_region_)
BOOLEAN
CdDismountVcb (
    _In_ PIRP_CONTEXT IrpContext,
    _Inout_ PVCB Vcb
    )
 
/*++
 
Routine Description:
 
    This routine is called when all of the user references to a volume are
    gone.  We will initiate all of the teardown any system resources.
 
    If all of the references to this volume are gone at the end of this routine
    then we will complete the teardown of this Vcb and mark the current Vpb
    as not mounted.  Otherwise we will allocated a new Vpb for this device
    and keep the current Vpb attached to the Vcb.
 
Arguments:
 
    Vcb - Vcb for the volume to dismount.
 
Return Value:
 
    BOOLEAN - TRUE if we didn't delete the Vcb, FALSE otherwise.
 
--*/
 
{
    PVPB OldVpb;
    BOOLEAN VcbPresent = TRUE;
    KIRQL SavedIrql;
 
    BOOLEAN FinalReference;
 
    ASSERT_EXCLUSIVE_CDDATA;
    ASSERT_EXCLUSIVE_VCB( Vcb );
 
    CdLockVcb( IrpContext, Vcb );
 
    //
    //  We should only take this path once.
    //
 
    NT_ASSERT( Vcb->VcbCondition != VcbDismountInProgress );
 
    //
    //  Mark the Vcb as DismountInProgress.
    //
 
    Vcb->VcbCondition = VcbDismountInProgress;
 
    if (Vcb->XASector != NULL) {
 
        CdFreePool( &Vcb->XASector );
        Vcb->XASector = 0;
        Vcb->XADiskOffset = 0;
    }
 
    //
    //  Remove our reference to the internal Fcb's.  The Fcb's will then
    //  be removed in the purge path below.
    //
 
    if (Vcb->RootIndexFcb != NULL) {
 
        Vcb->RootIndexFcb->FcbReference -= 1;
        Vcb->RootIndexFcb->FcbUserReference -= 1;
    }
 
    if (Vcb->PathTableFcb != NULL) {
 
        Vcb->PathTableFcb->FcbReference -= 1;
        Vcb->PathTableFcb->FcbUserReference -= 1;
    }
 
    if (Vcb->VolumeDasdFcb != NULL) {
 
        Vcb->VolumeDasdFcb->FcbReference -= 1;
        Vcb->VolumeDasdFcb->FcbUserReference -= 1;
    }
 
    CdUnlockVcb( IrpContext, Vcb );
 
    //
    //  Purge the volume.
    //
 
    CdPurgeVolume( IrpContext, Vcb, TRUE );
 
    //
    //  Empty the delayed and async close queues.
    //
 
    CdFspClose( Vcb );
 
    OldVpb = Vcb->Vpb;
 
    //
    //  Remove the mount volume reference.
    //
 
    CdLockVcb( IrpContext, Vcb );
    Vcb->VcbReference -= 1;
 
    //
    //  Acquire the Vpb spinlock to check for Vpb references.
    //
 
    IoAcquireVpbSpinLock( &SavedIrql );
 
    //
    //  Remember if this is the last reference on this Vcb.  We incremented
    //  the count on the Vpb earlier so we get one last crack it.  If our
    //  reference has gone to zero but the vpb reference count is greater
    //  than zero then the Io system will be responsible for deleting the
    //  Vpb.
    //
 
    FinalReference = (BOOLEAN) ((Vcb->VcbReference == 0) &&
                                (OldVpb->ReferenceCount == 1));
 
    //
    //  There is a reference count in the Vpb and in the Vcb.  We have
    //  incremented the reference count in the Vpb to make sure that
    //  we have last crack at it.  If this is a failed mount then we
    //  want to return the Vpb to the IO system to use for the next
    //  mount request.
    //
 
#pragma prefast(suppress: 28175, "this is a filesystem driver, touching the vpb is allowed")
    if (OldVpb->RealDevice->Vpb == OldVpb) {
 
        //
        //  If not the final reference then swap out the Vpb.  We must
        //  preserve the REMOVE_PENDING flag so that the device is
        //  not remounted in the middle of a PnP remove operation.
        //
 
        if (!FinalReference) {
 
            NT_ASSERT( Vcb->SwapVpb != NULL );
 
            Vcb->SwapVpb->Type = IO_TYPE_VPB;
            Vcb->SwapVpb->Size = sizeof( VPB );
 
#pragma prefast(push)
#pragma prefast(disable: 28175, "this is a filesystem driver, touching the vpb is allowed")
            Vcb->SwapVpb->RealDevice = OldVpb->RealDevice;
            Vcb->SwapVpb->RealDevice->Vpb = Vcb->SwapVpb;
#pragma prefast(pop)
 
            Vcb->SwapVpb->Flags = FlagOn( OldVpb->Flags, VPB_REMOVE_PENDING );
 
            IoReleaseVpbSpinLock( SavedIrql );
 
            //
            //  Indicate we used up the swap.
            //
 
            Vcb->SwapVpb = NULL;           
 
            CdUnlockVcb( IrpContext, Vcb );
 
        //
        //  We want to leave the Vpb for the IO system.  Mark it
        //  as being not mounted.  Go ahead and delete the Vcb as
        //  well.
        //
 
        } else {
 
            //
            //  Make sure to remove the last reference on the Vpb.
            //
 
            OldVpb->ReferenceCount -= 1;
 
            OldVpb->DeviceObject = NULL;
            ClearFlag( Vcb->Vpb->Flags, VPB_MOUNTED );
            ClearFlag( Vcb->Vpb->Flags, VPB_LOCKED );
 
            //
            //  Clear the Vpb flag so we know not to delete it.
            //
 
            Vcb->Vpb = NULL;
 
            IoReleaseVpbSpinLock( SavedIrql );
            CdUnlockVcb( IrpContext, Vcb );
            CdDeleteVcb( IrpContext, Vcb );
            VcbPresent = FALSE;
        }
 
    //
    //  Someone has already swapped in a new Vpb.  If this is the final reference
    //  then the file system is responsible for deleting the Vpb.
    //
 
    } else if (FinalReference) {
 
        //
        //  Make sure to remove the last reference on the Vpb.
        //
 
        OldVpb->ReferenceCount -= 1;
 
        IoReleaseVpbSpinLock( SavedIrql );
        CdUnlockVcb( IrpContext, Vcb );
        CdDeleteVcb( IrpContext, Vcb );
        VcbPresent = FALSE;
 
    //
    //  The current Vpb is no longer the Vpb for the device (the IO system
    //  has already allocated a new one).  We leave our reference in the
    //  Vpb and will be responsible for deleting it at a later time.
    //
 
    } else {
 
        IoReleaseVpbSpinLock( SavedIrql );
        CdUnlockVcb( IrpContext, Vcb );
    }
 
    //
    //  Let our caller know whether the Vcb is still present.
    //
 
    return VcbPresent;
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS