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
////////////////////////////////////////////////////////////////////
// Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
// All rights reserved
// This file was released under the GPLv2 on June 2015.
////////////////////////////////////////////////////////////////////
/*************************************************************************
*
* File: UDFinit.cpp
*
* Module: UDF File System Driver (Kernel mode execution only)
*
* Description:
*     This file contains the initialization code for the kernel mode
*     UDF FSD module. The DriverEntry() routine is called by the I/O
*     sub-system to initialize the FSD.
*
*************************************************************************/

#include            "udffs.h"

// define the file specific bug-check id
#define         UDF_BUG_CHECK_ID                UDF_FILE_INIT

// global variables are declared here
UDFData                 UDFGlobalData;

#define KD_PREFIX

struct UDF_MEDIA_CLASS_NAMES UDFMediaClassName[] = {
    {MediaUnknown, REG_DEFAULT_UNKNOWN},
    {MediaHdd    , REG_DEFAULT_HDD},
    {MediaCdr    , REG_DEFAULT_CDR},
    {MediaCdrw   , REG_DEFAULT_CDRW},
    {MediaCdrom  , REG_DEFAULT_CDROM},
    {MediaZip    , REG_DEFAULT_ZIP},
    {MediaFloppy , REG_DEFAULT_FLOPPY},
    {MediaDvdr   , REG_DEFAULT_DVDR},
    {MediaDvdrw  , REG_DEFAULT_DVDRW}
};
/*
ULONG  MajorVersion = 0;
ULONG  MinorVersion = 0;
ULONG  BuildNumber  = 0;
*/
ULONG  FsRegistered = FALSE;

WORK_QUEUE_ITEM    RemountWorkQueueItem;

//ptrFsRtlNotifyVolumeEvent FsRtlNotifyVolumeEvent = NULL;

HANDLE  FsNotification_ThreadId = (HANDLE)(-1);

NTSTATUS
UDFCreateFsDeviceObject(
    PCWSTR          FsDeviceName,
    PDRIVER_OBJECT  DriverObject,
    DEVICE_TYPE     DeviceType,
    PDEVICE_OBJECT  *DeviceObject);

NTSTATUS
UDFDismountDevice(
    PUNICODE_STRING unicodeCdRomDeviceName);

VOID
UDFRemountAll(
    IN PVOID Context);

