From 8653bb1b322afff64f0361b0d4e801f2ab889e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sun, 5 Jan 2025 18:06:45 +0100 Subject: [PATCH] [PARTMGR] Few code enhancements. - PartMgrAddDevice(): If IoAttachDeviceToDeviceStack() fails, bail out immediately, instead of continuing initializing the device extension then deciding to fail. - PartitionCreateDevice(): Initialize the fields pertaining to parent device linking in the partition device extension, before the other fields (following in a similar way what's done in PartMgrAddDevice). --- drivers/storage/partmgr/partition.c | 6 +++--- drivers/storage/partmgr/partmgr.c | 13 ++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/drivers/storage/partmgr/partition.c b/drivers/storage/partmgr/partition.c index f54ccf55596..e8a38e728ca 100644 --- a/drivers/storage/partmgr/partition.c +++ b/drivers/storage/partmgr/partition.c @@ -52,6 +52,9 @@ PartitionCreateDevice( PPARTITION_EXTENSION partExt = partitionDevice->DeviceExtension; RtlZeroMemory(partExt, sizeof(*partExt)); + partExt->DeviceObject = partitionDevice; + partExt->LowerDevice = FDObject; + partitionDevice->StackSize = FDObject->StackSize; partitionDevice->Flags |= DO_DIRECT_IO; @@ -77,9 +80,6 @@ PartitionCreateDevice( partExt->DetectedNumber = PdoNumber; // counts only partitions with PDO created partExt->VolumeNumber = volumeNum; - partExt->DeviceObject = partitionDevice; - partExt->LowerDevice = FDObject; - // The device is initialized partitionDevice->Flags &= ~DO_DEVICE_INITIALIZING; diff --git a/drivers/storage/partmgr/partmgr.c b/drivers/storage/partmgr/partmgr.c index fd3fd89c247..b1c37b5d964 100644 --- a/drivers/storage/partmgr/partmgr.c +++ b/drivers/storage/partmgr/partmgr.c @@ -1201,16 +1201,15 @@ PartMgrAddDevice( deviceExtension->IsFDO = TRUE; deviceExtension->DeviceObject = deviceObject; deviceExtension->LowerDevice = IoAttachDeviceToDeviceStack(deviceObject, PhysicalDeviceObject); + if (!deviceExtension->LowerDevice) + { + // The attachment failed + IoDeleteDevice(deviceObject); + return STATUS_DEVICE_REMOVED; + } deviceExtension->PhysicalDiskDO = PhysicalDeviceObject; KeInitializeEvent(&deviceExtension->SyncEvent, SynchronizationEvent, TRUE); - // the the attaching failed - if (!deviceExtension->LowerDevice) - { - IoDeleteDevice(deviceObject); - - return STATUS_DEVICE_REMOVED; - } deviceObject->Flags |= DO_DIRECT_IO | DO_POWER_PAGABLE; // The device is initialized