2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/device.c
5 * PURPOSE: Device Object Management, including Notifications and Queues.
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Filip Navara (navaraf@reactos.org)
8 * Hervé Poussineau (hpoussin@reactos.org)
11 /* INCLUDES *******************************************************************/
17 /* GLOBALS ********************************************************************/
19 ULONG IopDeviceObjectNumber
= 0;
20 LIST_ENTRY ShutdownListHead
, LastChanceShutdownListHead
;
21 KSPIN_LOCK ShutdownListLock
;
22 extern LIST_ENTRY IopDiskFileSystemQueueHead
;
23 extern LIST_ENTRY IopCdRomFileSystemQueueHead
;
24 extern LIST_ENTRY IopTapeFileSystemQueueHead
;
25 extern ERESOURCE IopDatabaseResource
;
27 /* PRIVATE FUNCTIONS **********************************************************/
31 IopReadyDeviceObjects(IN PDRIVER_OBJECT Driver
)
33 PDEVICE_OBJECT DeviceObject
;
36 /* Set the driver as initialized */
37 Driver
->Flags
|= DRVO_INITIALIZED
;
38 DeviceObject
= Driver
->DeviceObject
;
41 /* Set every device as initialized too */
42 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
43 DeviceObject
= DeviceObject
->NextDevice
;
49 IopDeleteDevice(IN PVOID ObjectBody
)
51 PDEVICE_OBJECT DeviceObject
= ObjectBody
;
52 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
55 /* Cleanup and free the device node */
57 IopFreeDeviceNode(DeviceNode
);
59 /* Dereference the driver object, referenced in IoCreateDevice */
60 if (DeviceObject
->DriverObject
)
61 ObDereferenceObject(DeviceObject
->DriverObject
);
67 IopAttachDeviceToDeviceStackSafe(IN PDEVICE_OBJECT SourceDevice
,
68 IN PDEVICE_OBJECT TargetDevice
,
69 OUT PDEVICE_OBJECT
*AttachedToDeviceObject OPTIONAL
)
71 PDEVICE_OBJECT AttachedDevice
;
72 PEXTENDED_DEVOBJ_EXTENSION SourceDeviceExtension
;
74 /* Get the Attached Device and source extension */
75 AttachedDevice
= IoGetAttachedDevice(TargetDevice
);
76 SourceDeviceExtension
= IoGetDevObjExtension(SourceDevice
);
77 ASSERT(SourceDeviceExtension
->AttachedTo
== NULL
);
79 /* Make sure that it's in a correct state */
80 if ((AttachedDevice
->Flags
& DO_DEVICE_INITIALIZING
) ||
81 (IoGetDevObjExtension(AttachedDevice
)->ExtensionFlags
&
85 DOE_REMOVE_PROCESSED
)))
87 /* Device was unloading or being removed */
88 AttachedDevice
= NULL
;
92 /* Update atached device fields */
93 AttachedDevice
->AttachedDevice
= SourceDevice
;
94 AttachedDevice
->Spare1
++;
96 /* Update the source with the attached data */
97 SourceDevice
->StackSize
= AttachedDevice
->StackSize
+ 1;
98 SourceDevice
->AlignmentRequirement
= AttachedDevice
->
100 SourceDevice
->SectorSize
= AttachedDevice
->SectorSize
;
102 /* Check for pending start flag */
103 if (IoGetDevObjExtension(AttachedDevice
)->ExtensionFlags
&
107 IoGetDevObjExtension(SourceDevice
)->ExtensionFlags
|=
111 /* Set the attachment in the device extension */
112 SourceDeviceExtension
->AttachedTo
= AttachedDevice
;
115 /* Return the attached device */
116 if (AttachedToDeviceObject
) *AttachedToDeviceObject
= AttachedDevice
;
117 return AttachedDevice
;
122 IoShutdownPnpDevices(VOID
)
124 /* This routine is only used by Driver Verifier to validate shutdown */
130 IoShutdownSystem(IN ULONG Phase
)
132 PLIST_ENTRY ListEntry
;
133 PDEVICE_OBJECT DeviceObject
;
134 PSHUTDOWN_ENTRY ShutdownEntry
;
135 IO_STATUS_BLOCK StatusBlock
;
140 /* Initialize an event to wait on */
141 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
147 IoShutdownPnpDevices();
149 /* Loop first-chance shutdown notifications */
150 ListEntry
= ExInterlockedRemoveHeadList(&ShutdownListHead
,
154 /* Get the shutdown entry */
155 ShutdownEntry
= CONTAINING_RECORD(ListEntry
,
159 /* Get the attached device */
160 DeviceObject
= IoGetAttachedDevice(ShutdownEntry
->DeviceObject
);
162 /* Build the shutdown IRP and call the driver */
163 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_SHUTDOWN
,
172 Status
= IoCallDriver(DeviceObject
, Irp
);
173 if (Status
== STATUS_PENDING
)
175 /* Wait on the driver */
176 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
180 /* Remove the flag */
181 ShutdownEntry
->DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
183 /* Get rid of our reference to it */
184 ObDereferenceObject(ShutdownEntry
->DeviceObject
);
186 /* Free the shutdown entry and reset the event */
187 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
188 KeClearEvent(&Event
);
190 /* Go to the next entry */
191 ListEntry
= ExInterlockedRemoveHeadList(&ShutdownListHead
,
197 /* Acquire resource forever */
198 ExAcquireResourceExclusiveLite(&IopDatabaseResource
, TRUE
);
200 /* Shutdown disk file systems */
201 IopShutdownBaseFileSystems(&IopDiskFileSystemQueueHead
);
203 /* Shutdown cdrom file systems */
204 IopShutdownBaseFileSystems(&IopCdRomFileSystemQueueHead
);
206 /* Shutdown tape filesystems */
207 IopShutdownBaseFileSystems(&IopTapeFileSystemQueueHead
);
209 /* Loop last-chance shutdown notifications */
210 ListEntry
= ExInterlockedRemoveHeadList(&LastChanceShutdownListHead
,
214 /* Get the shutdown entry */
215 ShutdownEntry
= CONTAINING_RECORD(ListEntry
,
219 /* Get the attached device */
220 DeviceObject
= IoGetAttachedDevice(ShutdownEntry
->DeviceObject
);
222 /* Build the shutdown IRP and call the driver */
223 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_SHUTDOWN
,
232 Status
= IoCallDriver(DeviceObject
, Irp
);
233 if (Status
== STATUS_PENDING
)
235 /* Wait on the driver */
236 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
240 /* Remove the flag */
241 ShutdownEntry
->DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
243 /* Get rid of our reference to it */
244 ObDereferenceObject(ShutdownEntry
->DeviceObject
);
246 /* Free the shutdown entry and reset the event */
247 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
248 KeClearEvent(&Event
);
250 /* Go to the next entry */
251 ListEntry
= ExInterlockedRemoveHeadList(&LastChanceShutdownListHead
,
260 IopGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName
,
261 IN ACCESS_MASK DesiredAccess
,
262 OUT PFILE_OBJECT
*FileObject
,
263 OUT PDEVICE_OBJECT
*DeviceObject
,
266 OBJECT_ATTRIBUTES ObjectAttributes
;
267 IO_STATUS_BLOCK StatusBlock
;
268 PFILE_OBJECT LocalFileObject
;
272 /* Open the Device */
273 InitializeObjectAttributes(&ObjectAttributes
,
278 Status
= ZwOpenFile(&FileHandle
,
283 FILE_NON_DIRECTORY_FILE
| AttachFlag
);
284 if (!NT_SUCCESS(Status
)) return Status
;
286 /* Get File Object */
287 Status
= ObReferenceObjectByHandle(FileHandle
,
291 (PVOID
*)&LocalFileObject
,
293 if (NT_SUCCESS(Status
))
295 /* Return the requested data */
296 *DeviceObject
= IoGetRelatedDeviceObject(LocalFileObject
);
297 *FileObject
= LocalFileObject
;
300 /* Close the handle */
308 IopGetLowestDevice(IN PDEVICE_OBJECT DeviceObject
)
310 PDEVICE_OBJECT LowestDevice
;
311 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
313 /* Get the current device and its extension */
314 LowestDevice
= DeviceObject
;
315 DeviceExtension
= IoGetDevObjExtension(LowestDevice
);
317 /* Keep looping as long as we're attached */
318 while (DeviceExtension
->AttachedTo
)
320 /* Get the lowest device and its extension */
321 LowestDevice
= DeviceExtension
->AttachedTo
;
322 DeviceExtension
= IoGetDevObjExtension(LowestDevice
);
325 /* Return the lowest device */
331 IopEditDeviceList(IN PDRIVER_OBJECT DriverObject
,
332 IN PDEVICE_OBJECT DeviceObject
,
333 IN IOP_DEVICE_LIST_OPERATION Type
)
335 PDEVICE_OBJECT Previous
;
337 /* Check the type of operation */
338 if (Type
== IopRemove
)
340 /* Get the current device and check if it's the current one */
341 Previous
= DeviceObject
->DriverObject
->DeviceObject
;
342 if (Previous
== DeviceObject
)
344 /* It is, simply unlink this one directly */
345 DeviceObject
->DriverObject
->DeviceObject
=
346 DeviceObject
->NextDevice
;
350 /* It's not, so loop until we find the device */
351 while (Previous
->NextDevice
!= DeviceObject
)
353 /* Not this one, keep moving */
354 Previous
= Previous
->NextDevice
;
357 /* We found it, now unlink us */
358 Previous
->NextDevice
= DeviceObject
->NextDevice
;
363 /* Link the device object and the driver object */
364 DeviceObject
->NextDevice
= DriverObject
->DeviceObject
;
365 DriverObject
->DeviceObject
= DeviceObject
;
371 IopUnloadDevice(IN PDEVICE_OBJECT DeviceObject
)
373 PDRIVER_OBJECT DriverObject
= DeviceObject
->DriverObject
;
374 PEXTENDED_DEVOBJ_EXTENSION ThisExtension
= IoGetDevObjExtension(DeviceObject
);
376 /* Check if deletion is pending */
377 if (ThisExtension
->ExtensionFlags
& DOE_DELETE_PENDING
)
379 if (DeviceObject
->AttachedDevice
)
381 DPRINT("Device object is in the middle of a device stack\n");
385 if (DeviceObject
->ReferenceCount
)
387 DPRINT("Device object still has %d references\n", DeviceObject
->ReferenceCount
);
391 /* Check if we have a Security Descriptor */
392 if (DeviceObject
->SecurityDescriptor
)
395 ExFreePoolWithTag(DeviceObject
->SecurityDescriptor
, TAG_SD
);
398 /* Remove the device from the list */
399 IopEditDeviceList(DeviceObject
->DriverObject
, DeviceObject
, IopRemove
);
401 /* Dereference the keep-alive */
402 ObDereferenceObject(DeviceObject
);
405 /* We can't unload a non-PnP driver here */
406 if (DriverObject
->Flags
& DRVO_LEGACY_DRIVER
)
408 DPRINT("Not a PnP driver! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
412 /* Return if we've already called unload (maybe we're in it?) */
413 if (DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
) return;
415 /* We can't unload unless there's an unload handler */
416 if (!DriverObject
->DriverUnload
)
418 DPRINT1("No DriverUnload function on PnP driver! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
422 /* Bail if there are still devices present */
423 if (DriverObject
->DeviceObject
)
425 DPRINT("Devices still present! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
429 DPRINT1("Unloading driver '%wZ' (automatic)\n", &DriverObject
->DriverName
);
431 /* Set the unload invoked flag */
432 DriverObject
->Flags
|= DRVO_UNLOAD_INVOKED
;
435 DriverObject
->DriverUnload(DriverObject
);
437 /* Make object temporary so it can be deleted */
438 ObMakeTemporaryObject(DriverObject
);
443 IopDereferenceDeviceObject(IN PDEVICE_OBJECT DeviceObject
,
444 IN BOOLEAN ForceUnload
)
447 ASSERT(DeviceObject
->ReferenceCount
);
449 /* Dereference the device */
450 InterlockedDecrement(&DeviceObject
->ReferenceCount
);
453 * Check if we can unload it and it's safe to unload (or if we're forcing
454 * an unload, which is OK too).
456 ASSERT(!ForceUnload
);
457 if (!(DeviceObject
->ReferenceCount
) &&
458 (IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
& DOE_DELETE_PENDING
))
461 IopUnloadDevice(DeviceObject
);
467 IopStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
468 IN BOOLEAN Cancelable
,
471 PKDEVICE_QUEUE_ENTRY Entry
;
475 /* Acquire the cancel lock if this is cancelable */
476 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
478 /* Clear the current IRP */
479 DeviceObject
->CurrentIrp
= NULL
;
481 /* Remove an entry from the queue */
482 Entry
= KeRemoveByKeyDeviceQueue(&DeviceObject
->DeviceQueue
, Key
);
485 /* Get the IRP and set it */
486 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
487 DeviceObject
->CurrentIrp
= Irp
;
489 /* Check if this is a cancelable packet */
492 /* Check if the caller requested no cancellation */
493 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
496 /* He did, so remove the cancel routine */
497 Irp
->CancelRoutine
= NULL
;
500 /* Release the cancel lock */
501 IoReleaseCancelSpinLock(OldIrql
);
504 /* Call the Start I/O Routine */
505 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
509 /* Otherwise, release the cancel lock if we had acquired it */
510 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
516 IopStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
517 IN BOOLEAN Cancelable
)
519 PKDEVICE_QUEUE_ENTRY Entry
;
523 /* Acquire the cancel lock if this is cancelable */
524 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
526 /* Clear the current IRP */
527 DeviceObject
->CurrentIrp
= NULL
;
529 /* Remove an entry from the queue */
530 Entry
= KeRemoveDeviceQueue(&DeviceObject
->DeviceQueue
);
533 /* Get the IRP and set it */
534 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
535 DeviceObject
->CurrentIrp
= Irp
;
537 /* Check if this is a cancelable packet */
540 /* Check if the caller requested no cancellation */
541 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
544 /* He did, so remove the cancel routine */
545 Irp
->CancelRoutine
= NULL
;
548 /* Release the cancel lock */
549 IoReleaseCancelSpinLock(OldIrql
);
552 /* Call the Start I/O Routine */
553 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
557 /* Otherwise, release the cancel lock if we had acquired it */
558 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
564 IopStartNextPacketByKeyEx(IN PDEVICE_OBJECT DeviceObject
,
568 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
569 ULONG CurrentKey
= Key
;
570 ULONG CurrentFlags
= Flags
;
572 /* Get the device extension and start the packet loop */
573 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
576 /* Increase the count */
577 if (InterlockedIncrement(&DeviceExtension
->StartIoCount
) > 1)
580 * We've already called the routine once...
581 * All we have to do is save the key and add the new flags
583 DeviceExtension
->StartIoFlags
|= CurrentFlags
;
584 DeviceExtension
->StartIoKey
= CurrentKey
;
588 /* Mask out the current packet flags and key */
589 DeviceExtension
->StartIoFlags
&= ~(DOE_SIO_WITH_KEY
|
592 DeviceExtension
->StartIoKey
= 0;
594 /* Check if this is a packet start with key */
595 if (Flags
& DOE_SIO_WITH_KEY
)
597 /* Start the packet with a key */
598 IopStartNextPacketByKey(DeviceObject
,
599 (Flags
& DOE_SIO_CANCELABLE
) ?
603 else if (Flags
& DOE_SIO_NO_KEY
)
605 /* Start the packet */
606 IopStartNextPacket(DeviceObject
,
607 (Flags
& DOE_SIO_CANCELABLE
) ?
612 /* Decrease the Start I/O count and check if it's 0 now */
613 if (!InterlockedDecrement(&DeviceExtension
->StartIoCount
))
615 /* Get the current active key and flags */
616 CurrentKey
= DeviceExtension
->StartIoKey
;
617 CurrentFlags
= DeviceExtension
->StartIoFlags
& (DOE_SIO_WITH_KEY
|
621 /* Check if we should still loop */
622 if (!(CurrentFlags
& (DOE_SIO_WITH_KEY
| DOE_SIO_NO_KEY
))) break;
626 /* There are still Start I/Os active, so quit this loop */
634 IopGetRelatedTargetDevice(IN PFILE_OBJECT FileObject
,
635 OUT PDEVICE_NODE
*DeviceNode
)
638 IO_STACK_LOCATION Stack
= {0};
639 PDEVICE_RELATIONS DeviceRelations
;
640 PDEVICE_OBJECT DeviceObject
= NULL
;
644 /* Get DeviceObject related to given FileObject */
645 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
646 if (!DeviceObject
) return STATUS_NO_SUCH_DEVICE
;
648 /* Define input parameters */
649 Stack
.MajorFunction
= IRP_MJ_PNP
;
650 Stack
.MinorFunction
= IRP_MN_QUERY_DEVICE_RELATIONS
;
651 Stack
.Parameters
.QueryDeviceRelations
.Type
= TargetDeviceRelation
;
652 Stack
.FileObject
= FileObject
;
654 /* Call the driver to query all relations (IRP_MJ_PNP) */
655 Status
= IopSynchronousCall(DeviceObject
,
657 (PVOID
)&DeviceRelations
);
658 if (!NT_SUCCESS(Status
)) return Status
;
660 /* Make sure it's not NULL and contains only one object */
661 ASSERT(DeviceRelations
);
662 ASSERT(DeviceRelations
->Count
== 1);
664 /* Finally get the device node */
665 *DeviceNode
= IopGetDeviceNode(DeviceRelations
->Objects
[0]);
666 if (!*DeviceNode
) Status
= STATUS_NO_SUCH_DEVICE
;
668 /* Free the DEVICE_RELATIONS structure, it's not needed anymore */
669 ExFreePool(DeviceRelations
);
674 /* PUBLIC FUNCTIONS ***********************************************************/
679 * Layers a device over the highest device in a device stack.
683 * Device to be attached.
686 * Name of the target device.
689 * Caller storage for the device attached to.
696 IoAttachDevice(PDEVICE_OBJECT SourceDevice
,
697 PUNICODE_STRING TargetDeviceName
,
698 PDEVICE_OBJECT
*AttachedDevice
)
701 PFILE_OBJECT FileObject
= NULL
;
702 PDEVICE_OBJECT TargetDevice
= NULL
;
704 /* Call the helper routine for an attach operation */
705 Status
= IopGetDeviceObjectPointer(TargetDeviceName
,
706 FILE_READ_ATTRIBUTES
,
709 IO_ATTACH_DEVICE_API
);
710 if (!NT_SUCCESS(Status
)) return Status
;
712 /* Attach the device */
713 Status
= IoAttachDeviceToDeviceStackSafe(SourceDevice
,
718 ObDereferenceObject(FileObject
);
723 * IoAttachDeviceByPointer
730 IoAttachDeviceByPointer(IN PDEVICE_OBJECT SourceDevice
,
731 IN PDEVICE_OBJECT TargetDevice
)
733 PDEVICE_OBJECT AttachedDevice
;
734 NTSTATUS Status
= STATUS_SUCCESS
;
737 AttachedDevice
= IoAttachDeviceToDeviceStack(SourceDevice
, TargetDevice
);
738 if (!AttachedDevice
) Status
= STATUS_NO_SUCH_DEVICE
;
740 /* Return the status */
749 IoAttachDeviceToDeviceStack(IN PDEVICE_OBJECT SourceDevice
,
750 IN PDEVICE_OBJECT TargetDevice
)
752 /* Attach it safely */
753 return IopAttachDeviceToDeviceStackSafe(SourceDevice
,
763 IoAttachDeviceToDeviceStackSafe(IN PDEVICE_OBJECT SourceDevice
,
764 IN PDEVICE_OBJECT TargetDevice
,
765 IN OUT PDEVICE_OBJECT
*AttachedToDeviceObject
)
767 /* Call the internal function */
768 if (!IopAttachDeviceToDeviceStackSafe(SourceDevice
,
770 AttachedToDeviceObject
))
773 return STATUS_NO_SUCH_DEVICE
;
777 return STATUS_SUCCESS
;
783 * Allocates memory for and intializes a device object for use for
788 * Driver object passed by IO Manager when the driver was loaded.
790 * DeviceExtensionSize
791 * Number of bytes for the device extension.
794 * Unicode name of device.
797 * Device type of the new device.
799 * DeviceCharacteristics
800 * Bit mask of device characteristics.
803 * TRUE if only one thread can access the device at a time.
806 * On successful return this parameter is filled by pointer to
807 * allocated device object.
814 IoCreateDevice(IN PDRIVER_OBJECT DriverObject
,
815 IN ULONG DeviceExtensionSize
,
816 IN PUNICODE_STRING DeviceName
,
817 IN DEVICE_TYPE DeviceType
,
818 IN ULONG DeviceCharacteristics
,
819 IN BOOLEAN Exclusive
,
820 OUT PDEVICE_OBJECT
*DeviceObject
)
822 WCHAR AutoNameBuffer
[20];
823 UNICODE_STRING AutoName
;
824 PDEVICE_OBJECT CreatedDeviceObject
;
825 PDEVOBJ_EXTENSION DeviceObjectExtension
;
826 OBJECT_ATTRIBUTES ObjectAttributes
;
828 ULONG AlignedDeviceExtensionSize
;
833 /* Check if we have to generate a name */
834 if (DeviceCharacteristics
& FILE_AUTOGENERATED_DEVICE_NAME
)
837 swprintf(AutoNameBuffer
,
839 InterlockedIncrementUL(&IopDeviceObjectNumber
));
841 /* Initialize the name */
842 RtlInitUnicodeString(&AutoName
, AutoNameBuffer
);
843 DeviceName
= &AutoName
;
846 /* Initialize the Object Attributes */
847 InitializeObjectAttributes(&ObjectAttributes
,
853 /* Honor exclusive flag */
854 if (Exclusive
) ObjectAttributes
.Attributes
|= OBJ_EXCLUSIVE
;
856 /* Create a permanent object for named devices */
857 if (DeviceName
) ObjectAttributes
.Attributes
|= OBJ_PERMANENT
;
859 /* Align the Extension Size to 8-bytes */
860 AlignedDeviceExtensionSize
= (DeviceExtensionSize
+ 7) &~ 7;
863 TotalSize
= AlignedDeviceExtensionSize
+
864 sizeof(DEVICE_OBJECT
) +
865 sizeof(EXTENDED_DEVOBJ_EXTENSION
);
867 /* Create the Device Object */
868 *DeviceObject
= NULL
;
869 Status
= ObCreateObject(KernelMode
,
877 (PVOID
*)&CreatedDeviceObject
);
878 if (!NT_SUCCESS(Status
)) return Status
;
880 /* Clear the whole Object and extension so we don't null stuff manually */
881 RtlZeroMemory(CreatedDeviceObject
, TotalSize
);
884 * Setup the Type and Size. Note that we don't use the aligned size,
885 * because that's only padding for the DevObjExt and not part of the Object.
887 CreatedDeviceObject
->Type
= IO_TYPE_DEVICE
;
888 CreatedDeviceObject
->Size
= sizeof(DEVICE_OBJECT
) + (USHORT
)DeviceExtensionSize
;
890 /* The kernel extension is after the driver internal extension */
891 DeviceObjectExtension
= (PDEVOBJ_EXTENSION
)
892 ((ULONG_PTR
)(CreatedDeviceObject
+ 1) +
893 AlignedDeviceExtensionSize
);
895 /* Set the Type and Size. Question: why is Size 0 on Windows? */
896 DeviceObjectExtension
->Type
= IO_TYPE_DEVICE_OBJECT_EXTENSION
;
897 DeviceObjectExtension
->Size
= 0;
899 /* Initialize with Power Manager */
900 PoInitializeDeviceObject(DeviceObjectExtension
);
902 /* Link the Object and Extension */
903 DeviceObjectExtension
->DeviceObject
= CreatedDeviceObject
;
904 CreatedDeviceObject
->DeviceObjectExtension
= DeviceObjectExtension
;
906 /* Set Device Object Data */
907 CreatedDeviceObject
->DeviceType
= DeviceType
;
908 CreatedDeviceObject
->Characteristics
= DeviceCharacteristics
;
909 CreatedDeviceObject
->DeviceExtension
= DeviceExtensionSize
?
910 CreatedDeviceObject
+ 1 :
912 CreatedDeviceObject
->StackSize
= 1;
913 CreatedDeviceObject
->AlignmentRequirement
= 0;
916 CreatedDeviceObject
->Flags
= DO_DEVICE_INITIALIZING
;
917 if (Exclusive
) CreatedDeviceObject
->Flags
|= DO_EXCLUSIVE
;
918 if (DeviceName
) CreatedDeviceObject
->Flags
|= DO_DEVICE_HAS_NAME
;
920 /* Attach a Vpb for Disks and Tapes, and create the Device Lock */
921 if ((CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK
) ||
922 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_VIRTUAL_DISK
) ||
923 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM
) ||
924 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE
))
927 Status
= IopCreateVpb(CreatedDeviceObject
);
928 if (!NT_SUCCESS(Status
))
930 /* Dereference the device object and fail */
931 ObDereferenceObject(CreatedDeviceObject
);
935 /* Initialize Lock Event */
936 KeInitializeEvent(&CreatedDeviceObject
->DeviceLock
,
937 SynchronizationEvent
,
941 /* Set the right Sector Size */
944 /* All disk systems */
945 case FILE_DEVICE_DISK_FILE_SYSTEM
:
946 case FILE_DEVICE_DISK
:
947 case FILE_DEVICE_VIRTUAL_DISK
:
949 /* The default is 512 bytes */
950 CreatedDeviceObject
->SectorSize
= 512;
953 /* CD-ROM file systems */
954 case FILE_DEVICE_CD_ROM_FILE_SYSTEM
:
956 /* The default is 2048 bytes */
957 CreatedDeviceObject
->SectorSize
= 2048;
960 /* Create the Device Queue */
961 if ((CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK_FILE_SYSTEM
) ||
962 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_FILE_SYSTEM
) ||
963 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM_FILE_SYSTEM
) ||
964 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_NETWORK_FILE_SYSTEM
) ||
965 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE_FILE_SYSTEM
))
967 /* Simple FS Devices, they don't need a real Device Queue */
968 InitializeListHead(&CreatedDeviceObject
->Queue
.ListEntry
);
972 /* An actual Device, initialize its DQ */
973 KeInitializeDeviceQueue(&CreatedDeviceObject
->DeviceQueue
);
976 /* Insert the Object */
977 Status
= ObInsertObject(CreatedDeviceObject
,
979 FILE_READ_DATA
| FILE_WRITE_DATA
,
981 (PVOID
*)&CreatedDeviceObject
,
983 if (!NT_SUCCESS(Status
)) return Status
;
985 /* Now do the final linking */
986 ObReferenceObject(DriverObject
);
987 ASSERT((DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
) == 0);
988 CreatedDeviceObject
->DriverObject
= DriverObject
;
989 IopEditDeviceList(DriverObject
, CreatedDeviceObject
, IopAdd
);
991 /* Link with the power manager */
992 if (CreatedDeviceObject
->Vpb
) PoVolumeDevice(CreatedDeviceObject
);
994 /* Close the temporary handle and return to caller */
995 ObCloseHandle(TempHandle
, KernelMode
);
996 *DeviceObject
= CreatedDeviceObject
;
997 return STATUS_SUCCESS
;
1008 IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject
)
1012 /* Check if the device is registered for shutdown notifications */
1013 if (DeviceObject
->Flags
& DO_SHUTDOWN_REGISTERED
)
1015 /* Call the shutdown notifications */
1016 IoUnregisterShutdownNotification(DeviceObject
);
1019 /* Check if it has a timer */
1020 Timer
= DeviceObject
->Timer
;
1023 /* Remove it and free it */
1024 IopRemoveTimerFromTimerList(Timer
);
1025 ExFreePoolWithTag(Timer
, TAG_IO_TIMER
);
1028 /* Check if the device has a name */
1029 if (DeviceObject
->Flags
& DO_DEVICE_HAS_NAME
)
1031 /* It does, make it temporary so we can remove it */
1032 ObMakeTemporaryObject(DeviceObject
);
1035 /* Set the pending delete flag */
1036 IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
|= DOE_DELETE_PENDING
;
1038 /* Check if the device object can be unloaded */
1039 if (!DeviceObject
->ReferenceCount
) IopUnloadDevice(DeviceObject
);
1050 IoDetachDevice(IN PDEVICE_OBJECT TargetDevice
)
1052 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1055 DeviceExtension
= IoGetDevObjExtension(TargetDevice
->AttachedDevice
);
1056 ASSERT(DeviceExtension
->AttachedTo
== TargetDevice
);
1058 /* Remove the attachment */
1059 DeviceExtension
->AttachedTo
= NULL
;
1060 TargetDevice
->AttachedDevice
= NULL
;
1062 /* Check if it's ok to delete this device */
1063 if ((IoGetDevObjExtension(TargetDevice
)->ExtensionFlags
& DOE_DELETE_PENDING
) &&
1064 !(TargetDevice
->ReferenceCount
))
1067 IopUnloadDevice(TargetDevice
);
1076 IoEnumerateDeviceObjectList(IN PDRIVER_OBJECT DriverObject
,
1077 IN PDEVICE_OBJECT
*DeviceObjectList
,
1078 IN ULONG DeviceObjectListSize
,
1079 OUT PULONG ActualNumberDeviceObjects
)
1081 ULONG ActualDevices
= 1;
1082 PDEVICE_OBJECT CurrentDevice
= DriverObject
->DeviceObject
;
1084 /* Find out how many devices we'll enumerate */
1085 while ((CurrentDevice
= CurrentDevice
->NextDevice
)) ActualDevices
++;
1087 /* Go back to the first */
1088 CurrentDevice
= DriverObject
->DeviceObject
;
1090 /* Start by at least returning this */
1091 *ActualNumberDeviceObjects
= ActualDevices
;
1093 /* Check if we can support so many */
1094 if ((ActualDevices
* 4) > DeviceObjectListSize
)
1096 /* Fail because the buffer was too small */
1097 return STATUS_BUFFER_TOO_SMALL
;
1100 /* Check if the caller only wanted the size */
1101 if (DeviceObjectList
)
1103 /* Loop through all the devices */
1104 while (ActualDevices
)
1106 /* Reference each Device */
1107 ObReferenceObject(CurrentDevice
);
1109 /* Add it to the list */
1110 *DeviceObjectList
= CurrentDevice
;
1112 /* Go to the next one */
1113 CurrentDevice
= CurrentDevice
->NextDevice
;
1119 /* Return the status */
1120 return STATUS_SUCCESS
;
1124 * IoGetAttachedDevice
1131 IoGetAttachedDevice(PDEVICE_OBJECT DeviceObject
)
1133 /* Get the last attached device */
1134 while (DeviceObject
->AttachedDevice
)
1136 /* Move to the next one */
1137 DeviceObject
= DeviceObject
->AttachedDevice
;
1141 return DeviceObject
;
1145 * IoGetAttachedDeviceReference
1152 IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject
)
1154 /* Reference the Attached Device */
1155 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1156 ObReferenceObject(DeviceObject
);
1157 return DeviceObject
;
1165 IoGetDeviceAttachmentBaseRef(IN PDEVICE_OBJECT DeviceObject
)
1167 /* Reference the lowest attached device */
1168 DeviceObject
= IopGetLowestDevice(DeviceObject
);
1169 ObReferenceObject(DeviceObject
);
1170 return DeviceObject
;
1174 * IoGetDeviceObjectPointer
1181 IoGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName
,
1182 IN ACCESS_MASK DesiredAccess
,
1183 OUT PFILE_OBJECT
*FileObject
,
1184 OUT PDEVICE_OBJECT
*DeviceObject
)
1186 /* Call the helper routine for a normal operation */
1187 return IopGetDeviceObjectPointer(ObjectName
,
1199 IoGetDiskDeviceObject(IN PDEVICE_OBJECT FileSystemDeviceObject
,
1200 OUT PDEVICE_OBJECT
*DiskDeviceObject
)
1202 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1207 /* Make sure there's a VPB */
1208 if (!FileSystemDeviceObject
->Vpb
) return STATUS_INVALID_PARAMETER
;
1211 IoAcquireVpbSpinLock(&OldIrql
);
1213 /* Get the Device Extension */
1214 DeviceExtension
= IoGetDevObjExtension(FileSystemDeviceObject
);
1216 /* Make sure this one has a VPB too */
1217 Vpb
= DeviceExtension
->Vpb
;
1220 /* Make sure that it's mounted */
1221 if ((Vpb
->ReferenceCount
) &&
1222 (Vpb
->Flags
& VPB_MOUNTED
))
1224 /* Return the Disk Device Object */
1225 *DiskDeviceObject
= Vpb
->RealDevice
;
1227 /* Reference it and return success */
1228 ObReferenceObject(Vpb
->RealDevice
);
1229 Status
= STATUS_SUCCESS
;
1233 /* It's not, so return failure */
1234 Status
= STATUS_VOLUME_DISMOUNTED
;
1240 Status
= STATUS_INVALID_PARAMETER
;
1243 /* Release the lock */
1244 IoReleaseVpbSpinLock(OldIrql
);
1253 IoGetLowerDeviceObject(IN PDEVICE_OBJECT DeviceObject
)
1255 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1256 PDEVICE_OBJECT LowerDeviceObject
= NULL
;
1258 /* Make sure it's not getting deleted */
1259 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1260 if (!(DeviceExtension
->ExtensionFlags
& (DOE_UNLOAD_PENDING
|
1261 DOE_DELETE_PENDING
|
1262 DOE_REMOVE_PENDING
|
1263 DOE_REMOVE_PROCESSED
)))
1265 /* Get the Lower Device Object */
1266 LowerDeviceObject
= DeviceExtension
->AttachedTo
;
1268 /* Check that we got a valid device object */
1269 if (LowerDeviceObject
)
1271 /* We did so let's reference it */
1272 ObReferenceObject(LowerDeviceObject
);
1277 return LowerDeviceObject
;
1285 IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject
)
1287 PDEVICE_OBJECT DeviceObject
= FileObject
->DeviceObject
;
1289 /* Check if we have a VPB with a device object */
1290 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1292 /* Then use the DO from the VPB */
1293 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1294 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1296 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1297 (FileObject
->DeviceObject
->Vpb
) &&
1298 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1300 /* The disk device actually has a VPB, so get the DO from there */
1301 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1305 /* Otherwise, this was a direct open */
1306 DeviceObject
= FileObject
->DeviceObject
;
1310 ASSERT(DeviceObject
!= NULL
);
1312 /* Check if we were attached */
1313 if (DeviceObject
->AttachedDevice
)
1315 /* Check if the file object has an extension present */
1316 if (FileObject
->Flags
& FO_FILE_OBJECT_HAS_EXTENSION
)
1318 /* Sanity check, direct open files can't have this */
1319 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1321 /* Check if the extension is really present */
1322 if (FileObject
->FileObjectExtension
)
1324 /* FIXME: Unhandled yet */
1325 DPRINT1("FOEs not supported\n");
1330 /* Return the highest attached device */
1331 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1334 /* Return the DO we found */
1335 return DeviceObject
;
1343 IoGetRelatedTargetDevice(IN PFILE_OBJECT FileObject
,
1344 OUT PDEVICE_OBJECT
*DeviceObject
)
1347 PDEVICE_NODE DeviceNode
= NULL
;
1349 /* Call the internal helper function */
1350 Status
= IopGetRelatedTargetDevice(FileObject
, &DeviceNode
);
1351 if (NT_SUCCESS(Status
) && DeviceNode
)
1353 *DeviceObject
= DeviceNode
->PhysicalDeviceObject
;
1363 IoGetBaseFileSystemDeviceObject(IN PFILE_OBJECT FileObject
)
1365 PDEVICE_OBJECT DeviceObject
;
1368 * If the FILE_OBJECT's VPB is defined,
1369 * get the device from it.
1371 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1373 /* Use the VPB's Device Object's */
1374 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1376 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1377 (FileObject
->DeviceObject
->Vpb
) &&
1378 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1380 /* Use the VPB's File System Object */
1381 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1385 /* Use the FO's Device Object */
1386 DeviceObject
= FileObject
->DeviceObject
;
1389 /* Return the device object we found */
1390 ASSERT(DeviceObject
!= NULL
);
1391 return DeviceObject
;
1399 IoRegisterLastChanceShutdownNotification(IN PDEVICE_OBJECT DeviceObject
)
1401 PSHUTDOWN_ENTRY Entry
;
1403 /* Allocate the shutdown entry */
1404 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1405 sizeof(SHUTDOWN_ENTRY
),
1406 TAG_SHUTDOWN_ENTRY
);
1407 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1410 Entry
->DeviceObject
= DeviceObject
;
1412 /* Reference it so it doesn't go away */
1413 ObReferenceObject(DeviceObject
);
1415 /* Insert it into the list */
1416 ExInterlockedInsertHeadList(&LastChanceShutdownListHead
,
1417 &Entry
->ShutdownList
,
1420 /* Set the shutdown registered flag */
1421 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1422 return STATUS_SUCCESS
;
1430 IoRegisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1432 PSHUTDOWN_ENTRY Entry
;
1434 /* Allocate the shutdown entry */
1435 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1436 sizeof(SHUTDOWN_ENTRY
),
1437 TAG_SHUTDOWN_ENTRY
);
1438 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1441 Entry
->DeviceObject
= DeviceObject
;
1443 /* Reference it so it doesn't go away */
1444 ObReferenceObject(DeviceObject
);
1446 /* Insert it into the list */
1447 ExInterlockedInsertHeadList(&ShutdownListHead
,
1448 &Entry
->ShutdownList
,
1451 /* Set the shutdown registered flag */
1452 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1453 return STATUS_SUCCESS
;
1461 IoUnregisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1463 PSHUTDOWN_ENTRY ShutdownEntry
;
1464 PLIST_ENTRY NextEntry
;
1467 /* Remove the flag */
1468 DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
1470 /* Acquire the shutdown lock and loop the shutdown list */
1471 KeAcquireSpinLock(&ShutdownListLock
, &OldIrql
);
1472 NextEntry
= ShutdownListHead
.Flink
;
1473 while (NextEntry
!= &ShutdownListHead
)
1476 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1480 /* Get if the DO matches */
1481 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1483 /* Remove it from the list */
1484 RemoveEntryList(NextEntry
);
1485 NextEntry
= NextEntry
->Blink
;
1487 /* Free the entry */
1488 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
1490 /* Get rid of our reference to it */
1491 ObDereferenceObject(DeviceObject
);
1494 /* Go to the next entry */
1495 NextEntry
= NextEntry
->Flink
;
1498 /* Now loop the last chance list */
1499 NextEntry
= LastChanceShutdownListHead
.Flink
;
1500 while (NextEntry
!= &LastChanceShutdownListHead
)
1503 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1507 /* Get if the DO matches */
1508 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1510 /* Remove it from the list */
1511 RemoveEntryList(NextEntry
);
1512 NextEntry
= NextEntry
->Blink
;
1514 /* Free the entry */
1515 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
1517 /* Get rid of our reference to it */
1518 ObDereferenceObject(DeviceObject
);
1521 /* Go to the next entry */
1522 NextEntry
= NextEntry
->Flink
;
1525 /* Release the shutdown lock */
1526 KeReleaseSpinLock(&ShutdownListLock
, OldIrql
);
1534 IoSetStartIoAttributes(IN PDEVICE_OBJECT DeviceObject
,
1535 IN BOOLEAN DeferredStartIo
,
1536 IN BOOLEAN NonCancelable
)
1538 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1540 /* Get the Device Extension */
1541 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1543 /* Set the flags the caller requested */
1544 DeviceExtension
->StartIoFlags
|= (DeferredStartIo
) ? DOE_SIO_DEFERRED
: 0;
1545 DeviceExtension
->StartIoFlags
|= (NonCancelable
) ? DOE_SIO_NO_CANCEL
: 0;
1553 IoStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
1554 IN BOOLEAN Cancelable
,
1557 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1559 /* Get the Device Extension */
1560 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1562 /* Check if deferred start was requested */
1563 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1565 /* Call our internal function to handle the defered case */
1566 IopStartNextPacketByKeyEx(DeviceObject
,
1569 (Cancelable
? DOE_SIO_CANCELABLE
: 0));
1573 /* Call the normal routine */
1574 IopStartNextPacketByKey(DeviceObject
, Cancelable
, Key
);
1583 IoStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
1584 IN BOOLEAN Cancelable
)
1586 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1588 /* Get the Device Extension */
1589 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1591 /* Check if deferred start was requested */
1592 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1594 /* Call our internal function to handle the defered case */
1595 IopStartNextPacketByKeyEx(DeviceObject
,
1598 (Cancelable
? DOE_SIO_CANCELABLE
: 0));
1602 /* Call the normal routine */
1603 IopStartNextPacket(DeviceObject
, Cancelable
);
1612 IoStartPacket(IN PDEVICE_OBJECT DeviceObject
,
1615 IN PDRIVER_CANCEL CancelFunction
)
1618 KIRQL OldIrql
, CancelIrql
;
1620 /* Raise to dispatch level */
1621 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1623 /* Check if we should acquire the cancel lock */
1626 /* Acquire and set it */
1627 IoAcquireCancelSpinLock(&CancelIrql
);
1628 Irp
->CancelRoutine
= CancelFunction
;
1631 /* Check if we have a key */
1635 Stat
= KeInsertByKeyDeviceQueue(&DeviceObject
->DeviceQueue
,
1636 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
1641 /* Insert without a key */
1642 Stat
= KeInsertDeviceQueue(&DeviceObject
->DeviceQueue
,
1643 &Irp
->Tail
.Overlay
.DeviceQueueEntry
);
1646 /* Check if this was a first insert */
1650 DeviceObject
->CurrentIrp
= Irp
;
1652 /* Check if this is a cancelable packet */
1655 /* Check if the caller requested no cancellation */
1656 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
1659 /* He did, so remove the cancel routine */
1660 Irp
->CancelRoutine
= NULL
;
1663 /* Release the cancel lock */
1664 IoReleaseCancelSpinLock(CancelIrql
);
1667 /* Call the Start I/O function */
1668 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
1672 /* The packet was inserted... check if we have a cancel function */
1675 /* Check if the IRP got cancelled */
1679 * Set the cancel IRQL, clear the currnet cancel routine and
1682 Irp
->CancelIrql
= CancelIrql
;
1683 Irp
->CancelRoutine
= NULL
;
1684 CancelFunction(DeviceObject
, Irp
);
1688 /* Otherwise, release the lock */
1689 IoReleaseCancelSpinLock(CancelIrql
);
1694 /* Return back to previous IRQL */
1695 KeLowerIrql(OldIrql
);
1698 #if defined (_WIN64)
1701 IoWMIDeviceObjectToProviderId(
1702 IN PDEVICE_OBJECT DeviceObject
)