/*************************************************************************
*
* Function: DriverEntry()
*
* Description:
*   This routine is the standard entry point for all kernel mode drivers.
*   The routine is invoked at IRQL PASSIVE_LEVEL in the context of a
*   system worker thread.
*   All FSD specific data structures etc. are initialized here.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: STATUS_SUCCESS/Error (will cause driver to be unloaded).
*
*************************************************************************/
NTSTATUS
NTAPI
DriverEntry(
    PDRIVER_OBJECT  DriverObject,       // created by the I/O sub-system
    PUNICODE_STRING RegistryPath        // path to the registry key
    )
{
    NTSTATUS        RC = STATUS_SUCCESS;
    UNICODE_STRING  DriverDeviceName;
    UNICODE_STRING  unicodeDeviceName;
//    BOOLEAN         RegisteredShutdown = FALSE;
    BOOLEAN         InternalMMInitialized = FALSE;
//    BOOLEAN         DLDetectInitialized = FALSE;
//    ULONG           CdRomNumber;
//    CCHAR           deviceNameBuffer[MAXIMUM_FILENAME_LENGTH];
//    ANSI_STRING     deviceName;
//    UNICODE_STRING  unicodeCdRomDeviceName;
    PUDFFS_DEV_EXTENSION FSDevExt;
    HKEY            hUdfRootKey;
    LARGE_INTEGER   delay;

//    UDFPrint(("UDF: Entered " VER_STR_PRODUCT_NAME " UDF DriverEntry \n"));
//    UDFPrint((KD_PREFIX "Build " VER_STR_PRODUCT "\n"));

    _SEH2_TRY {
        _SEH2_TRY {

/*
            CrNtInit(DriverObject, RegistryPath);

            //PsGetVersion(&MajorVersion, &MinorVersion, &BuildNumber, NULL);
            UDFPrint(("UDF: OS Version Major: %x, Minor: %x, Build number: %d\n",
                                MajorVersion, MinorVersion, BuildNumber));
*/
#ifdef __REACTOS__
            UDFPrint(("UDF Init: OS should be ReactOS\n"));
#endif

            // initialize the global data structure
            RtlZeroMemory(&UDFGlobalData, sizeof(UDFGlobalData));

            // initialize some required fields
            UDFGlobalData.NodeIdentifier.NodeType = UDF_NODE_TYPE_GLOBAL_DATA;
            UDFGlobalData.NodeIdentifier.NodeSize = sizeof(UDFGlobalData);

            // initialize the global data resource and remember the fact that
            //  the resource has been initialized
            RC = UDFInitializeResourceLite(&(UDFGlobalData.GlobalDataResource));
            ASSERT(NT_SUCCESS(RC));
            UDFSetFlag(UDFGlobalData.UDFFlags, UDF_DATA_FLAGS_RESOURCE_INITIALIZED);

            RC = UDFInitializeResourceLite(&(UDFGlobalData.DelayedCloseResource));
            ASSERT(NT_SUCCESS(RC));
//            UDFSetFlag(UDFGlobalData.UDFFlags, UDF_DATA_FLAGS_RESOURCE_INITIALIZED);

            // keep a ptr to the driver object sent to us by the I/O Mgr
            UDFGlobalData.DriverObject = DriverObject;

            //SeEnableAccessToExports();

            // initialize the mounted logical volume list head
            InitializeListHead(&(UDFGlobalData.VCBQueue));

            UDFPrint(("UDF: Init memory manager\n"));
            // Initialize internal memory management
            if(!MyAllocInit()) {
                try_return(RC = STATUS_INSUFFICIENT_RESOURCES);
            }
            InternalMMInitialized = TRUE;

#ifdef USE_DLD
            // Initialize Deadlock Detector 
            DLDInit(1280);
            DLDetectInitialized = TRUE;
#endif
            // before we proceed with any more initialization, read in
            //  user supplied configurable values ...

            // Save RegistryPath
            RtlCopyMemory(&(UDFGlobalData.SavedRegPath), RegistryPath, sizeof(UNICODE_STRING));
    
            UDFGlobalData.SavedRegPath.Buffer = (PWSTR)MyAllocatePool__(NonPagedPool, RegistryPath->Length + 2);
            if(!UDFGlobalData.SavedRegPath.Buffer) try_return (RC = STATUS_INSUFFICIENT_RESOURCES);
            RtlCopyMemory(UDFGlobalData.SavedRegPath.Buffer, RegistryPath->Buffer, RegistryPath->Length + 2);

            RegTGetKeyHandle(NULL, UDFGlobalData.SavedRegPath.Buffer, &hUdfRootKey);

            RtlInitUnicodeString(&UDFGlobalData.UnicodeStrRoot, L"\\");
            RtlInitUnicodeString(&UDFGlobalData.UnicodeStrSDir, L":");
            RtlInitUnicodeString(&UDFGlobalData.AclName, UDF_SN_NT_ACL);

            UDFPrint(("UDF: Init delayed close queues\n"));
#ifdef UDF_DELAYED_CLOSE
            InitializeListHead( &UDFGlobalData.DelayedCloseQueue );
            InitializeListHead( &UDFGlobalData.DirDelayedCloseQueue );

            ExInitializeWorkItem( &UDFGlobalData.CloseItem,
                                  UDFDelayedClose,
                                  NULL );

            UDFGlobalData.DelayedCloseCount = 0;
            UDFGlobalData.DirDelayedCloseCount = 0;
#endif //UDF_DELAYED_CLOSE

            // we should have the registry data (if any), allocate zone memory ...
            //  This is an example of when FSD implementations __try to pre-allocate
            //  some fixed amount of memory to avoid internal fragmentation and/or waiting
            //  later during run-time ...

            UDFGlobalData.DefaultZoneSizeInNumStructs=10;

            UDFPrint(("UDF: Init zones\n"));
            if (!NT_SUCCESS(RC = UDFInitializeZones()))
                try_return(RC);<--- Unused variable value 'RC'

            UDFPrint(("UDF: Init pointers\n"));
            // initialize the IRP major function table, and the fast I/O table
            UDFInitializeFunctionPointers(DriverObject);

            UDFGlobalData.CPU_Count = KeNumberProcessors;

            // create a device object representing the driver itself
            //  so that requests can be targeted to the driver ...
            //  e.g. for a disk-based FSD, "mount" requests will be sent to
            //        this device object by the I/O Manager.
            //        For a redirector/server, you may have applications
            //        send "special" IOCTL's using this device object ...

            RtlInitUnicodeString(&DriverDeviceName, UDF_FS_NAME);

            UDFPrint(("UDF: Create Driver dev obj\n"));
            if (!NT_SUCCESS(RC = IoCreateDevice(
                    DriverObject,       // our driver object
                    sizeof(UDFFS_DEV_EXTENSION),  // don't need an extension for this object
                    &DriverDeviceName,  // name - can be used to "open" the driver
                                        // see the book for alternate choices
                    FILE_DEVICE_CD_ROM_FILE_SYSTEM,
                    0,                  // no special characteristics
                                        // do not want this as an exclusive device, though you might
                    FALSE,
                    &(UDFGlobalData.UDFDeviceObject)))) {
                        // failed to create a device object, leave ...
                try_return(RC);<--- Unused variable value 'RC'
            }

            FSDevExt = (PUDFFS_DEV_EXTENSION)((UDFGlobalData.UDFDeviceObject)->DeviceExtension);
            // Zero it out (typically this has already been done by the I/O
            // Manager but it does not hurt to do it again)!
            RtlZeroMemory(FSDevExt, sizeof(UDFFS_DEV_EXTENSION));

            // Initialize the signature fields
            FSDevExt->NodeIdentifier.NodeType = UDF_NODE_TYPE_UDFFS_DRVOBJ;
            FSDevExt->NodeIdentifier.NodeSize = sizeof(UDFFS_DEV_EXTENSION);

            RtlInitUnicodeString(&unicodeDeviceName, UDF_DOS_FS_NAME);
            IoCreateSymbolicLink(&unicodeDeviceName, &DriverDeviceName);

            UDFPrint(("UDF: Create CD dev obj\n"));
            if (!NT_SUCCESS(RC = UDFCreateFsDeviceObject(UDF_FS_NAME_CD,
                                    DriverObject,
                                    FILE_DEVICE_CD_ROM_FILE_SYSTEM,
                                    &(UDFGlobalData.UDFDeviceObject_CD)))) {
                // failed to create a device object, leave ...
                try_return(RC);<--- Unused variable value 'RC'
            }
#ifdef UDF_HDD_SUPPORT
            UDFPrint(("UDF: Create HDD dev obj\n"));
            if (!NT_SUCCESS(RC = UDFCreateFsDeviceObject(UDF_FS_NAME_HDD,
                                    DriverObject,
                                    FILE_DEVICE_DISK_FILE_SYSTEM,
                                    &(UDFGlobalData.UDFDeviceObject_HDD)))) {
                // failed to create a device object, leave ...
                try_return(RC);<--- Unused variable value 'RC'
            }
#endif //UDF_HDD_SUPPORT

/*            RtlInitUnicodeString(&DriverDeviceName, UDF_FS_NAME_OTHER);

            if (!NT_SUCCESS(RC = IoCreateDevice(
                    DriverObject,       // our driver object
                    0,                  // don't need an extension for this object
                    &DriverDeviceName,  // name - can be used to "open" the driver
                                        // see the book for alternate choices
                    FILE_DEVICE_FILE_SYSTEM,
                    0,                  // no special characteristics
                                        // do not want this as an exclusive device, though you might
                    FALSE,
                    &(UDFGlobalData.UDFDeviceObject_OTHER)))) {
                        // failed to create a device object, leave ...
                try_return(RC);
            }
            // register the driver with the I/O Manager, pretend as if this is
            //  a physical disk based FSD (or in order words, this FSD manages
            //  logical volumes residing on physical disk drives)
            IoRegisterFileSystem(UDFGlobalData.UDFDeviceObject_OTHER);

            RtlInitUnicodeString(&DriverDeviceName, UDF_FS_NAME_TAPE);

            if (!NT_SUCCESS(RC = IoCreateDevice(
                    DriverObject,       // our driver object
                    0,                  // don't need an extension for this object
                    &DriverDeviceName,  // name - can be used to "open" the driver
                                        // see the book for alternate choices
                    FILE_DEVICE_TAPE_FILE_SYSTEM,
                    0,                  // no special characteristics
                                        // do not want this as an exclusive device, though you might
                    FALSE,
                    &(UDFGlobalData.UDFDeviceObject_TAPE)))) {
                        // failed to create a device object, leave ...
                try_return(RC);
            }
            // register the driver with the I/O Manager, pretend as if this is
            //  a physical disk based FSD (or in order words, this FSD manages
            //  logical volumes residing on physical disk drives)
            IoRegisterFileSystem(UDFGlobalData.UDFDeviceObject_TAPE);
*/

            if (UDFGlobalData.UDFDeviceObject_CD) {
                UDFPrint(("UDFCreateFsDeviceObject: IoRegisterFileSystem() for CD\n"));
                IoRegisterFileSystem(UDFGlobalData.UDFDeviceObject_CD);
            }
#ifdef UDF_HDD_SUPPORT
            if (UDFGlobalData.UDFDeviceObject_HDD) {
                UDFPrint(("UDFCreateFsDeviceObject: IoRegisterFileSystem() for HDD\n"));
                IoRegisterFileSystem(UDFGlobalData.UDFDeviceObject_HDD);
            }
#endif // UDF_HDD_SUPPORT
            FsRegistered = TRUE;

            UDFPrint(("UDF: IoRegisterFsRegistrationChange()\n"));
            IoRegisterFsRegistrationChange( DriverObject, UDFFsNotification );

//            delay.QuadPart = -10000000;
//            KeDelayExecutionThread(KernelMode, FALSE, &delay);        //10 microseconds

           delay.QuadPart = -10000000; // 1 sec
           KeDelayExecutionThread(KernelMode, FALSE, &delay);

#if 0
            if(!WinVer_IsNT) {
                /*ExInitializeWorkItem(&RemountWorkQueueItem, UDFRemountAll, NULL);
                UDFPrint(("UDFDriverEntry: create remount thread\n"));
                ExQueueWorkItem(&RemountWorkQueueItem, DelayedWorkQueue);*/

                for(CdRomNumber = 0;true;CdRomNumber++) {
                    sprintf(deviceNameBuffer, "\\Device\\CdRom%d", CdRomNumber);
                    UDFPrint(( "UDF: DriverEntry : dismount %s\n", deviceNameBuffer));
                    RtlInitString(&deviceName, deviceNameBuffer);
                    RC = RtlAnsiStringToUnicodeString(&unicodeCdRomDeviceName, &deviceName, TRUE);

                    if (!NT_SUCCESS(RC)) {
                        RtlFreeUnicodeString(&unicodeCdRomDeviceName);
                        break;
                    }

                    RC = UDFDismountDevice(&unicodeCdRomDeviceName);
                    RtlFreeUnicodeString(&unicodeCdRomDeviceName);
                    
                    if (!NT_SUCCESS(RC)) break;

                }

                PVOID ModuleBase = NULL;

                // get NTOSKRNL.EXE exports
                ModuleBase = CrNtGetModuleBase("NTOSKRNL.EXE");
                if(ModuleBase) {
                    FsRtlNotifyVolumeEvent = (ptrFsRtlNotifyVolumeEvent)CrNtGetProcAddress(ModuleBase, "FsRtlNotifyVolumeEvent");
                }

            }
#endif
            RC = STATUS_SUCCESS;

        } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
            // we encountered an exception somewhere, eat it up
            UDFPrint(("UDF: exception\n"));
            RC = _SEH2_GetExceptionCode();
        } _SEH2_END;

        InternalMMInitialized = FALSE;

        try_exit:   NOTHING;
    } _SEH2_FINALLY {
        // start unwinding if we were unsuccessful
        if (!NT_SUCCESS(RC)) {
            UDFPrint(("UDF: failed with status %x\n", RC));
            // Now, delete any device objects, etc. we may have created
/*            if (UDFGlobalData.UDFDeviceObject) {
                IoDeleteDevice(UDFGlobalData.UDFDeviceObject);
                UDFGlobalData.UDFDeviceObject = NULL;
            }*/
#ifdef USE_DLD
            if (DLDetectInitialized) DLDFree();
#endif
            if (InternalMMInitialized) {
                MyAllocRelease();
            }
            if (UDFGlobalData.UDFDeviceObject_CD) {
                IoDeleteDevice(UDFGlobalData.UDFDeviceObject_CD);
                UDFGlobalData.UDFDeviceObject_CD = NULL;
            }
#ifdef UDF_HDD_SUPPORT

            if (UDFGlobalData.UDFDeviceObject_HDD) {
                IoDeleteDevice(UDFGlobalData.UDFDeviceObject_HDD);
                UDFGlobalData.UDFDeviceObject_HDD = NULL;
            }
#endif // UDF_HDD_SUPPORT

/*
            if (UDFGlobalData.UDFDeviceObject_OTHER) {
                IoDeleteDevice(UDFGlobalData.UDFDeviceObject_CD);
                UDFGlobalData.UDFDeviceObject_CD = NULL;
            }

            if (UDFGlobalData.UDFDeviceObject_TAPE) {
                IoDeleteDevice(UDFGlobalData.UDFDeviceObject_CD);
                UDFGlobalData.UDFDeviceObject_CD = NULL;
            }
*/
            // free up any memory we might have reserved for zones/lookaside
            //  lists
            if (UDFGlobalData.UDFFlags & UDF_DATA_FLAGS_ZONES_INITIALIZED) {
                UDFDestroyZones();
            }

            // delete the resource we may have initialized
            if (UDFGlobalData.UDFFlags & UDF_DATA_FLAGS_RESOURCE_INITIALIZED) {
                // un-initialize this resource
                UDFDeleteResource(&(UDFGlobalData.GlobalDataResource));
                UDFClearFlag(UDFGlobalData.UDFFlags, UDF_DATA_FLAGS_RESOURCE_INITIALIZED);
            }
//        } else {
        }
    } _SEH2_END;

    return(RC);
} // end DriverEntry()



