From da4fa4a9250ea8c0ff2adaa9faae677fd00397ee Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Sun, 20 Dec 2015 11:20:22 -0800 Subject: [PATCH] Major refactoring: WIP --- src/sys/close.c | 3 + src/sys/create.c | 8 ++ src/sys/device.c | 247 +++++++++++++++++++++++------------------------ src/sys/driver.h | 16 ++- src/sys/fsctl.c | 13 +++ 5 files changed, 154 insertions(+), 133 deletions(-) diff --git a/src/sys/close.c b/src/sys/close.c index 384534b2..ed67c3fe 100644 --- a/src/sys/close.c +++ b/src/sys/close.c @@ -28,6 +28,9 @@ static NTSTATUS FspFsctlClose( { PAGED_CODE(); + FspFree(IrpSp->FileObject->FsContext2); + IrpSp->FileObject->FsContext2 = 0; + Irp->IoStatus.Information = 0; return STATUS_SUCCESS; } diff --git a/src/sys/create.c b/src/sys/create.c index fc1fdfbd..d118f596 100644 --- a/src/sys/create.c +++ b/src/sys/create.c @@ -32,6 +32,14 @@ static NTSTATUS FspFsctlCreate( { PAGED_CODE(); + FSP_FSCTL_FILE_CONTEXT2 *FsContext2; + FsContext2 = FspAlloc(sizeof *FsContext2); + if (0 == FsContext2) + return STATUS_INSUFFICIENT_RESOURCES; + + RtlZeroMemory(FsContext2, sizeof *FsContext2); + IrpSp->FileObject->FsContext2 = FsContext2; + Irp->IoStatus.Information = FILE_OPENED; return STATUS_SUCCESS; } diff --git a/src/sys/device.c b/src/sys/device.c index e4339bb4..e3777cfb 100644 --- a/src/sys/device.c +++ b/src/sys/device.c @@ -17,19 +17,19 @@ VOID FspDeviceInitComplete(PDEVICE_OBJECT DeviceObject); VOID FspDeviceDelete(PDEVICE_OBJECT DeviceObject); BOOLEAN FspDeviceRetain(PDEVICE_OBJECT DeviceObject); VOID FspDeviceRelease(PDEVICE_OBJECT DeviceObject); -PVOID FspDeviceLookupContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier); -PVOID FspDeviceInsertContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, PVOID Context, - FSP_DEVICE_GENERIC_TABLE_ELEMENT *ElementStorage, PBOOLEAN PInserted); -VOID FspDeviceDeleteContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, - PBOOLEAN PDeleted); -static RTL_AVL_COMPARE_ROUTINE FspDeviceCompareElement; -static RTL_AVL_ALLOCATE_ROUTINE FspDeviceAllocateElement; -static RTL_AVL_FREE_ROUTINE FspDeviceFreeElement; static NTSTATUS FspFsvolDeviceInit(PDEVICE_OBJECT DeviceObject); static VOID FspFsvolDeviceInitComplete(PDEVICE_OBJECT DeviceObject); static VOID FspFsvolDeviceFini(PDEVICE_OBJECT DeviceObject); static IO_TIMER_ROUTINE FspFsvolDeviceTimerRoutine; static WORKER_THREAD_ROUTINE FspFsvolDeviceExpirationRoutine; +PVOID FspFsvolDeviceLookupContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier); +PVOID FspFsvolDeviceInsertContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, PVOID Context, + FSP_DEVICE_GENERIC_TABLE_ELEMENT *ElementStorage, PBOOLEAN PInserted); +VOID FspFsvolDeviceDeleteContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, + PBOOLEAN PDeleted); +static RTL_AVL_COMPARE_ROUTINE FspFsvolDeviceCompareElement; +static RTL_AVL_ALLOCATE_ROUTINE FspFsvolDeviceAllocateElement; +static RTL_AVL_FREE_ROUTINE FspFsvolDeviceFreeElement; NTSTATUS FspDeviceCopyList( PDEVICE_OBJECT **PDeviceObjects, PULONG PDeviceObjectCount); VOID FspDeviceDeleteList( @@ -41,15 +41,15 @@ VOID FspDeviceDeleteAll(VOID); #pragma alloc_text(PAGE, FspDeviceCreate) #pragma alloc_text(PAGE, FspDeviceInitComplete) #pragma alloc_text(PAGE, FspDeviceDelete) -#pragma alloc_text(PAGE, FspDeviceLookupContext) -#pragma alloc_text(PAGE, FspDeviceInsertContext) -#pragma alloc_text(PAGE, FspDeviceDeleteContext) -#pragma alloc_text(PAGE, FspDeviceCompareElement) -#pragma alloc_text(PAGE, FspDeviceAllocateElement) -#pragma alloc_text(PAGE, FspDeviceFreeElement) #pragma alloc_text(PAGE, FspFsvolDeviceInit) #pragma alloc_text(PAGE, FspFsvolDeviceInitComplete) #pragma alloc_text(PAGE, FspFsvolDeviceFini) +#pragma alloc_text(PAGE, FspFsvolDeviceLookupContext) +#pragma alloc_text(PAGE, FspFsvolDeviceInsertContext) +#pragma alloc_text(PAGE, FspFsvolDeviceDeleteContext) +#pragma alloc_text(PAGE, FspFsvolDeviceCompareElement) +#pragma alloc_text(PAGE, FspFsvolDeviceAllocateElement) +#pragma alloc_text(PAGE, FspFsvolDeviceFreeElement) #pragma alloc_text(PAGE, FspDeviceCopyList) #pragma alloc_text(PAGE, FspDeviceDeleteList) #pragma alloc_text(PAGE, FspDeviceDeleteAll) @@ -101,8 +101,6 @@ NTSTATUS FspDeviceCreateSecure(UINT32 Kind, ULONG ExtraSize, KeInitializeSpinLock(&DeviceExtension->SpinLock); DeviceExtension->RefCount = 1; ExInitializeResourceLite(&DeviceExtension->Resource); - RtlInitializeGenericTableAvl(&DeviceExtension->GenericTable, - FspDeviceCompareElement, FspDeviceAllocateElement, FspDeviceFreeElement, 0); DeviceExtension->Kind = Kind; switch (Kind) @@ -118,17 +116,6 @@ NTSTATUS FspDeviceCreateSecure(UINT32 Kind, ULONG ExtraSize, if (!NT_SUCCESS(Result)) { -#if 0 - /* FspDeviceFreeElement is now a no-op, so this is no longer necessary */ - /* - * Enumerate and delete all entries in the GenericTable. - * There is no need to protect accesses to the table as we are in the device destructor. - */ - FSP_DEVICE_GENERIC_TABLE_ELEMENT_DATA *Element; - while (0 != (Element = RtlGetElementGenericTableAvl(&DeviceExtension->GenericTable, 0))) - RtlDeleteElementGenericTableAvl(&DeviceExtension->GenericTable, &Element->Identifier); -#endif - ExDeleteResourceLite(&DeviceExtension->Resource); IoDeleteDevice(DeviceObject); } @@ -190,17 +177,6 @@ VOID FspDeviceDelete(PDEVICE_OBJECT DeviceObject) return; } -#if 0 - /* FspDeviceFreeElement is now a no-op, so this is no longer necessary */ - /* - * Enumerate and delete all entries in the GenericTable. - * There is no need to protect accesses to the table as we are in the device destructor. - */ - FSP_DEVICE_GENERIC_TABLE_ELEMENT_DATA *Element; - while (0 != (Element = RtlGetElementGenericTableAvl(&DeviceExtension->GenericTable, 0))) - RtlDeleteElementGenericTableAvl(&DeviceExtension->GenericTable, &Element->Identifier); -#endif - ExDeleteResourceLite(&DeviceExtension->Resource); IoDeleteDevice(DeviceObject); } @@ -244,92 +220,6 @@ VOID FspDeviceRelease(PDEVICE_OBJECT DeviceObject) FspDeviceDelete(DeviceObject); } -PVOID FspDeviceLookupContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier) -{ - PAGED_CODE(); - - FSP_DEVICE_EXTENSION *DeviceExtension = FspDeviceExtension(DeviceObject); - ASSERT(ExIsResourceAcquiredExclusiveLite(&DeviceExtension->Resource)); - - FSP_DEVICE_GENERIC_TABLE_ELEMENT_DATA *Result; - - Result = RtlLookupElementGenericTableAvl(&DeviceExtension->GenericTable, &Identifier); - - return 0 != Result ? Result->Context : 0; -} - -PVOID FspDeviceInsertContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, PVOID Context, - FSP_DEVICE_GENERIC_TABLE_ELEMENT *ElementStorage, PBOOLEAN PInserted) -{ - PAGED_CODE(); - - FSP_DEVICE_EXTENSION *DeviceExtension = FspDeviceExtension(DeviceObject); - ASSERT(ExIsResourceAcquiredExclusiveLite(&DeviceExtension->Resource)); - ASSERT(0 != ElementStorage); - - FSP_DEVICE_GENERIC_TABLE_ELEMENT_DATA *Result, Element = { 0 }; - Element.Identifier = Identifier; - Element.Context = Context; - - DeviceExtension->GenericTableElementStorage = ElementStorage; - Result = RtlInsertElementGenericTableAvl(&DeviceExtension->GenericTable, - &Element, sizeof Element, PInserted); - DeviceExtension->GenericTableElementStorage = 0; - - ASSERT(0 != Result); - - return Result->Context; -} - -VOID FspDeviceDeleteContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, - PBOOLEAN PDeleted) -{ - PAGED_CODE(); - - FSP_DEVICE_EXTENSION *DeviceExtension = FspDeviceExtension(DeviceObject); - ASSERT(ExIsResourceAcquiredExclusiveLite(&DeviceExtension->Resource)); - - BOOLEAN Deleted; - - Deleted = RtlDeleteElementGenericTableAvl(&DeviceExtension->GenericTable, &Identifier); - - if (0 != PDeleted) - *PDeleted = Deleted; -} - -static RTL_GENERIC_COMPARE_RESULTS NTAPI FspDeviceCompareElement( - PRTL_AVL_TABLE Table, PVOID FirstElement, PVOID SecondElement) -{ - PAGED_CODE(); - - if (FirstElement < SecondElement) - return GenericLessThan; - else - if (SecondElement < FirstElement) - return GenericGreaterThan; - else - return GenericEqual; -} - -static PVOID NTAPI FspDeviceAllocateElement( - PRTL_AVL_TABLE Table, CLONG ByteSize) -{ - PAGED_CODE(); - - FSP_DEVICE_EXTENSION *DeviceExtension = - CONTAINING_RECORD(Table, FSP_DEVICE_EXTENSION, GenericTable); - - ASSERT(sizeof(FSP_DEVICE_GENERIC_TABLE_ELEMENT) == ByteSize); - - return DeviceExtension->GenericTableElementStorage; -} - -static VOID NTAPI FspDeviceFreeElement( - PRTL_AVL_TABLE Table, PVOID Buffer) -{ - PAGED_CODE(); -} - static NTSTATUS FspFsvolDeviceInit(PDEVICE_OBJECT DeviceObject) { PAGED_CODE(); @@ -337,6 +227,11 @@ static NTSTATUS FspFsvolDeviceInit(PDEVICE_OBJECT DeviceObject) NTSTATUS Result; FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject); + /* initialize our timer routine */ + Result = IoInitializeTimer(DeviceObject, FspFsvolDeviceTimerRoutine, 0); + if (!NT_SUCCESS(Result)) + return Result; + /* allocate a spare VPB in case we are mounted on an fsvrt */ if (FILE_DEVICE_DISK_FILE_SYSTEM == DeviceObject->DeviceType) { @@ -352,10 +247,9 @@ static NTSTATUS FspFsvolDeviceInit(PDEVICE_OBJECT DeviceObject) ExInitializeWorkItem(&FsvolDeviceExtension->ExpirationWorkItem, FspFsvolDeviceExpirationRoutine, DeviceObject); - /* initialize our timer routine */ - Result = IoInitializeTimer(DeviceObject, FspFsvolDeviceTimerRoutine, 0); - if (!NT_SUCCESS(Result)) - return Result; + /* initialize our generic table */ + RtlInitializeGenericTableAvl(&FsvolDeviceExtension->GenericTable, + FspFsvolDeviceCompareElement, FspFsvolDeviceAllocateElement, FspFsvolDeviceFreeElement, 0); return STATUS_SUCCESS; } @@ -391,6 +285,17 @@ static VOID FspFsvolDeviceFini(PDEVICE_OBJECT DeviceObject) */ IoStopTimer(DeviceObject); +#if 0 + /* FspDeviceFreeElement is now a no-op, so this is no longer necessary */ + /* + * Enumerate and delete all entries in the GenericTable. + * There is no need to protect accesses to the table as we are in the device destructor. + */ + FSP_DEVICE_GENERIC_TABLE_ELEMENT_DATA *Element; + while (0 != (Element = RtlGetElementGenericTableAvl(&FsvolDeviceExtension->GenericTable, 0))) + RtlDeleteElementGenericTableAvl(&FsvolDeviceExtension->GenericTable, &Element->Identifier); +#endif + /* * Dereference the virtual volume device so that it can now go away. */ @@ -454,6 +359,92 @@ static VOID FspFsvolDeviceExpirationRoutine(PVOID Context) ObDereferenceObject(DeviceObject); } +PVOID FspFsvolDeviceLookupContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier) +{ + PAGED_CODE(); + + FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject); + ASSERT(ExIsResourceAcquiredExclusiveLite(&FsvolDeviceExtension->Base.Resource)); + + FSP_DEVICE_GENERIC_TABLE_ELEMENT_DATA *Result; + + Result = RtlLookupElementGenericTableAvl(&FsvolDeviceExtension->GenericTable, &Identifier); + + return 0 != Result ? Result->Context : 0; +} + +PVOID FspFsvolDeviceInsertContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, PVOID Context, + FSP_DEVICE_GENERIC_TABLE_ELEMENT *ElementStorage, PBOOLEAN PInserted) +{ + PAGED_CODE(); + + FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject); + ASSERT(ExIsResourceAcquiredExclusiveLite(&FsvolDeviceExtension->Base.Resource)); + ASSERT(0 != ElementStorage); + + FSP_DEVICE_GENERIC_TABLE_ELEMENT_DATA *Result, Element = { 0 }; + Element.Identifier = Identifier; + Element.Context = Context; + + FsvolDeviceExtension->GenericTableElementStorage = ElementStorage; + Result = RtlInsertElementGenericTableAvl(&FsvolDeviceExtension->GenericTable, + &Element, sizeof Element, PInserted); + FsvolDeviceExtension->GenericTableElementStorage = 0; + + ASSERT(0 != Result); + + return Result->Context; +} + +VOID FspFsvolDeviceDeleteContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, + PBOOLEAN PDeleted) +{ + PAGED_CODE(); + + FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(DeviceObject); + ASSERT(ExIsResourceAcquiredExclusiveLite(&FsvolDeviceExtension->Base.Resource)); + + BOOLEAN Deleted; + + Deleted = RtlDeleteElementGenericTableAvl(&FsvolDeviceExtension->GenericTable, &Identifier); + + if (0 != PDeleted) + *PDeleted = Deleted; +} + +static RTL_GENERIC_COMPARE_RESULTS NTAPI FspFsvolDeviceCompareElement( + PRTL_AVL_TABLE Table, PVOID FirstElement, PVOID SecondElement) +{ + PAGED_CODE(); + + if (FirstElement < SecondElement) + return GenericLessThan; + else + if (SecondElement < FirstElement) + return GenericGreaterThan; + else + return GenericEqual; +} + +static PVOID NTAPI FspFsvolDeviceAllocateElement( + PRTL_AVL_TABLE Table, CLONG ByteSize) +{ + PAGED_CODE(); + + FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = + CONTAINING_RECORD(Table, FSP_FSVOL_DEVICE_EXTENSION, GenericTable); + + ASSERT(sizeof(FSP_DEVICE_GENERIC_TABLE_ELEMENT) == ByteSize); + + return FsvolDeviceExtension->GenericTableElementStorage; +} + +static VOID NTAPI FspFsvolDeviceFreeElement( + PRTL_AVL_TABLE Table, PVOID Buffer) +{ + PAGED_CODE(); +} + NTSTATUS FspDeviceCopyList( PDEVICE_OBJECT **PDeviceObjects, PULONG PDeviceObjectCount) { diff --git a/src/sys/driver.h b/src/sys/driver.h index 9184a8de..5f51d4a8 100644 --- a/src/sys/driver.h +++ b/src/sys/driver.h @@ -355,8 +355,6 @@ typedef struct KSPIN_LOCK SpinLock; LONG RefCount; ERESOURCE Resource; - RTL_AVL_TABLE GenericTable; - PVOID GenericTableElementStorage; UINT32 Kind; } FSP_DEVICE_EXTENSION; typedef struct @@ -374,6 +372,8 @@ typedef struct KSPIN_LOCK ExpirationLock; WORK_QUEUE_ITEM ExpirationWorkItem; BOOLEAN ExpirationInProgress; + RTL_AVL_TABLE GenericTable; + PVOID GenericTableElementStorage; } FSP_FSVOL_DEVICE_EXTENSION; static inline FSP_DEVICE_EXTENSION *FspDeviceExtension(PDEVICE_OBJECT DeviceObject) @@ -397,10 +397,10 @@ VOID FspDeviceInitComplete(PDEVICE_OBJECT DeviceObject); VOID FspDeviceDelete(PDEVICE_OBJECT DeviceObject); BOOLEAN FspDeviceRetain(PDEVICE_OBJECT DeviceObject); VOID FspDeviceRelease(PDEVICE_OBJECT DeviceObject); -PVOID FspDeviceLookupContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier); -PVOID FspDeviceInsertContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, PVOID Context, +PVOID FspFsvolDeviceLookupContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier); +PVOID FspFsvolDeviceInsertContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, PVOID Context, FSP_DEVICE_GENERIC_TABLE_ELEMENT *ElementStorage, PBOOLEAN PInserted); -VOID FspDeviceDeleteContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, +VOID FspFsvolDeviceDeleteContext(PDEVICE_OBJECT DeviceObject, UINT64 Identifier, PBOOLEAN PDeleted); NTSTATUS FspDeviceCopyList( PDEVICE_OBJECT **PDeviceObjects, PULONG PDeviceObjectCount); @@ -408,6 +408,12 @@ VOID FspDeviceDeleteList( PDEVICE_OBJECT *DeviceObjects, ULONG DeviceObjectCount); VOID FspDeviceDeleteAll(VOID); +/* fsctl file objects */ +typedef struct +{ + PDEVICE_OBJECT FsvolDeviceObject; +} FSP_FSCTL_FILE_CONTEXT2; + /* debug */ #if DBG BOOLEAN HasDbgBreakPoint(const char *Function); diff --git a/src/sys/fsctl.c b/src/sys/fsctl.c index b77c6a19..5d1a2e9d 100644 --- a/src/sys/fsctl.c +++ b/src/sys/fsctl.c @@ -69,6 +69,7 @@ static NTSTATUS FspFsctlCreateVolume( { PAGED_CODE(); +#if 0 /* check parameters */ ULONG InputBufferLength = IrpSp->Parameters.FileSystemControl.InputBufferLength; ULONG OutputBufferLength = IrpSp->Parameters.FileSystemControl.OutputBufferLength; @@ -189,6 +190,9 @@ static NTSTATUS FspFsctlCreateVolume( } return Result; +#else + return STATUS_INVALID_DEVICE_REQUEST; +#endif } static NTSTATUS FspFsctlMountVolume( @@ -196,6 +200,7 @@ static NTSTATUS FspFsctlMountVolume( { PAGED_CODE(); +#if 0 NTSTATUS Result; FSP_DEVICE_EXTENSION *DeviceExtension = FspDeviceExtension(DeviceObject); @@ -237,6 +242,9 @@ static NTSTATUS FspFsctlMountVolume( } return Result; +#else + return STATUS_INVALID_DEVICE_REQUEST; +#endif } VOID FspFsctlDeleteVolume( @@ -245,6 +253,7 @@ VOID FspFsctlDeleteVolume( /* performed during IRP_MJ_CLEANUP! */ PAGED_CODE(); +#if 0 PDEVICE_OBJECT FsvolDeviceObject = 0; FSP_DEVICE_EXTENSION *DeviceExtension = FspDeviceExtension(DeviceObject); @@ -336,12 +345,15 @@ VOID FspFsctlDeleteVolume( FspDeviceRelease(FsvolDeviceObject); } } +#endif } static VOID FspFsctlDeleteVolumeDelayed(PVOID Context) { PAGED_CODE(); +#if 0 + PDEVICE_OBJECT FsvolDeviceObject = Context; FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension = FspFsvolDeviceExtension(FsvolDeviceObject); KIRQL Irql; @@ -365,6 +377,7 @@ static VOID FspFsctlDeleteVolumeDelayed(PVOID Context) DelayTimeout.QuadPart = 300/*ms*/ * -10000; FspQueueWorkItemWithDelay(&FsvolDeviceExtension->DeleteVolumeWorkItem, DelayTimeout); } +#endif } static NTSTATUS FspFsctlTransact(