Remove unnecessary executable bits
[reactos.git] / drivers / filesystems / cdfs_new / strucsup.c
old mode 100755 (executable)
new mode 100644 (file)
index 0ab42dc..7aa9c72
@@ -14,7 +14,7 @@ Abstract:
 
 --*/
 
-#include "CdProcs.h"
+#include "cdprocs.h"
 
 //
 //  The Bug check file id for this module
@@ -29,46 +29,46 @@ Abstract:
 //
 //  PFCB
 //  CdAllocateFcbData (
-//      IN PIRP_CONTEXT IrpContext
+//      _In_ PIRP_CONTEXT IrpContext
 //      );
 //
 //  VOID
 //  CdDeallocateFcbData (
-//      IN PIRP_CONTEXT IrpContext,
-//      IN PFCB Fcb
+//      _In_ PIRP_CONTEXT IrpContext,
+//      _Inout_ PFCB Fcb
 //      );
 //
 //  PFCB
 //  CdAllocateFcbIndex (
-//      IN PIRP_CONTEXT IrpContext
+//      _In_ PIRP_CONTEXT IrpContext
 //      );
 //
 //  VOID
 //  CdDeallocateFcbIndex (
-//      IN PIRP_CONTEXT IrpContext,
-//      IN PFCB Fcb
+//      _In_ PIRP_CONTEXT IrpContext,
+//      _Inout_ PFCB Fcb
 //      );
 //
 //  PFCB_NONPAGED
 //  CdAllocateFcbNonpaged (
-//      IN PIRP_CONTEXT IrpContext
+//      _In_ PIRP_CONTEXT IrpContext
 //      );
 //
 //  VOID
 //  CdDeallocateFcbNonpaged (
-//      IN PIRP_CONTEXT IrpContext,
-//      IN PFCB_NONPAGED FcbNonpaged
+//      _In_ PIRP_CONTEXT IrpContext,
+//      _Inout_ PFCB_NONPAGED FcbNonpaged
 //      );
 //
 //  PCCB
 //  CdAllocateCcb (
-//      IN PIRP_CONTEXT IrpContext
+//      _In_ PIRP_CONTEXT IrpContext
 //      );
 //
 //  VOID
 //  CdDeallocateCcb (
-//      IN PIRP_CONTEXT IrpContext,
-//      IN PCCB Ccb
+//      _In_ PIRP_CONTEXT IrpContext,
+//      _Inout_ PCCB Ccb
 //      );
 //
 