/*************************************************************************
*
* Function: UDFInitializeFunctionPointers()
*
* Description:
*   Initialize the IRP... function pointer array in the driver object
*   structure. Also initialize the fast-io function ptr array ...
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
VOID
NTAPI
UDFInitializeFunctionPointers(
    PDRIVER_OBJECT      DriverObject       // created by the I/O sub-system
    )
{
    PFAST_IO_DISPATCH    PtrFastIoDispatch = NULL;
    
    // initialize the function pointers for the IRP major
    //  functions that this FSD is prepared to  handle ...
    //  NT Version 4.0 has 28 possible functions that a
    //  kernel mode driver can handle.
    //  NT Version 3.51 and before has only 22 such functions,
    //  of which 18 are typically interesting to most FSD's.
    
    //  The only interesting new functions that a FSD might
    //  want to respond to beginning with Version 4.0 are the
    //  IRP_MJ_QUERY_QUOTA and the IRP_MJ_SET_QUOTA requests.
    
    //  The code below does not handle quota manipulation, neither
    //  does the NT Version 4.0 operating system (or I/O Manager).
    //  However, you should be on the lookout for any such new
    //  functionality that the FSD might have to implement in
    //  the near future.
    
    DriverObject->MajorFunction[IRP_MJ_CREATE]              = UDFCreate;
    DriverObject->MajorFunction[IRP_MJ_CLOSE]               = UDFClose;
    DriverObject->MajorFunction[IRP_MJ_READ]                = UDFRead;
#ifndef UDF_READ_ONLY_BUILD
    DriverObject->MajorFunction[IRP_MJ_WRITE]               = UDFWrite;
#endif //UDF_READ_ONLY_BUILD

    DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION]   = UDFFileInfo;
#ifndef UDF_READ_ONLY_BUILD
    DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION]     = UDFFileInfo;
#endif //UDF_READ_ONLY_BUILD

#ifndef UDF_READ_ONLY_BUILD
    DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS]       = UDFFlush;
#endif //UDF_READ_ONLY_BUILD
    // To implement support for querying and modifying volume attributes
    // (volume information query/set operations), enable initialization
    // of the following two function pointers and then implement the supporting
    // functions.
    DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] = UDFQueryVolInfo;
#ifndef UDF_READ_ONLY_BUILD
    DriverObject->MajorFunction[IRP_MJ_SET_VOLUME_INFORMATION] = UDFSetVolInfo;
#endif //UDF_READ_ONLY_BUILD
    DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL]   = UDFDirControl;
    // To implement support for file system IOCTL calls, enable initialization
    // of the following function pointer and implement appropriate support.
    DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] = UDFFSControl;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]      = UDFDeviceControl;
    DriverObject->MajorFunction[IRP_MJ_SHUTDOWN]            = UDFShutdown;
    // For byte-range lock support, enable initialization of the following
    // function pointer and implement appropriate support.
    DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL]        = UDFLockControl;
    DriverObject->MajorFunction[IRP_MJ_CLEANUP]             = UDFCleanup;
#ifdef UDF_HANDLE_EAS
    DriverObject->MajorFunction[IRP_MJ_QUERY_EA]            = UDFQuerySetEA;
#ifndef UDF_READ_ONLY_BUILD
    DriverObject->MajorFunction[IRP_MJ_SET_EA]              = UDFQuerySetEA;
#endif //UDF_READ_ONLY_BUILD
#endif //UDF_HANDLE_EAS
    // If the FSD supports security attributes, we should provide appropriate
    // dispatch entry points and initialize the function pointers as given below.

#ifdef UDF_ENABLE_SECURITY
    DriverObject->MajorFunction[IRP_MJ_QUERY_SECURITY]   = UDFGetSecurity;
#ifndef UDF_READ_ONLY_BUILD
    DriverObject->MajorFunction[IRP_MJ_SET_SECURITY]     = UDFSetSecurity;
#endif //UDF_READ_ONLY_BUILD
#endif //UDF_ENABLE_SECURITY

//    if(MajorVersion >= 0x05) {
        // w2k and higher
//        DriverObject->MajorFunction[IRP_MJ_PNP]           = UDFPnp;
//    }

    // Now, it is time to initialize the fast-io stuff ...
    PtrFastIoDispatch = DriverObject->FastIoDispatch = &(UDFGlobalData.UDFFastIoDispatch);

    // initialize the global fast-io structure
    //  NOTE: The fast-io structure has undergone a substantial revision
    //  in Windows NT Version 4.0. The structure has been extensively expanded.
    //  Therefore, if the driver needs to work on both V3.51 and V4.0+,
    //  we will have to be able to distinguish between the two versions at compile time.

    RtlZeroMemory(PtrFastIoDispatch, sizeof(FAST_IO_DISPATCH));

    PtrFastIoDispatch->SizeOfFastIoDispatch = sizeof(FAST_IO_DISPATCH);
    PtrFastIoDispatch->FastIoCheckIfPossible    = UDFFastIoCheckIfPossible;
    PtrFastIoDispatch->FastIoRead               = FsRtlCopyRead;
#ifndef UDF_READ_ONLY_BUILD
    PtrFastIoDispatch->FastIoWrite              = UDFFastIoCopyWrite /*FsRtlCopyWrite*/;
#endif //UDF_READ_ONLY_BUILD
    PtrFastIoDispatch->FastIoQueryBasicInfo     = UDFFastIoQueryBasicInfo;
    PtrFastIoDispatch->FastIoQueryStandardInfo  = UDFFastIoQueryStdInfo;
    PtrFastIoDispatch->FastIoLock               = UDFFastLock;         // Lock
    PtrFastIoDispatch->FastIoUnlockSingle       = UDFFastUnlockSingle; // UnlockSingle
    PtrFastIoDispatch->FastIoUnlockAll          = UDFFastUnlockAll;    // UnlockAll
    PtrFastIoDispatch->FastIoUnlockAllByKey     =  (unsigned char (__stdcall *)(struct _FILE_OBJECT *,
        PVOID ,unsigned long,struct _IO_STATUS_BLOCK *,struct _DEVICE_OBJECT *))UDFFastUnlockAllByKey;     //  UnlockAllByKey

    PtrFastIoDispatch->AcquireFileForNtCreateSection = UDFFastIoAcqCreateSec;
    PtrFastIoDispatch->ReleaseFileForNtCreateSection = UDFFastIoRelCreateSec;

//    PtrFastIoDispatch->FastIoDeviceControl = UDFFastIoDeviceControl;

    // the remaining are only valid under NT Version 4.0 and later