@@ -114,14 +114,14 @@ typedef struct _FCB_TABLE_ELEMENT {
 //
 //  VOID
 //  CdInsertFcbTable (
-//      IN PIRP_CONTEXT IrpContext,
-//      IN PFCB Fcb
+//      _In_ PIRP_CONTEXT IrpContext,
+//      _In_ PFCB Fcb
 //      );
 //
 //  VOID
 //  CdDeleteFcbTable (
-//      IN PIRP_CONTEXT IrpContext,
-//      IN PFCB Fcb
+//      _In_ PIRP_CONTEXT IrpContext,
+//      _In_ PFCB Fcb
 //      );
 //
 
@@ -148,44 +148,56 @@ typedef struct _FCB_TABLE_ELEMENT {
 
 VOID
 CdDeleteFcb (
-    IN PIRP_CONTEXT IrpContext,
-    IN PFCB Fcb
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PFCB Fcb
     );
 
 PFCB_NONPAGED
 CdCreateFcbNonpaged (
-    IN PIRP_CONTEXT IrpContext
+    _In_ PIRP_CONTEXT IrpContext
     );
 
 VOID
 CdDeleteFcbNonpaged (
-    IN PIRP_CONTEXT IrpContext,
-    IN PFCB_NONPAGED FcbNonpaged
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PFCB_NONPAGED FcbNonpaged
     );
 
+//  Inform prefast that this is a compare routine.
+RTL_GENERIC_COMPARE_ROUTINE CdFcbTableCompare;
+
 RTL_GENERIC_COMPARE_RESULTS
+NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
 CdFcbTableCompare (
-    IN PRTL_GENERIC_TABLE FcbTable,
-    IN PVOID Fid1,
-    IN PVOID Fid2
+    _In_ PRTL_GENERIC_TABLE FcbTable,
+    _In_ PVOID Fid1,
+    _In_ PVOID Fid2
     );
 
+//  Inform prefast that this is an alloc reoutine.
+RTL_GENERIC_ALLOCATE_ROUTINE CdAllocateFcbTable;
+
 PVOID
+NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
 CdAllocateFcbTable (
-    IN PRTL_GENERIC_TABLE FcbTable,
-    IN CLONG ByteSize
+    _In_ PRTL_GENERIC_TABLE FcbTable,
+    _In_ CLONG ByteSize
     );
 
+//  Inform prefast that this is a free reoutine.
+RTL_GENERIC_FREE_ROUTINE CdDeallocateFcbTable;
+
 VOID
+NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
 CdDeallocateFcbTable (
-    IN PRTL_GENERIC_TABLE FcbTable,
-    IN PVOID Buffer
+    _In_ PRTL_GENERIC_TABLE FcbTable,
+    _In_ __drv_freesMem(Mem) _Post_invalid_ PVOID Buffer
     );
 
 ULONG
 CdTocSerial (
-    IN PIRP_CONTEXT IrpContext,
-    IN PCDROM_TOC CdromToc
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PCDROM_TOC_LARGE CdromToc
     );
 
 #ifdef ALLOC_PRAGMA
@@ -215,19 +227,28 @@ CdTocSerial (
 #pragma alloc_text(PAGE, CdUpdateVcbFromVolDescriptor)
 #endif
 
+//
+//  Some static names for volume streams
+//
+
+UNICODE_STRING CdInternalStreamNames[] = {
+        { 24, 24, L"$PATH_TABLE$"},
+        { 2,  2,  L"\\"}
+};
+
 \f
 VOID
 CdInitializeVcb (
-    IN PIRP_CONTEXT IrpContext,
-    IN OUT PVCB Vcb,
-    IN PDEVICE_OBJECT TargetDeviceObject,
-    IN PVPB Vpb,
-    IN PCDROM_TOC CdromToc,
-    IN ULONG TocLength,
-    IN ULONG TocTrackCount,
-    IN ULONG TocDiskFlags,
-    IN ULONG BlockFactor,
-    IN ULONG MediaChangeCount
+    _In_ PIRP_CONTEXT IrpContext,
+    _Inout_ PVCB Vcb,
+    _In_ __drv_aliasesMem  PDEVICE_OBJECT TargetDeviceObject,
+    _In_ __drv_aliasesMem PVPB Vpb,
+    _In_ __drv_aliasesMem PCDROM_TOC_LARGE CdromToc,
+    _In_ ULONG TocLength,
+    _In_ ULONG TocTrackCount,
+    _In_ ULONG TocDiskFlags,
+    _In_ ULONG BlockFactor,
+    _In_ ULONG MediaChangeCount
     )
 
 /*++
@@ -271,6 +292,8 @@ Return Value:
 {
     PAGED_CODE();
 
+    UNREFERENCED_PARAMETER( IrpContext );
+
     //
     //  We start by first zeroing out all of the VCB, this will guarantee
     //  that any stale data is wiped clean.
@@ -298,19 +321,19 @@ Return Value:
     //  uninitialize the notify structures before returning.
     //
     
-    try  {
+    _SEH2_TRY  {
 
-        Vcb->SwapVpb = FsRtlAllocatePoolWithTag( NonPagedPool,
+        Vcb->SwapVpb = FsRtlAllocatePoolWithTag( CdNonPagedPool,
                                                  sizeof( VPB ),
                                                  TAG_VPB );
     }
-    finally {
+    _SEH2_FINALLY {
 
-        if (AbnormalTermination())  {
+        if (_SEH2_AbnormalTermination())  {
         
             FsRtlNotifyUninitializeSync( &Vcb->NotifySync );
         }
-    }
+    } _SEH2_END;
 
     //
     //  Nothing beyond this point should raise.
@@ -411,9 +434,9 @@ Return Value:
 \f
 VOID
 CdUpdateVcbFromVolDescriptor (
-    IN PIRP_CONTEXT IrpContext,
-    IN OUT PVCB Vcb,
-    IN PCHAR RawIsoVd OPTIONAL
+    _In_ PIRP_CONTEXT IrpContext,
+    _Inout_ PVCB Vcb,
+    _In_reads_bytes_opt_(SECTOR_SIZE) PCHAR RawIsoVd
     )
 
 /*++
@@ -438,7 +461,6 @@ Return Value:
 --*/
 
 {
-    //ULONG Shift; /* ReactOS Change: GCC Unused variable */
     ULONG StartingBlock;
     ULONG ByteCount;
 
@@ -456,7 +478,7 @@ Return Value:
     //  Use a try-finally to facilitate cleanup.
     //
 
-    try {
+    _SEH2_TRY {
 
         //
         //  Copy the block size and compute the various block masks.
@@ -553,7 +575,7 @@ Return Value:
 
             SetFlag( Vcb->PathTableFcb->FcbState, FCB_STATE_INITIALIZED );
 
-            CdCreateInternalStream( IrpContext, Vcb, Vcb->PathTableFcb );
+            CdCreateInternalStream( IrpContext, Vcb, Vcb->PathTableFcb, &CdInternalStreamNames[0]);
 
             //
             //  Create the root index and reference it in the Vcb.
@@ -599,7 +621,7 @@ Return Value:
             //  Create the stream file for the root directory.
             //
 
-            CdCreateInternalStream( IrpContext, Vcb, Vcb->RootIndexFcb );
+            CdCreateInternalStream( IrpContext, Vcb, Vcb->RootIndexFcb, &CdInternalStreamNames[1] );
 
             //
             //  Now do the volume dasd Fcb.  Create this and reference it in the
@@ -723,7 +745,7 @@ Return Value:
 
             SetFlag( Vcb->PathTableFcb->FcbState, FCB_STATE_INITIALIZED );
 
-            CdCreateInternalStream( IrpContext, Vcb, Vcb->PathTableFcb );
+            CdCreateInternalStream( IrpContext, Vcb, Vcb->PathTableFcb, &CdInternalStreamNames[0]);
 
             //
             //  Create the root index and reference it in the Vcb.
@@ -781,7 +803,7 @@ Return Value:
             //  Create the stream file for the root directory.
             //
 
-            CdCreateInternalStream( IrpContext, Vcb, Vcb->RootIndexFcb );
+            CdCreateInternalStream( IrpContext, Vcb, Vcb->RootIndexFcb, &CdInternalStreamNames[1] );
 
             //
             //  Now do the volume dasd Fcb.  Create this and reference it in the
@@ -843,17 +865,17 @@ Return Value:
             SetFlag( Vcb->VcbState, VCB_STATE_ISO );
         }
         
-    } finally {
+    } _SEH2_FINALLY {
 
         if (UnlockVcb) { CdUnlockVcb( IrpContext, Vcb ); }
-    }
+    } _SEH2_END;
 }
 
 \f
 VOID
 CdDeleteVcb (
-    IN PIRP_CONTEXT IrpContext,
-    IN OUT PVCB Vcb
+    _In_ PIRP_CONTEXT IrpContext,
+    _Inout_ PVCB Vcb
     )
 
 /*++
@@ -879,24 +901,20 @@ Return Value:
 
     ASSERT_EXCLUSIVE_CDDATA;
     ASSERT_EXCLUSIVE_VCB( Vcb );
-
+    
+    UNREFERENCED_PARAMETER( IrpContext );
+    
     //
     //  Chuck the backpocket Vpb we kept just in case.
     //
 
-    if (Vcb->SwapVpb) {
-
-        CdFreePool( &Vcb->SwapVpb );
-    }
+    CdFreePool( &Vcb->SwapVpb );
     
     //
     //  If there is a Vpb then we must delete it ourselves.
     //
 
-    if (Vcb->Vpb != NULL) {
-
-        CdFreePool( &Vcb->Vpb );
-    }
+    CdFreePool( &Vcb->Vpb );
 
     //
     //  Dereference our target if we haven't already done so.
@@ -908,12 +926,18 @@ Return Value:
     }
 
     //
-    //  Delete the XA Sector if allocated.
+    //  Delete the XA Sector and sector cache buffer if allocated.
     //
 
-    if (Vcb->XASector != NULL) {
+    CdFreePool( &Vcb->XASector );
+    CdFreePool( &Vcb->SectorCacheBuffer);
+
+    if (Vcb->SectorCacheIrp != NULL) {
+
+        IoFreeIrp( Vcb->SectorCacheIrp);
+        Vcb->SectorCacheIrp = NULL;
 
-        CdFreePool( &Vcb->XASector );
+        ExDeleteResourceLite( &Vcb->SectorCacheResource);
     }
 
     //
@@ -933,10 +957,7 @@ Return Value:
     //  Delete the TOC if present.
     //
 
-    if (Vcb->CdromToc != NULL) {
-
-        CdFreePool( &Vcb->CdromToc );
-    }
+    CdFreePool( &Vcb->CdromToc );
 
     //
     //  Uninitialize the notify structures.
@@ -950,7 +971,9 @@ Return Value:
     //
     //  Now delete the volume device object.
     //
-
+#ifdef _MSC_VER
+#pragma prefast( suppress: __WARNING_BUFFER_UNDERFLOW, "This is ok, the Vcb is embedded in our volume device object, and that is what we are really deleting." )
+#endif
     IoDeleteDevice( (PDEVICE_OBJECT) CONTAINING_RECORD( Vcb,
                                                         VOLUME_DEVICE_OBJECT,
                                                         Vcb ));
@@ -961,10 +984,10 @@ Return Value:
 \f
 PFCB
 CdCreateFcb (
-    IN PIRP_CONTEXT IrpContext,
-    IN FILE_ID FileId,
-    IN NODE_TYPE_CODE NodeTypeCode,
-    OUT PBOOLEAN FcbExisted OPTIONAL
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ FILE_ID FileId,
+    _In_ NODE_TYPE_CODE NodeTypeCode,
+    _Out_opt_ PBOOLEAN FcbExisted
     )
 
 /*++
@@ -1051,6 +1074,9 @@ Return Value:
 
         default:
 
+#ifdef _MSC_VER
+#pragma prefast( suppress: __WARNING_USE_OTHER_FUNCTION, "This is a bug." )   
+#endif
             CdBugCheck( 0, 0, 0 );
         }
 
@@ -1091,6 +1117,12 @@ Return Value:
         ExInitializeFastMutex( &NewFcb->FcbNonpaged->AdvancedFcbHeaderMutex );
         FsRtlSetupAdvancedHeader( &NewFcb->Header, 
                                   &NewFcb->FcbNonpaged->AdvancedFcbHeaderMutex );
+
+        if (NodeTypeCode == CDFS_NTC_FCB_DATA) {
+
+            FsRtlInitializeOplock( CdGetFcbOplock(NewFcb) );
+        }
+
     } else {
 
         *FcbExisted = TRUE;
@@ -1102,10 +1134,10 @@ Return Value:
 \f
 VOID
 CdInitializeFcbFromPathEntry (
-    IN PIRP_CONTEXT IrpContext,
-    IN PFCB Fcb,
-    IN PFCB ParentFcb OPTIONAL,
-    IN PPATH_ENTRY PathEntry
+    _In_ PIRP_CONTEXT IrpContext,
+    _Inout_ PFCB Fcb,
+    _In_opt_ PFCB ParentFcb,
+    _In_ PPATH_ENTRY PathEntry
     )
 
 /*++
@@ -1167,8 +1199,7 @@ Return Value:
     //  State flags for this Fcb.
     //
 
-    SetFlag( Fcb->FileAttributes,
-             FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY );
+    SetFlag( Fcb->FileAttributes, FILE_ATTRIBUTE_DIRECTORY );
 
     //
     //  Link into the other in-memory structures and into the Fcb table.
@@ -1192,10 +1223,10 @@ Return Value:
 \f
 VOID
 CdInitializeFcbFromFileContext (
-    IN PIRP_CONTEXT IrpContext,
-    IN PFCB Fcb,
-    IN PFCB ParentFcb,
-    IN PFILE_ENUM_CONTEXT FileContext
+    _In_ PIRP_CONTEXT IrpContext,
+    _Inout_ PFCB Fcb,
+    _In_ PFCB ParentFcb,
+    _In_ PFILE_ENUM_CONTEXT FileContext
     )
 
 /*++
@@ -1238,7 +1269,7 @@ Return Value:
 
     CdLockFcb( IrpContext, Fcb );
 
-    try {
+    _SEH2_TRY {
 
         //
         //  Initialize the common header in the Fcb.  The node type is already
@@ -1361,10 +1392,10 @@ Return Value:
         CdInsertFcbTable( IrpContext, Fcb );
         SetFlag( Fcb->FcbState, FCB_STATE_IN_FCB_TABLE );
 
-    } finally {
+    } _SEH2_FINALLY {
 
         CdUnlockFcb( IrpContext, Fcb );
-    }
+    } _SEH2_END;
 
     return;
 }
@@ -1372,9 +1403,9 @@ Return Value:
 \f
 PCCB
 CdCreateCcb (
-    IN PIRP_CONTEXT IrpContext,
-    IN PFCB Fcb,
-    IN ULONG Flags
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PFCB Fcb,
+    _In_ ULONG Flags
     )
 
 /*++
@@ -1399,6 +1430,8 @@ Return Value:
     PCCB NewCcb;
     PAGED_CODE();
 
+    UNREFERENCED_PARAMETER( IrpContext );
+    
     //
     //  Allocate and initialize the structure.
     //
@@ -1427,8 +1460,8 @@ Return Value:
 \f
 VOID
 CdDeleteCcb (
-    IN PIRP_CONTEXT IrpContext,
-    IN PCCB Ccb
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ __drv_freesMem( Pool ) PCCB Ccb
     )
 /*++
 
@@ -1449,6 +1482,8 @@ Return Value:
 {
     PAGED_CODE();
 
+    UNREFERENCED_PARAMETER( IrpContext );
+    
     if (Ccb->SearchExpression.FileName.Buffer != NULL) {
 
         CdFreePool( &Ccb->SearchExpression.FileName.Buffer );
@@ -1459,11 +1494,13 @@ Return Value:
 }
 
 \f
+_When_(RaiseOnError || return, _At_(Fcb->FileLock, _Post_notnull_))
+_When_(RaiseOnError, _At_(IrpContext, _Pre_notnull_))
 BOOLEAN
 CdCreateFileLock (
-    IN PIRP_CONTEXT IrpContext OPTIONAL,
-    IN PFCB Fcb,
-    IN BOOLEAN RaiseOnError
+    _In_opt_ PIRP_CONTEXT IrpContext,
+    _Inout_ PFCB Fcb,
+    _In_ BOOLEAN RaiseOnError
     )
 
 /*++
@@ -1520,7 +1557,7 @@ Return Value:
          
         if (RaiseOnError) {
 
-            ASSERT( ARGUMENT_PRESENT( IrpContext ));
+            NT_ASSERT( ARGUMENT_PRESENT( IrpContext ));
 
             CdRaiseStatus( IrpContext, STATUS_INSUFFICIENT_RESOURCES );
         }
@@ -1532,10 +1569,10 @@ Return Value:
 }
 
 \f
-PIRP_CONTEXT
+_Ret_valid_ PIRP_CONTEXT
 CdCreateIrpContext (
-    IN PIRP Irp,
-    IN BOOLEAN Wait
+    _In_ PIRP Irp,
+    _In_ BOOLEAN Wait
     )
 
 /*++
@@ -1570,7 +1607,12 @@ Return Value:
     //  occur in the context of fileobjects (i.e., mount).
     //
 
+#ifndef __REACTOS__
     if (IrpSp->DeviceObject == CdData.FileSystemDeviceObject) {
+#else
+    if (IrpSp->DeviceObject == CdData.FileSystemDeviceObject ||
+        IrpSp->DeviceObject == CdData.HddFileSystemDeviceObject) {
+#endif
 
         if (IrpSp->FileObject != NULL &&
             IrpSp->MajorFunction != IRP_MJ_CREATE &&
@@ -1580,7 +1622,7 @@ Return Value:
             ExRaiseStatus( STATUS_INVALID_DEVICE_REQUEST );
         }
 
-        ASSERT( IrpSp->FileObject != NULL ||
+        NT_ASSERT( IrpSp->FileObject != NULL ||
                 
                 (IrpSp->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL &&
                  IrpSp->MinorFunction == IRP_MN_USER_FS_REQUEST &&
@@ -1614,7 +1656,7 @@ Return Value:
         //  We didn't get it from our private list so allocate it from pool.
         //
 
-        NewIrpContext = FsRtlAllocatePoolWithTag( NonPagedPool, sizeof( IRP_CONTEXT ), TAG_IRP_CONTEXT );
+        NewIrpContext = FsRtlAllocatePoolWithTag( CdNonPagedPool, sizeof( IRP_CONTEXT ), TAG_IRP_CONTEXT );
     }
 
     RtlZeroMemory( NewIrpContext, sizeof( IRP_CONTEXT ));
@@ -1648,7 +1690,12 @@ Return Value:
     //  the Vcb field.
     //
 
+#ifndef __REACTOS__
     if (IrpSp->DeviceObject != CdData.FileSystemDeviceObject) {
+#else
+    if (IrpSp->DeviceObject != CdData.FileSystemDeviceObject &&
+        IrpSp->DeviceObject != CdData.HddFileSystemDeviceObject) {
+#endif
 
         NewIrpContext->Vcb =  &((PVOLUME_DEVICE_OBJECT) IrpSp->DeviceObject)->Vcb;
     
@@ -1684,8 +1731,8 @@ Return Value:
 \f
 VOID
 CdCleanupIrpContext (
-    IN PIRP_CONTEXT IrpContext,
-    IN BOOLEAN Post
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ BOOLEAN Post
     )
 
 /*++
@@ -1790,8 +1837,8 @@ Return Value:
 \f
 VOID
 CdInitializeStackIrpContext (
-    OUT PIRP_CONTEXT IrpContext,
-    IN PIRP_CONTEXT_LITE IrpContextLite
+    _Out_ PIRP_CONTEXT IrpContext,
+    _In_ PIRP_CONTEXT_LITE IrpContextLite
     )
 
 /*++
@@ -1864,11 +1911,13 @@ Return Value:
 }
 
 \f
+
+_Requires_lock_held_(_Global_critical_region_)
 VOID
 CdTeardownStructures (
-    IN PIRP_CONTEXT IrpContext,
-    IN PFCB StartingFcb,
-    OUT PBOOLEAN RemovedStartingFcb
+    _In_ PIRP_CONTEXT IrpContext,
+    _Inout_ PFCB StartingFcb,
+    _Out_ PBOOLEAN RemovedStartingFcb
     )
 
 /*++
@@ -1927,7 +1976,7 @@ Return Value:
     //  Use a try-finally to safely clear the top-level field.
     //
 
-    try {
+    _SEH2_TRY {
 
         //
         //  Loop until we find an Fcb we can't remove.
@@ -2031,7 +2080,7 @@ Return Value:
 
         } while (CurrentFcb != NULL);
 
-    } finally {
+    } _SEH2_FINALLY {
 
         //
         //  Release the current Fcb if we have acquired it.
@@ -2047,7 +2096,7 @@ Return Value:
         //
 
         ClearFlag( IrpContext->TopLevel->Flags, IRP_CONTEXT_FLAG_IN_TEARDOWN );
-    }
+    } _SEH2_END;
 
     *RemovedStartingFcb = (CurrentFcb != StartingFcb);
     return;
@@ -2056,9 +2105,9 @@ Return Value:
 \f
 PFCB
 CdLookupFcbTable (
-    IN PIRP_CONTEXT IrpContext,
-    IN PVCB Vcb,
-    IN FILE_ID FileId
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PVCB Vcb,
+    _In_ FILE_ID FileId
     )
 
 /*++
@@ -2104,9 +2153,9 @@ Return Value:
 \f
 PFCB
 CdGetNextFcb (
-    IN PIRP_CONTEXT IrpContext,
-    IN PVCB Vcb,
-    IN PVOID *RestartKey
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PVCB Vcb,
+    _In_ PVOID *RestartKey
     )
 
 /*++
@@ -2135,6 +2184,8 @@ Return Value:
 
     PAGED_CODE();
 
+    UNREFERENCED_PARAMETER( IrpContext );
+    
     Fcb = (PFCB) RtlEnumerateGenericTableWithoutSplaying( &Vcb->FcbTable, RestartKey );
 
     if (Fcb != NULL) {
@@ -2148,12 +2199,12 @@ Return Value:
 \f
 NTSTATUS
 CdProcessToc (
-    IN PIRP_CONTEXT IrpContext,
-    IN PDEVICE_OBJECT TargetDeviceObject,
-    IN PCDROM_TOC CdromToc,
-    IN OUT PULONG Length,
-    OUT PULONG TrackCount,
-    OUT PULONG DiskFlags
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PDEVICE_OBJECT TargetDeviceObject,
+    _In_ PCDROM_TOC_LARGE CdromToc,
+    _Inout_ PULONG Length,
+    _Out_ PULONG TrackCount,
+    _Inout_ PULONG DiskFlags
     )
 
 /*++
@@ -2186,10 +2237,13 @@ Return Value:
 {
     NTSTATUS Status;
     IO_STATUS_BLOCK Iosb;
+    CDROM_READ_TOC_EX Command;
 
     ULONG CurrentTrack;
     ULONG LocalTrackCount;
     ULONG LocalTocLength;
+    ULONG Address = 0;
+    BOOLEAN UseReadToc = FALSE;
 
     union {
 
@@ -2202,24 +2256,48 @@ Return Value:
 
     PAGED_CODE();
 
+    //
+    //  Zero the command block.  This conveniently corresponds to an
+    //  LBA mode READ_TOC request.
+    //
+    
+    RtlZeroMemory( &Command, sizeof( Command));
+
+RetryReadToc:
+
     //
     //  Go ahead and read the table of contents
     //
 
-    Status = CdPerformDevIoCtrl( IrpContext,
-                                 IOCTL_CDROM_READ_TOC,
-                                 TargetDeviceObject,
-                                 CdromToc,
-                                 sizeof( CDROM_TOC ),
-                                 FALSE,
-                                 TRUE,
-                                 &Iosb );
+    Status = CdPerformDevIoCtrlEx( IrpContext,
+                                   UseReadToc ? IOCTL_CDROM_READ_TOC : IOCTL_CDROM_READ_TOC_EX,
+                                   TargetDeviceObject,
+                                   &Command,
+                                   sizeof( Command ),
+                                   CdromToc,
+                                   sizeof( CDROM_TOC_LARGE ),
+                                   FALSE,
+                                   TRUE,
+                                   &Iosb );
 
     //
     //  Nothing to process if this request fails.
     //
 
-    if (Status != STATUS_SUCCESS) {
+    if (!NT_SUCCESS( Status )) {
+
+        //
+        //  If the underlying device does not support READ_TOC_EX, try the old method.
+        //
+
+        if (!UseReadToc &&
+            ((Status == STATUS_INVALID_DEVICE_REQUEST) ||
+             (Status == STATUS_NOT_IMPLEMENTED) || /* ReactOS Change: we return STATUS_NOT_IMPLEMENTED for IOCTL_CDROM_READ_TOC_EX */
+             (Status == STATUS_INVALID_PARAMETER))) {
+
+            UseReadToc = TRUE;
+            goto RetryReadToc;
+        }
 
         return Status;
     }