#if(_WIN32_WINNT >= 0x0400)

    PtrFastIoDispatch->FastIoQueryNetworkOpenInfo = UDFFastIoQueryNetInfo;

    PtrFastIoDispatch->AcquireForModWrite       = UDFFastIoAcqModWrite;
    PtrFastIoDispatch->ReleaseForModWrite       = UDFFastIoRelModWrite;
    PtrFastIoDispatch->AcquireForCcFlush        = UDFFastIoAcqCcFlush;
    PtrFastIoDispatch->ReleaseForCcFlush        = UDFFastIoRelCcFlush;

/*    // MDL functionality

    PtrFastIoDispatch->MdlRead                  = UDFFastIoMdlRead;
    PtrFastIoDispatch->MdlReadComplete          = UDFFastIoMdlReadComplete;
    PtrFastIoDispatch->PrepareMdlWrite          = UDFFastIoPrepareMdlWrite;
    PtrFastIoDispatch->MdlWriteComplete         = UDFFastIoMdlWriteComplete;*/

    //  this FSD does not support compressed read/write functionality,
    //  NTFS does, and if we design a FSD that can provide such functionality,
    //  we should consider initializing the fast io entry points for reading
    //  and/or writing compressed data ...
#endif  // (_WIN32_WINNT >= 0x0400)

    // last but not least, initialize the Cache Manager callback functions
    //  which are used in CcInitializeCacheMap()

    UDFGlobalData.CacheMgrCallBacks.AcquireForLazyWrite  = UDFAcqLazyWrite;
    UDFGlobalData.CacheMgrCallBacks.ReleaseFromLazyWrite = UDFRelLazyWrite;
    UDFGlobalData.CacheMgrCallBacks.AcquireForReadAhead  = UDFAcqReadAhead;
    UDFGlobalData.CacheMgrCallBacks.ReleaseFromReadAhead = UDFRelReadAhead;

    DriverObject->DriverUnload = UDFDriverUnload;

    return;
} // end UDFInitializeFunctionPointers()

NTSTATUS
UDFCreateFsDeviceObject(
    PCWSTR          FsDeviceName,
    PDRIVER_OBJECT  DriverObject,
    DEVICE_TYPE     DeviceType,
    PDEVICE_OBJECT  *DeviceObject
    )
{
    NTSTATUS RC = STATUS_SUCCESS;
    UNICODE_STRING  DriverDeviceName;
    PUDFFS_DEV_EXTENSION FSDevExt;
    RtlInitUnicodeString(&DriverDeviceName, FsDeviceName);
    *DeviceObject = NULL;

    UDFPrint(("UDFCreateFsDeviceObject: create dev\n"));

    if (!NT_SUCCESS(RC = IoCreateDevice(
            DriverObject,                   // our driver object
            sizeof(UDFFS_DEV_EXTENSION),    // don't need an extension for this object
            &DriverDeviceName,              // name - can be used to "open" the driver
                                // see the book for alternate choices
            DeviceType,
            0,                  // no special characteristics
                                // do not want this as an exclusive device, though you might
            FALSE,
            DeviceObject))) {
                // failed to create a device object, leave ...
        return(RC);
    }
    FSDevExt = (PUDFFS_DEV_EXTENSION)((*DeviceObject)->DeviceExtension);
    // Zero it out (typically this has already been done by the I/O
    // Manager but it does not hurt to do it again)!
    RtlZeroMemory(FSDevExt, sizeof(UDFFS_DEV_EXTENSION));

    // Initialize the signature fields
    FSDevExt->NodeIdentifier.NodeType = UDF_NODE_TYPE_UDFFS_DEVOBJ;
    FSDevExt->NodeIdentifier.NodeSize = sizeof(UDFFS_DEV_EXTENSION);
    // register the driver with the I/O Manager, pretend as if this is
    //  a physical disk based FSD (or in order words, this FSD manages
    //  logical volumes residing on physical disk drives)
/*    UDFPrint(("UDFCreateFsDeviceObject: IoRegisterFileSystem()\n"));
    IoRegisterFileSystem(*DeviceObject);*/
    return(RC);
} // end UDFCreateFsDeviceObject()