@@ -2280,21 +2358,13 @@ Return Value:
                 CdromToc->LastTrack -= 1;
 
                 //
-                //  Knock 2.5 minutes off the current track to
-                //  hide the final leadin.
+                //  Knock 2.5 minutes off the current track to hide the final leadin.
+                //  2.5 min = 150 sec = (x 75) 11250 frames (sectors).
                 //
-
-                Track->Address[1] -= 2;
-                Track->Address[2] += 30;
-
-                if (Track->Address[2] < 60) {
-
-                    Track->Address[1] -= 1;
-
-                } else {
-
-                    Track->Address[2] -= 60;
-                }
+                
+                SwapCopyUchar4( &Address, &Track->Address);
+                Address -= 11250;
+                SwapCopyUchar4( &Track->Address, &Address);
 
                 Track->TrackNumber = TOC_LAST_TRACK;
 
@@ -2352,8 +2422,8 @@ Return Value:
 
 VOID
 CdDeleteFcb (
-    IN PIRP_CONTEXT IrpContext,
-    IN PFCB Fcb
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PFCB Fcb
     )
 
 /*++
@@ -2382,8 +2452,8 @@ Return Value:
     //  Sanity check the counts.
     //
 
-    ASSERT( Fcb->FcbCleanup == 0 );
-    ASSERT( Fcb->FcbReference == 0 );
+    NT_ASSERT( Fcb->FcbCleanup == 0 );
+    NT_ASSERT( Fcb->FcbReference == 0 );
 
     //
     //  Release any Filter Context structures associated with this FCB
@@ -2427,8 +2497,8 @@ Return Value:
     case CDFS_NTC_FCB_PATH_TABLE:
     case CDFS_NTC_FCB_INDEX:
 
-        ASSERT( Fcb->FileObject == NULL );
-        ASSERT( IsListEmpty( &Fcb->FcbQueue ));
+        NT_ASSERT( Fcb->FileObject == NULL );
+        NT_ASSERT( IsListEmpty( &Fcb->FcbQueue ));
 
         if (Fcb == Fcb->Vcb->RootIndexFcb) {
 
@@ -2441,7 +2511,7 @@ Return Value:
             Vcb->PathTableFcb = NULL;
         }
 
-        CdDeallocateFcbIndex( IrpContext, Fcb );
+        CdDeallocateFcbIndex( IrpContext, *(PVOID*)&Fcb );/* ReactOS Change: GCC "passing argument 1 from incompatible pointer type" */
         break;
 
     case CDFS_NTC_FCB_DATA :