NTSTATUS
UDFDismountDevice(
    PUNICODE_STRING unicodeCdRomDeviceName
    )
{
    NTSTATUS RC;
    IO_STATUS_BLOCK IoStatus;
    HANDLE NtFileHandle = (HANDLE)-1;
    OBJECT_ATTRIBUTES ObjectAttributes;
    NOTIFY_MEDIA_CHANGE_USER_IN buffer = { 0 };
    PFILE_FS_ATTRIBUTE_INFORMATION Buffer;

    _SEH2_TRY {

        Buffer = (PFILE_FS_ATTRIBUTE_INFORMATION)MyAllocatePool__(NonPagedPool,sizeof(FILE_FS_ATTRIBUTE_INFORMATION)+2*sizeof(UDF_FS_TITLE_DVDRAM));
        if (!Buffer) try_return(RC = STATUS_INSUFFICIENT_RESOURCES);
        
        InitializeObjectAttributes ( &ObjectAttributes,
                                     unicodeCdRomDeviceName,
                                     OBJ_CASE_INSENSITIVE,
                                     NULL,
                                     NULL );

        UDFPrint(("\n*** UDFDismountDevice: Create\n"));
        RC = ZwCreateFile( &NtFileHandle,
                           GENERIC_READ,
                           &ObjectAttributes,
                           &IoStatus,
                           NULL,                          
                           FILE_ATTRIBUTE_NORMAL,
                           FILE_SHARE_READ,
                           FILE_OPEN,
                           FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
                           NULL,  
                           0 );


        if (!NT_SUCCESS(RC)) try_return(RC);<--- Unused variable value 'RC'
                  
        UDFPrint(("\n*** UDFDismountDevice: QueryVolInfo\n"));
        RC = ZwQueryVolumeInformationFile( NtFileHandle,
                                           &IoStatus,
                                           Buffer,
                                           sizeof(FILE_FS_ATTRIBUTE_INFORMATION)+2*sizeof(UDF_FS_TITLE_DVDRAM),
                                           FileFsAttributeInformation);

#define UDF_CHECK_FS_NAME(name) \
    (Buffer->FileSystemNameLength+sizeof(WCHAR) == sizeof(name) && \
            DbgCompareMemory(&Buffer->FileSystemName[0],name  , sizeof(name)) == sizeof(name))
                    
        if (NT_SUCCESS(RC) &&
           (UDF_CHECK_FS_NAME((PVOID)UDF_FS_TITLE_CDR)    ||
            UDF_CHECK_FS_NAME((PVOID)UDF_FS_TITLE_CDRW)   ||
            UDF_CHECK_FS_NAME((PVOID)UDF_FS_TITLE_DVDR)   ||
            UDF_CHECK_FS_NAME((PVOID)UDF_FS_TITLE_DVDRW)  ||
            UDF_CHECK_FS_NAME((PVOID)UDF_FS_TITLE_DVDpR)  ||
            UDF_CHECK_FS_NAME((PVOID)UDF_FS_TITLE_DVDpRW) ||
            UDF_CHECK_FS_NAME((PVOID)UDF_FS_TITLE_DVDRAM) )) try_return(STATUS_SUCCESS);
        
        UDFPrint(("\n*** UDFDismountDevice: LockVolume\n"));
        RC = ZwFsControlFile(NtFileHandle,
                             NULL,
                             NULL,
                             NULL,
                             &IoStatus,
                             FSCTL_LOCK_VOLUME,
                             NULL,
                             NULL,
                             NULL,
                             NULL);

        if (!NT_SUCCESS(RC)) try_return(RC);<--- Unused variable value 'RC'

        UDFPrint(("\n*** UDFDismountDevice: DismountVolume\n"));
        RC = ZwFsControlFile(NtFileHandle,
                             NULL,
                             NULL,
                             NULL,
                             &IoStatus,
                             FSCTL_DISMOUNT_VOLUME,
                             NULL,
                             NULL,
                             NULL,
                             NULL);

        if (!NT_SUCCESS(RC)) try_return(RC);<--- Unused variable value 'RC'

        UDFPrint(("\n*** UDFDismountDevice: NotifyMediaChange\n"));
        RC = ZwDeviceIoControlFile(NtFileHandle,
                                   NULL,
                                   NULL,
                                   NULL,
                                   &IoStatus,
                                   IOCTL_CDRW_NOTIFY_MEDIA_CHANGE,
                                   &buffer,
                                   sizeof(buffer),
                                   &buffer,
                                   sizeof(buffer));

        if (!NT_SUCCESS(RC)) try_return(RC);<--- Unused variable value 'RC'
        
        
        UDFPrint(("\n*** UDFDismountDevice: UnlockVolume\n"));
        RC = ZwFsControlFile(NtFileHandle,<--- RC is assigned
                             NULL,
                             NULL,
                             NULL,
                             &IoStatus,
                             FSCTL_UNLOCK_VOLUME,
                             NULL,
                             NULL,
                             NULL,
                             NULL);

        UDFPrint(("\n*** UDFDismountDevice: Close\n"));
        ZwClose( NtFileHandle );
        
        NtFileHandle = (HANDLE)-1;

        UDFPrint(("\n*** UDFDismountDevice: Create 2\n"));
        RC = ZwCreateFile( &NtFileHandle,<--- RC is overwritten
                           GENERIC_READ,
                           &ObjectAttributes,
                           &IoStatus,
                           NULL,
                           FILE_ATTRIBUTE_NORMAL,
                           FILE_SHARE_READ,
                           FILE_OPEN,
                           FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
                           NULL,  
                           0 );

try_exit: NOTHING;

    } _SEH2_FINALLY {
        if (Buffer) MyFreePool__(Buffer);
        if (NtFileHandle != (HANDLE)-1) ZwClose( NtFileHandle );
    } _SEH2_END;

    UDFPrint(("\n*** UDFDismountDevice: RC=%x\n",RC));
    return RC;
}