@@ -2451,7 +2521,7 @@ Return Value:
             FsRtlFreeFileLock( Fcb->FileLock );
         }
 
-        FsRtlUninitializeOplock( &Fcb->Oplock );
+        FsRtlUninitializeOplock( CdGetFcbOplock(Fcb) );
 
         if (Fcb == Fcb->Vcb->VolumeDasdFcb) {
 
@@ -2459,7 +2529,7 @@ Return Value:
             Vcb->VolumeDasdFcb = NULL;
         }
 
-        CdDeallocateFcbData( IrpContext, Fcb );
+        CdDeallocateFcbData( IrpContext, *(PVOID*)&Fcb );/* ReactOS Change: GCC "passing argument 1 from incompatible pointer type" */
     }
 
     //
@@ -2469,8 +2539,8 @@ Return Value:
 
     if (Vcb != NULL) {
 
-        InterlockedDecrement( &Vcb->VcbReference );
-        InterlockedDecrement( &Vcb->VcbUserReference );
+        InterlockedDecrement( (LONG*)&Vcb->VcbReference );
+        InterlockedDecrement( (LONG*)&Vcb->VcbUserReference );
     }
 
     return;
@@ -2483,7 +2553,7 @@ Return Value:
 
 PFCB_NONPAGED
 CdCreateFcbNonpaged (
-    IN PIRP_CONTEXT IrpContext
+    _In_ PIRP_CONTEXT IrpContext
     )
 
 /*++
@@ -2505,7 +2575,9 @@ Return Value:
     PFCB_NONPAGED FcbNonpaged;
 
     PAGED_CODE();
-
+    
+    UNREFERENCED_PARAMETER( IrpContext );
+    
     //
     //  Allocate the non-paged pool and initialize the various
     //  synchronization objects.
@@ -2534,8 +2606,8 @@ Return Value:
 
 VOID
 CdDeleteFcbNonpaged (
-    IN PIRP_CONTEXT IrpContext,
-    IN PFCB_NONPAGED FcbNonpaged
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PFCB_NONPAGED FcbNonpaged
     )
 
 /*++
@@ -2556,10 +2628,12 @@ Return Value:
 
 {
     PAGED_CODE();
-
+    
+    UNREFERENCED_PARAMETER( IrpContext );
+    
     ExDeleteResourceLite( &FcbNonpaged->FcbResource );
 
-    CdDeallocateFcbNonpaged( IrpContext, FcbNonpaged );
+    CdDeallocateFcbNonpaged( IrpContext, *(PVOID*)&FcbNonpaged );/* ReactOS Change: GCC "passing argument 1 from incompatible pointer type" */
 
     return;
 }
@@ -2570,10 +2644,11 @@ Return Value:
 //
 
 RTL_GENERIC_COMPARE_RESULTS
+NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
 CdFcbTableCompare (
-    IN PRTL_GENERIC_TABLE FcbTable,
-    IN PVOID Fid1,
-    IN PVOID Fid2
+    _In_ PRTL_GENERIC_TABLE FcbTable,
+    _In_ PVOID Fid1,
+    _In_ PVOID Fid2
     )
 
 /*++
@@ -2627,9 +2702,10 @@ Return Value:
 //
 
 PVOID
+NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
 CdAllocateFcbTable (
-    IN PRTL_GENERIC_TABLE FcbTable,
-    IN CLONG ByteSize
+    _In_ PRTL_GENERIC_TABLE FcbTable,
+    _In_ CLONG ByteSize
     )
 
 /*++
@@ -2652,6 +2728,8 @@ Return Value:
 
 {
     PAGED_CODE();
+    
+    UNREFERENCED_PARAMETER( FcbTable );
 
     return( FsRtlAllocatePoolWithTag( CdPagedPool, ByteSize, TAG_FCB_TABLE ));
 }
@@ -2660,13 +2738,12 @@ Return Value:
 //
 //  Local support routine
 //
-
 VOID
+NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
 CdDeallocateFcbTable (
-    IN PRTL_GENERIC_TABLE FcbTable,
-    IN PVOID Buffer
+    _In_ PRTL_GENERIC_TABLE FcbTable,
+    _In_ __drv_freesMem(Mem) _Post_invalid_ PVOID Buffer
     )
-
 /*++
 
 Routine Description:
@@ -2700,8 +2777,8 @@ Return Value:
 
 ULONG
 CdTocSerial (
-    IN PIRP_CONTEXT IrpContext,
-    IN PCDROM_TOC CdromToc
+    _In_ PIRP_CONTEXT IrpContext,
+    _In_ PCDROM_TOC_LARGE CdromToc
     )
 
 /*++
@@ -2731,9 +2808,13 @@ Return Value:
     ULONG SerialNumber = 0;
     PTRACK_DATA ThisTrack;
     PTRACK_DATA LastTrack;
+    ULONG Address;
+    ULONG MsfAddress = 0;   // satisfy PREFIX
 
     PAGED_CODE();
 
+    UNREFERENCED_PARAMETER( IrpContext );
+    
     //
     //  Check if there are two tracks or fewer.
     //
@@ -2743,21 +2824,23 @@ Return Value:
 
     if (CdromToc->LastTrack - CdromToc->FirstTrack <= 1) {
 
-        SerialNumber = (((LastTrack->Address[1] * 60) + LastTrack->Address[2]) * 75) + LastTrack->Address[3];
-
-        SerialNumber -= (((ThisTrack->Address[1] * 60) + ThisTrack->Address[2]) * 75) + ThisTrack->Address[3];
+        SwapCopyUchar4( &Address, LastTrack->Address);
+        CdLbnToMmSsFf( Address, (PUCHAR)&SerialNumber);
     }
+    else {
 
-    //
-    //  Now find the starting offset of each track and add to the serial number.
-    //
-
-    while (ThisTrack != LastTrack) {
+        //
+        //  Add the starting offset of each track and add to the serial number.
+        //
 
-        SerialNumber += (ThisTrack->Address[1] << 16);
-        SerialNumber += (ThisTrack->Address[2] << 8);
-        SerialNumber += ThisTrack->Address[3];
-        ThisTrack += 1;
+        while (ThisTrack != LastTrack) {
+            
+            SwapCopyUchar4( &Address, ThisTrack->Address);
+            CdLbnToMmSsFf( Address, (PUCHAR)&MsfAddress);
+            
+            SerialNumber += MsfAddress;
+            ThisTrack += 1;
+        }
     }
 
     return SerialNumber;