VOID
NTAPI
UDFFsNotification(
    IN PDEVICE_OBJECT DeviceObject,
    IN BOOLEAN FsActive
    )

/*

Routine Description:

    This routine is invoked whenever a file system has either registered or
    unregistered itself as an active file system.

    For the former case, this routine creates a device object and attaches it
    to the specified file system's device object.  This allows this driver
    to filter all requests to that file system.

    For the latter case, this file system's device object is located,
    detached, and deleted.  This removes this file system as a filter for
    the specified file system.

Arguments:

    DeviceObject - Pointer to the file system's device object.

    FsActive - bolean indicating whether the file system has registered
        (TRUE) or unregistered (FALSE) itself as an active file system.

Return Value:

    None.

*/

{
    // Begin by determine whether or not the file system is a cdrom-based file
    // system.  If not, then this driver is not concerned with it.
    if (!FsRegistered ||
        DeviceObject->DeviceType != FILE_DEVICE_CD_ROM_FILE_SYSTEM) {
        return;
    }

    // Begin by determining whether this file system is registering or
    // unregistering as an active file system.
    if (FsActive
        && UDFGlobalData.UDFDeviceObject_CD  != DeviceObject
#ifdef UDF_HDD_SUPPORT
        && UDFGlobalData.UDFDeviceObject_HDD != DeviceObject
#endif // UDF_HDD_SUPPORT
    ) {
        UDFPrint(("\n*** UDFFSNotification \n\n"));

        // Acquire GlobalDataResource
        UDFAcquireResourceExclusive(&(UDFGlobalData.GlobalDataResource), TRUE);

        if(FsNotification_ThreadId != PsGetCurrentThreadId()) {

            FsNotification_ThreadId = PsGetCurrentThreadId();

            IoUnregisterFileSystem(UDFGlobalData.UDFDeviceObject_CD);
            IoRegisterFileSystem(UDFGlobalData.UDFDeviceObject_CD);

#ifdef UDF_HDD_SUPPORT
            IoUnregisterFileSystem(UDFGlobalData.UDFDeviceObject_HDD);
            IoRegisterFileSystem(UDFGlobalData.UDFDeviceObject_HDD);
#endif // UDF_HDD_SUPPORT

            FsNotification_ThreadId = (HANDLE)(-1);

        } else {
            UDFPrint(("\n*** recursive UDFFSNotification call,\n can't become top-level UDF FSD \n\n"));
        }

        // Release the global resource.
        UDFReleaseResource( &(UDFGlobalData.GlobalDataResource) );

        
    }
}
/*VOID
UDFRemountAll(
    IN PVOID Context
    )
{
    NTSTATUS RC = STATUS_SUCCESS;
    ULONG           CdRomNumber;
    CCHAR           deviceNameBuffer[MAXIMUM_FILENAME_LENGTH];
    ANSI_STRING     deviceName;
    UNICODE_STRING  unicodeCdRomDeviceName;
    LARGE_INTEGER   delay;

*/
/*    delay.QuadPart = -80*10000000;
    KeDelayExecutionThread(KernelMode, FALSE, &delay);        //10 seconds*/
    
/*    for(CdRomNumber = 0;true;CdRomNumber++) {
        sprintf(deviceNameBuffer, "\\Device\\CdRom%d", CdRomNumber);
        UDFPrint(( "UDF: UDFRemountAll : dismount %s\n", deviceNameBuffer));
        RtlInitString(&deviceName, deviceNameBuffer);
        RC = RtlAnsiStringToUnicodeString(&unicodeCdRomDeviceName, &deviceName, TRUE);

        if (!NT_SUCCESS(RC)) {
            RtlFreeUnicodeString(&unicodeCdRomDeviceName);
            break;
        }

        RC = UDFDismountDevice(&unicodeCdRomDeviceName);
        RtlFreeUnicodeString(&unicodeCdRomDeviceName);
        
        if (!NT_SUCCESS(RC)) break;
    }
}*/