2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/iomgr/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
;
338 /* Lock the Device list while we edit it */
339 OldIrql
= KeAcquireQueuedSpinLock(LockQueueIoDatabaseLock
);
341 /* Check the type of operation */
342 if (Type
== IopRemove
)
344 /* Get the current device and check if it's the current one */
345 Previous
= DeviceObject
->DriverObject
->DeviceObject
;
346 if (Previous
== DeviceObject
)
348 /* It is, simply unlink this one directly */
349 DeviceObject
->DriverObject
->DeviceObject
=
350 DeviceObject
->NextDevice
;
354 /* It's not, so loop until we find the device */
355 while (Previous
->NextDevice
!= DeviceObject
)
357 /* Not this one, keep moving */
358 if (!Previous
->NextDevice
)
360 DPRINT1("Failed to remove PDO %p (not found)\n",
364 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock
, OldIrql
);
367 Previous
= Previous
->NextDevice
;
370 /* We found it, now unlink us */
371 Previous
->NextDevice
= DeviceObject
->NextDevice
;
376 /* Link the device object and the driver object */
377 DeviceObject
->NextDevice
= DriverObject
->DeviceObject
;
378 DriverObject
->DeviceObject
= DeviceObject
;
381 /* Release the device list lock */
382 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock
, OldIrql
);
387 IopUnloadDevice(IN PDEVICE_OBJECT DeviceObject
)
389 PDRIVER_OBJECT DriverObject
= DeviceObject
->DriverObject
;
390 PEXTENDED_DEVOBJ_EXTENSION ThisExtension
= IoGetDevObjExtension(DeviceObject
);
392 /* Check if deletion is pending */
393 if (ThisExtension
->ExtensionFlags
& DOE_DELETE_PENDING
)
395 if (DeviceObject
->AttachedDevice
)
397 DPRINT("Device object is in the middle of a device stack\n");
401 if (DeviceObject
->ReferenceCount
)
403 DPRINT("Device object still has %d references\n", DeviceObject
->ReferenceCount
);
407 /* Check if we have a Security Descriptor */
408 if (DeviceObject
->SecurityDescriptor
)
411 ObDereferenceSecurityDescriptor(DeviceObject
->SecurityDescriptor
, 1);
414 /* Remove the device from the list */
415 IopEditDeviceList(DeviceObject
->DriverObject
, DeviceObject
, IopRemove
);
417 /* Dereference the keep-alive */
418 ObDereferenceObject(DeviceObject
);
421 /* We can't unload a non-PnP driver here */
422 if (DriverObject
->Flags
& DRVO_LEGACY_DRIVER
)
424 DPRINT("Not a PnP driver! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
428 /* Return if we've already called unload (maybe we're in it?) */
429 if (DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
) return;
431 /* We can't unload unless there's an unload handler */
432 if (!DriverObject
->DriverUnload
)
434 DPRINT1("No DriverUnload function on PnP driver! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
438 /* Bail if there are still devices present */
439 if (DriverObject
->DeviceObject
)
441 DPRINT("Devices still present! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
445 DPRINT1("Unloading driver '%wZ' (automatic)\n", &DriverObject
->DriverName
);
447 /* Set the unload invoked flag */
448 DriverObject
->Flags
|= DRVO_UNLOAD_INVOKED
;
451 DriverObject
->DriverUnload(DriverObject
);
453 /* Make object temporary so it can be deleted */
454 ObMakeTemporaryObject(DriverObject
);
459 IopDereferenceDeviceObject(IN PDEVICE_OBJECT DeviceObject
,
460 IN BOOLEAN ForceUnload
)
463 ASSERT(DeviceObject
->ReferenceCount
);
465 /* Dereference the device */
466 InterlockedDecrement(&DeviceObject
->ReferenceCount
);
469 * Check if we can unload it and it's safe to unload (or if we're forcing
470 * an unload, which is OK too).
472 ASSERT(!ForceUnload
);
473 if (!(DeviceObject
->ReferenceCount
) &&
474 (IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
& DOE_DELETE_PENDING
))
477 IopUnloadDevice(DeviceObject
);
483 IopStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
484 IN BOOLEAN Cancelable
,
487 PKDEVICE_QUEUE_ENTRY Entry
;
491 /* Acquire the cancel lock if this is cancelable */
492 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
494 /* Clear the current IRP */
495 DeviceObject
->CurrentIrp
= NULL
;
497 /* Remove an entry from the queue */
498 Entry
= KeRemoveByKeyDeviceQueue(&DeviceObject
->DeviceQueue
, Key
);
501 /* Get the IRP and set it */
502 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
503 DeviceObject
->CurrentIrp
= Irp
;
505 /* Check if this is a cancelable packet */
508 /* Check if the caller requested no cancellation */
509 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
512 /* He did, so remove the cancel routine */
513 Irp
->CancelRoutine
= NULL
;
516 /* Release the cancel lock */
517 IoReleaseCancelSpinLock(OldIrql
);
520 /* Call the Start I/O Routine */
521 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
525 /* Otherwise, release the cancel lock if we had acquired it */
526 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
532 IopStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
533 IN BOOLEAN Cancelable
)
535 PKDEVICE_QUEUE_ENTRY Entry
;
539 /* Acquire the cancel lock if this is cancelable */
540 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
542 /* Clear the current IRP */
543 DeviceObject
->CurrentIrp
= NULL
;
545 /* Remove an entry from the queue */
546 Entry
= KeRemoveDeviceQueue(&DeviceObject
->DeviceQueue
);
549 /* Get the IRP and set it */
550 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
551 DeviceObject
->CurrentIrp
= Irp
;
553 /* Check if this is a cancelable packet */
556 /* Check if the caller requested no cancellation */
557 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
560 /* He did, so remove the cancel routine */
561 Irp
->CancelRoutine
= NULL
;
564 /* Release the cancel lock */
565 IoReleaseCancelSpinLock(OldIrql
);
568 /* Call the Start I/O Routine */
569 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
573 /* Otherwise, release the cancel lock if we had acquired it */
574 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
580 IopStartNextPacketByKeyEx(IN PDEVICE_OBJECT DeviceObject
,
584 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
585 ULONG CurrentKey
= Key
;
586 ULONG CurrentFlags
= Flags
;
588 /* Get the device extension and start the packet loop */
589 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
592 /* Increase the count */
593 if (InterlockedIncrement(&DeviceExtension
->StartIoCount
) > 1)
596 * We've already called the routine once...
597 * All we have to do is save the key and add the new flags
599 DeviceExtension
->StartIoFlags
|= CurrentFlags
;
600 DeviceExtension
->StartIoKey
= CurrentKey
;
604 /* Mask out the current packet flags and key */
605 DeviceExtension
->StartIoFlags
&= ~(DOE_SIO_WITH_KEY
|
608 DeviceExtension
->StartIoKey
= 0;
610 /* Check if this is a packet start with key */
611 if (Flags
& DOE_SIO_WITH_KEY
)
613 /* Start the packet with a key */
614 IopStartNextPacketByKey(DeviceObject
,
615 (Flags
& DOE_SIO_CANCELABLE
) ?
619 else if (Flags
& DOE_SIO_NO_KEY
)
621 /* Start the packet */
622 IopStartNextPacket(DeviceObject
,
623 (Flags
& DOE_SIO_CANCELABLE
) ?
628 /* Decrease the Start I/O count and check if it's 0 now */
629 if (!InterlockedDecrement(&DeviceExtension
->StartIoCount
))
631 /* Get the current active key and flags */
632 CurrentKey
= DeviceExtension
->StartIoKey
;
633 CurrentFlags
= DeviceExtension
->StartIoFlags
& (DOE_SIO_WITH_KEY
|
637 /* Check if we should still loop */
638 if (!(CurrentFlags
& (DOE_SIO_WITH_KEY
| DOE_SIO_NO_KEY
))) break;
642 /* There are still Start I/Os active, so quit this loop */
650 IopGetRelatedTargetDevice(IN PFILE_OBJECT FileObject
,
651 OUT PDEVICE_NODE
*DeviceNode
)
654 IO_STACK_LOCATION Stack
= {0};
655 PDEVICE_RELATIONS DeviceRelations
;
656 PDEVICE_OBJECT DeviceObject
= NULL
;
660 /* Get DeviceObject related to given FileObject */
661 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
662 if (!DeviceObject
) return STATUS_NO_SUCH_DEVICE
;
664 /* Define input parameters */
665 Stack
.MajorFunction
= IRP_MJ_PNP
;
666 Stack
.MinorFunction
= IRP_MN_QUERY_DEVICE_RELATIONS
;
667 Stack
.Parameters
.QueryDeviceRelations
.Type
= TargetDeviceRelation
;
668 Stack
.FileObject
= FileObject
;
670 /* Call the driver to query all relations (IRP_MJ_PNP) */
671 Status
= IopSynchronousCall(DeviceObject
,
673 (PVOID
)&DeviceRelations
);
674 if (!NT_SUCCESS(Status
)) return Status
;
676 /* Make sure it's not NULL and contains only one object */
677 ASSERT(DeviceRelations
);
678 ASSERT(DeviceRelations
->Count
== 1);
680 /* Finally get the device node */
681 *DeviceNode
= IopGetDeviceNode(DeviceRelations
->Objects
[0]);
682 if (!*DeviceNode
) Status
= STATUS_NO_SUCH_DEVICE
;
684 /* Free the DEVICE_RELATIONS structure, it's not needed anymore */
685 ExFreePool(DeviceRelations
);
690 /* PUBLIC FUNCTIONS ***********************************************************/
695 * Layers a device over the highest device in a device stack.
699 * Device to be attached.
702 * Name of the target device.
705 * Caller storage for the device attached to.
712 IoAttachDevice(PDEVICE_OBJECT SourceDevice
,
713 PUNICODE_STRING TargetDeviceName
,
714 PDEVICE_OBJECT
*AttachedDevice
)
717 PFILE_OBJECT FileObject
= NULL
;
718 PDEVICE_OBJECT TargetDevice
= NULL
;
720 /* Call the helper routine for an attach operation */
721 Status
= IopGetDeviceObjectPointer(TargetDeviceName
,
722 FILE_READ_ATTRIBUTES
,
725 IO_ATTACH_DEVICE_API
);
726 if (!NT_SUCCESS(Status
)) return Status
;
728 /* Attach the device */
729 Status
= IoAttachDeviceToDeviceStackSafe(SourceDevice
,
734 ObDereferenceObject(FileObject
);
739 * IoAttachDeviceByPointer
746 IoAttachDeviceByPointer(IN PDEVICE_OBJECT SourceDevice
,
747 IN PDEVICE_OBJECT TargetDevice
)
749 PDEVICE_OBJECT AttachedDevice
;
750 NTSTATUS Status
= STATUS_SUCCESS
;
753 AttachedDevice
= IoAttachDeviceToDeviceStack(SourceDevice
, TargetDevice
);
754 if (!AttachedDevice
) Status
= STATUS_NO_SUCH_DEVICE
;
756 /* Return the status */
765 IoAttachDeviceToDeviceStack(IN PDEVICE_OBJECT SourceDevice
,
766 IN PDEVICE_OBJECT TargetDevice
)
768 /* Attach it safely */
769 return IopAttachDeviceToDeviceStackSafe(SourceDevice
,
779 IoAttachDeviceToDeviceStackSafe(IN PDEVICE_OBJECT SourceDevice
,
780 IN PDEVICE_OBJECT TargetDevice
,
781 IN OUT PDEVICE_OBJECT
*AttachedToDeviceObject
)
783 /* Call the internal function */
784 if (!IopAttachDeviceToDeviceStackSafe(SourceDevice
,
786 AttachedToDeviceObject
))
789 return STATUS_NO_SUCH_DEVICE
;
793 return STATUS_SUCCESS
;
799 * Allocates memory for and intializes a device object for use for
804 * Driver object passed by IO Manager when the driver was loaded.
806 * DeviceExtensionSize
807 * Number of bytes for the device extension.
810 * Unicode name of device.
813 * Device type of the new device.
815 * DeviceCharacteristics
816 * Bit mask of device characteristics.
819 * TRUE if only one thread can access the device at a time.
822 * On successful return this parameter is filled by pointer to
823 * allocated device object.
830 IoCreateDevice(IN PDRIVER_OBJECT DriverObject
,
831 IN ULONG DeviceExtensionSize
,
832 IN PUNICODE_STRING DeviceName
,
833 IN DEVICE_TYPE DeviceType
,
834 IN ULONG DeviceCharacteristics
,
835 IN BOOLEAN Exclusive
,
836 OUT PDEVICE_OBJECT
*DeviceObject
)
838 WCHAR AutoNameBuffer
[20];
839 UNICODE_STRING AutoName
;
840 PDEVICE_OBJECT CreatedDeviceObject
;
841 PDEVOBJ_EXTENSION DeviceObjectExtension
;
842 OBJECT_ATTRIBUTES ObjectAttributes
;
844 ULONG AlignedDeviceExtensionSize
;
849 /* Check if we have to generate a name */
850 if (DeviceCharacteristics
& FILE_AUTOGENERATED_DEVICE_NAME
)
853 swprintf(AutoNameBuffer
,
855 InterlockedIncrementUL(&IopDeviceObjectNumber
));
857 /* Initialize the name */
858 RtlInitUnicodeString(&AutoName
, AutoNameBuffer
);
859 DeviceName
= &AutoName
;
862 /* Initialize the Object Attributes */
863 InitializeObjectAttributes(&ObjectAttributes
,
867 SePublicOpenUnrestrictedSd
);
869 /* Honor exclusive flag */
870 if (Exclusive
) ObjectAttributes
.Attributes
|= OBJ_EXCLUSIVE
;
872 /* Create a permanent object for named devices */
873 if (DeviceName
) ObjectAttributes
.Attributes
|= OBJ_PERMANENT
;
875 /* Align the Extension Size to 8-bytes */
876 AlignedDeviceExtensionSize
= (DeviceExtensionSize
+ 7) &~ 7;
879 TotalSize
= AlignedDeviceExtensionSize
+
880 sizeof(DEVICE_OBJECT
) +
881 sizeof(EXTENDED_DEVOBJ_EXTENSION
);
883 /* Create the Device Object */
884 *DeviceObject
= NULL
;
885 Status
= ObCreateObject(KernelMode
,
893 (PVOID
*)&CreatedDeviceObject
);
894 if (!NT_SUCCESS(Status
)) return Status
;
896 /* Clear the whole Object and extension so we don't null stuff manually */
897 RtlZeroMemory(CreatedDeviceObject
, TotalSize
);
900 * Setup the Type and Size. Note that we don't use the aligned size,
901 * because that's only padding for the DevObjExt and not part of the Object.
903 CreatedDeviceObject
->Type
= IO_TYPE_DEVICE
;
904 CreatedDeviceObject
->Size
= sizeof(DEVICE_OBJECT
) + (USHORT
)DeviceExtensionSize
;
906 /* The kernel extension is after the driver internal extension */
907 DeviceObjectExtension
= (PDEVOBJ_EXTENSION
)
908 ((ULONG_PTR
)(CreatedDeviceObject
+ 1) +
909 AlignedDeviceExtensionSize
);
911 /* Set the Type and Size. Question: why is Size 0 on Windows? */
912 DeviceObjectExtension
->Type
= IO_TYPE_DEVICE_OBJECT_EXTENSION
;
913 DeviceObjectExtension
->Size
= 0;
915 /* Initialize with Power Manager */
916 PoInitializeDeviceObject(DeviceObjectExtension
);
918 /* Link the Object and Extension */
919 DeviceObjectExtension
->DeviceObject
= CreatedDeviceObject
;
920 CreatedDeviceObject
->DeviceObjectExtension
= DeviceObjectExtension
;
922 /* Set Device Object Data */
923 CreatedDeviceObject
->DeviceType
= DeviceType
;
924 CreatedDeviceObject
->Characteristics
= DeviceCharacteristics
;
925 CreatedDeviceObject
->DeviceExtension
= DeviceExtensionSize
?
926 CreatedDeviceObject
+ 1 :
928 CreatedDeviceObject
->StackSize
= 1;
929 CreatedDeviceObject
->AlignmentRequirement
= 0;
932 CreatedDeviceObject
->Flags
= DO_DEVICE_INITIALIZING
;
933 if (Exclusive
) CreatedDeviceObject
->Flags
|= DO_EXCLUSIVE
;
934 if (DeviceName
) CreatedDeviceObject
->Flags
|= DO_DEVICE_HAS_NAME
;
936 /* Attach a Vpb for Disks and Tapes, and create the Device Lock */
937 if ((CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK
) ||
938 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_VIRTUAL_DISK
) ||
939 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM
) ||
940 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE
))
943 Status
= IopCreateVpb(CreatedDeviceObject
);
944 if (!NT_SUCCESS(Status
))
946 /* Dereference the device object and fail */
947 ObDereferenceObject(CreatedDeviceObject
);
951 /* Initialize Lock Event */
952 KeInitializeEvent(&CreatedDeviceObject
->DeviceLock
,
953 SynchronizationEvent
,
957 /* Set the right Sector Size */
960 /* All disk systems */
961 case FILE_DEVICE_DISK_FILE_SYSTEM
:
962 case FILE_DEVICE_DISK
:
963 case FILE_DEVICE_VIRTUAL_DISK
:
965 /* The default is 512 bytes */
966 CreatedDeviceObject
->SectorSize
= 512;
969 /* CD-ROM file systems */
970 case FILE_DEVICE_CD_ROM_FILE_SYSTEM
:
972 /* The default is 2048 bytes */
973 CreatedDeviceObject
->SectorSize
= 2048;
976 /* Create the Device Queue */
977 if ((CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK_FILE_SYSTEM
) ||
978 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_FILE_SYSTEM
) ||
979 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM_FILE_SYSTEM
) ||
980 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_NETWORK_FILE_SYSTEM
) ||
981 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE_FILE_SYSTEM
))
983 /* Simple FS Devices, they don't need a real Device Queue */
984 InitializeListHead(&CreatedDeviceObject
->Queue
.ListEntry
);
988 /* An actual Device, initialize its DQ */
989 KeInitializeDeviceQueue(&CreatedDeviceObject
->DeviceQueue
);
992 /* Insert the Object */
993 Status
= ObInsertObject(CreatedDeviceObject
,
995 FILE_READ_DATA
| FILE_WRITE_DATA
,
997 (PVOID
*)&CreatedDeviceObject
,
999 if (!NT_SUCCESS(Status
)) return Status
;
1001 /* Now do the final linking */
1002 ObReferenceObject(DriverObject
);
1003 ASSERT((DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
) == 0);
1004 CreatedDeviceObject
->DriverObject
= DriverObject
;
1005 IopEditDeviceList(DriverObject
, CreatedDeviceObject
, IopAdd
);
1007 /* Link with the power manager */
1008 if (CreatedDeviceObject
->Vpb
) PoVolumeDevice(CreatedDeviceObject
);
1010 /* Close the temporary handle and return to caller */
1011 ObCloseHandle(TempHandle
, KernelMode
);
1012 *DeviceObject
= CreatedDeviceObject
;
1013 return STATUS_SUCCESS
;
1024 IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject
)
1028 /* Check if the device is registered for shutdown notifications */
1029 if (DeviceObject
->Flags
& DO_SHUTDOWN_REGISTERED
)
1031 /* Call the shutdown notifications */
1032 IoUnregisterShutdownNotification(DeviceObject
);
1035 /* Check if it has a timer */
1036 Timer
= DeviceObject
->Timer
;
1039 /* Remove it and free it */
1040 IopRemoveTimerFromTimerList(Timer
);
1041 ExFreePoolWithTag(Timer
, TAG_IO_TIMER
);
1044 /* Check if the device has a name */
1045 if (DeviceObject
->Flags
& DO_DEVICE_HAS_NAME
)
1047 /* It does, make it temporary so we can remove it */
1048 ObMakeTemporaryObject(DeviceObject
);
1051 /* Set the pending delete flag */
1052 IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
|= DOE_DELETE_PENDING
;
1054 /* Unlink with the power manager */
1055 if (DeviceObject
->Vpb
) PoRemoveVolumeDevice(DeviceObject
);
1057 /* Check if the device object can be unloaded */
1058 if (!DeviceObject
->ReferenceCount
) IopUnloadDevice(DeviceObject
);
1069 IoDetachDevice(IN PDEVICE_OBJECT TargetDevice
)
1071 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1074 DeviceExtension
= IoGetDevObjExtension(TargetDevice
->AttachedDevice
);
1075 ASSERT(DeviceExtension
->AttachedTo
== TargetDevice
);
1077 /* Remove the attachment */
1078 DeviceExtension
->AttachedTo
= NULL
;
1079 TargetDevice
->AttachedDevice
= NULL
;
1081 /* Check if it's ok to delete this device */
1082 if ((IoGetDevObjExtension(TargetDevice
)->ExtensionFlags
& DOE_DELETE_PENDING
) &&
1083 !(TargetDevice
->ReferenceCount
))
1086 IopUnloadDevice(TargetDevice
);
1095 IoEnumerateDeviceObjectList(IN PDRIVER_OBJECT DriverObject
,
1096 IN PDEVICE_OBJECT
*DeviceObjectList
,
1097 IN ULONG DeviceObjectListSize
,
1098 OUT PULONG ActualNumberDeviceObjects
)
1100 ULONG ActualDevices
= 1;
1101 PDEVICE_OBJECT CurrentDevice
= DriverObject
->DeviceObject
;
1104 /* Lock the Device list while we enumerate it */
1105 OldIrql
= KeAcquireQueuedSpinLock(LockQueueIoDatabaseLock
);
1107 /* Find out how many devices we'll enumerate */
1108 while ((CurrentDevice
= CurrentDevice
->NextDevice
)) ActualDevices
++;
1110 /* Go back to the first */
1111 CurrentDevice
= DriverObject
->DeviceObject
;
1113 /* Start by at least returning this */
1114 *ActualNumberDeviceObjects
= ActualDevices
;
1116 /* Check if we can support so many */
1117 if ((ActualDevices
* sizeof(PDEVICE_OBJECT
)) > DeviceObjectListSize
)
1119 /* Fail because the buffer was too small */
1120 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock
, OldIrql
);
1121 return STATUS_BUFFER_TOO_SMALL
;
1124 /* Check if the caller wanted the device list */
1125 if (DeviceObjectList
)
1127 /* Loop through all the devices */
1128 while (ActualDevices
)
1130 /* Reference each Device */
1131 ObReferenceObject(CurrentDevice
);
1133 /* Add it to the list */
1134 *DeviceObjectList
= CurrentDevice
;
1136 /* Go to the next one */
1137 CurrentDevice
= CurrentDevice
->NextDevice
;
1143 /* Release the device list lock */
1144 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock
, OldIrql
);
1146 /* Return the status */
1147 return STATUS_SUCCESS
;
1151 * IoGetAttachedDevice
1158 IoGetAttachedDevice(PDEVICE_OBJECT DeviceObject
)
1160 /* Get the last attached device */
1161 while (DeviceObject
->AttachedDevice
)
1163 /* Move to the next one */
1164 DeviceObject
= DeviceObject
->AttachedDevice
;
1168 return DeviceObject
;
1172 * IoGetAttachedDeviceReference
1179 IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject
)
1181 /* Reference the Attached Device */
1182 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1183 ObReferenceObject(DeviceObject
);
1184 return DeviceObject
;
1192 IoGetDeviceAttachmentBaseRef(IN PDEVICE_OBJECT DeviceObject
)
1194 /* Reference the lowest attached device */
1195 DeviceObject
= IopGetLowestDevice(DeviceObject
);
1196 ObReferenceObject(DeviceObject
);
1197 return DeviceObject
;
1201 * IoGetDeviceObjectPointer
1208 IoGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName
,
1209 IN ACCESS_MASK DesiredAccess
,
1210 OUT PFILE_OBJECT
*FileObject
,
1211 OUT PDEVICE_OBJECT
*DeviceObject
)
1213 /* Call the helper routine for a normal operation */
1214 return IopGetDeviceObjectPointer(ObjectName
,
1226 IoGetDiskDeviceObject(IN PDEVICE_OBJECT FileSystemDeviceObject
,
1227 OUT PDEVICE_OBJECT
*DiskDeviceObject
)
1229 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1234 /* Make sure there's a VPB */
1235 if (!FileSystemDeviceObject
->Vpb
) return STATUS_INVALID_PARAMETER
;
1238 IoAcquireVpbSpinLock(&OldIrql
);
1240 /* Get the Device Extension */
1241 DeviceExtension
= IoGetDevObjExtension(FileSystemDeviceObject
);
1243 /* Make sure this one has a VPB too */
1244 Vpb
= DeviceExtension
->Vpb
;
1247 /* Make sure that it's mounted */
1248 if ((Vpb
->ReferenceCount
) &&
1249 (Vpb
->Flags
& VPB_MOUNTED
))
1251 /* Return the Disk Device Object */
1252 *DiskDeviceObject
= Vpb
->RealDevice
;
1254 /* Reference it and return success */
1255 ObReferenceObject(Vpb
->RealDevice
);
1256 Status
= STATUS_SUCCESS
;
1260 /* It's not, so return failure */
1261 Status
= STATUS_VOLUME_DISMOUNTED
;
1267 Status
= STATUS_INVALID_PARAMETER
;
1270 /* Release the lock */
1271 IoReleaseVpbSpinLock(OldIrql
);
1280 IoGetLowerDeviceObject(IN PDEVICE_OBJECT DeviceObject
)
1282 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1283 PDEVICE_OBJECT LowerDeviceObject
= NULL
;
1285 /* Make sure it's not getting deleted */
1286 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1287 if (!(DeviceExtension
->ExtensionFlags
& (DOE_UNLOAD_PENDING
|
1288 DOE_DELETE_PENDING
|
1289 DOE_REMOVE_PENDING
|
1290 DOE_REMOVE_PROCESSED
)))
1292 /* Get the Lower Device Object */
1293 LowerDeviceObject
= DeviceExtension
->AttachedTo
;
1295 /* Check that we got a valid device object */
1296 if (LowerDeviceObject
)
1298 /* We did so let's reference it */
1299 ObReferenceObject(LowerDeviceObject
);
1304 return LowerDeviceObject
;
1312 IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject
)
1314 PDEVICE_OBJECT DeviceObject
= FileObject
->DeviceObject
;
1316 /* Check if we have a VPB with a device object */
1317 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1319 /* Then use the DO from the VPB */
1320 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1321 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1323 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1324 (FileObject
->DeviceObject
->Vpb
) &&
1325 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1327 /* The disk device actually has a VPB, so get the DO from there */
1328 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1332 /* Otherwise, this was a direct open */
1333 DeviceObject
= FileObject
->DeviceObject
;
1337 ASSERT(DeviceObject
!= NULL
);
1339 /* Check if we were attached */
1340 if (DeviceObject
->AttachedDevice
)
1342 /* Check if the file object has an extension present */
1343 if (FileObject
->Flags
& FO_FILE_OBJECT_HAS_EXTENSION
)
1345 /* Sanity check, direct open files can't have this */
1346 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1348 /* Check if the extension is really present */
1349 if (FileObject
->FileObjectExtension
)
1351 PFILE_OBJECT_EXTENSION FileObjectExtension
;
1354 /* Cast the buffer to something we understand */
1355 FileObjectExtension
= FileObject
->FileObjectExtension
;
1357 /* Check if have a replacement top level device */
1358 if (FileObjectExtension
->TopDeviceObjectHint
)
1360 /* Use this instead of returning the top level device */
1361 return FileObjectExtension
->TopDeviceObjectHint
;
1366 /* Return the highest attached device */
1367 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1370 /* Return the DO we found */
1371 return DeviceObject
;
1379 IoGetRelatedTargetDevice(IN PFILE_OBJECT FileObject
,
1380 OUT PDEVICE_OBJECT
*DeviceObject
)
1383 PDEVICE_NODE DeviceNode
= NULL
;
1385 /* Call the internal helper function */
1386 Status
= IopGetRelatedTargetDevice(FileObject
, &DeviceNode
);
1387 if (NT_SUCCESS(Status
) && DeviceNode
)
1389 *DeviceObject
= DeviceNode
->PhysicalDeviceObject
;
1399 IoGetBaseFileSystemDeviceObject(IN PFILE_OBJECT FileObject
)
1401 PDEVICE_OBJECT DeviceObject
;
1404 * If the FILE_OBJECT's VPB is defined,
1405 * get the device from it.
1407 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1409 /* Use the VPB's Device Object's */
1410 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1412 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1413 (FileObject
->DeviceObject
->Vpb
) &&
1414 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1416 /* Use the VPB's File System Object */
1417 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1421 /* Use the FO's Device Object */
1422 DeviceObject
= FileObject
->DeviceObject
;
1425 /* Return the device object we found */
1426 ASSERT(DeviceObject
!= NULL
);
1427 return DeviceObject
;
1435 IoRegisterLastChanceShutdownNotification(IN PDEVICE_OBJECT DeviceObject
)
1437 PSHUTDOWN_ENTRY Entry
;
1439 /* Allocate the shutdown entry */
1440 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1441 sizeof(SHUTDOWN_ENTRY
),
1442 TAG_SHUTDOWN_ENTRY
);
1443 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1446 Entry
->DeviceObject
= DeviceObject
;
1448 /* Reference it so it doesn't go away */
1449 ObReferenceObject(DeviceObject
);
1451 /* Insert it into the list */
1452 ExInterlockedInsertHeadList(&LastChanceShutdownListHead
,
1453 &Entry
->ShutdownList
,
1456 /* Set the shutdown registered flag */
1457 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1458 return STATUS_SUCCESS
;
1466 IoRegisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1468 PSHUTDOWN_ENTRY Entry
;
1470 /* Allocate the shutdown entry */
1471 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1472 sizeof(SHUTDOWN_ENTRY
),
1473 TAG_SHUTDOWN_ENTRY
);
1474 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1477 Entry
->DeviceObject
= DeviceObject
;
1479 /* Reference it so it doesn't go away */
1480 ObReferenceObject(DeviceObject
);
1482 /* Insert it into the list */
1483 ExInterlockedInsertHeadList(&ShutdownListHead
,
1484 &Entry
->ShutdownList
,
1487 /* Set the shutdown registered flag */
1488 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1489 return STATUS_SUCCESS
;
1497 IoUnregisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1499 PSHUTDOWN_ENTRY ShutdownEntry
;
1500 PLIST_ENTRY NextEntry
;
1503 /* Remove the flag */
1504 DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
1506 /* Acquire the shutdown lock and loop the shutdown list */
1507 KeAcquireSpinLock(&ShutdownListLock
, &OldIrql
);
1508 NextEntry
= ShutdownListHead
.Flink
;
1509 while (NextEntry
!= &ShutdownListHead
)
1512 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1516 /* Get if the DO matches */
1517 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1519 /* Remove it from the list */
1520 RemoveEntryList(NextEntry
);
1521 NextEntry
= NextEntry
->Blink
;
1523 /* Free the entry */
1524 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
1526 /* Get rid of our reference to it */
1527 ObDereferenceObject(DeviceObject
);
1530 /* Go to the next entry */
1531 NextEntry
= NextEntry
->Flink
;
1534 /* Now loop the last chance list */
1535 NextEntry
= LastChanceShutdownListHead
.Flink
;
1536 while (NextEntry
!= &LastChanceShutdownListHead
)
1539 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1543 /* Get if the DO matches */
1544 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1546 /* Remove it from the list */
1547 RemoveEntryList(NextEntry
);
1548 NextEntry
= NextEntry
->Blink
;
1550 /* Free the entry */
1551 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
1553 /* Get rid of our reference to it */
1554 ObDereferenceObject(DeviceObject
);
1557 /* Go to the next entry */
1558 NextEntry
= NextEntry
->Flink
;
1561 /* Release the shutdown lock */
1562 KeReleaseSpinLock(&ShutdownListLock
, OldIrql
);
1570 IoSetStartIoAttributes(IN PDEVICE_OBJECT DeviceObject
,
1571 IN BOOLEAN DeferredStartIo
,
1572 IN BOOLEAN NonCancelable
)
1574 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1576 /* Get the Device Extension */
1577 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1579 /* Set the flags the caller requested */
1580 DeviceExtension
->StartIoFlags
|= (DeferredStartIo
) ? DOE_SIO_DEFERRED
: 0;
1581 DeviceExtension
->StartIoFlags
|= (NonCancelable
) ? DOE_SIO_NO_CANCEL
: 0;
1589 IoStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
1590 IN BOOLEAN Cancelable
,
1593 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1595 /* Get the Device Extension */
1596 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1598 /* Check if deferred start was requested */
1599 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1601 /* Call our internal function to handle the defered case */
1602 IopStartNextPacketByKeyEx(DeviceObject
,
1605 (Cancelable
? DOE_SIO_CANCELABLE
: 0));
1609 /* Call the normal routine */
1610 IopStartNextPacketByKey(DeviceObject
, Cancelable
, Key
);
1619 IoStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
1620 IN BOOLEAN Cancelable
)
1622 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1624 /* Get the Device Extension */
1625 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1627 /* Check if deferred start was requested */
1628 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1630 /* Call our internal function to handle the defered case */
1631 IopStartNextPacketByKeyEx(DeviceObject
,
1634 (Cancelable
? DOE_SIO_CANCELABLE
: 0));
1638 /* Call the normal routine */
1639 IopStartNextPacket(DeviceObject
, Cancelable
);
1648 IoStartPacket(IN PDEVICE_OBJECT DeviceObject
,
1651 IN PDRIVER_CANCEL CancelFunction
)
1654 KIRQL OldIrql
, CancelIrql
;
1656 /* Raise to dispatch level */
1657 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1659 /* Check if we should acquire the cancel lock */
1662 /* Acquire and set it */
1663 IoAcquireCancelSpinLock(&CancelIrql
);
1664 Irp
->CancelRoutine
= CancelFunction
;
1667 /* Check if we have a key */
1671 Stat
= KeInsertByKeyDeviceQueue(&DeviceObject
->DeviceQueue
,
1672 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
1677 /* Insert without a key */
1678 Stat
= KeInsertDeviceQueue(&DeviceObject
->DeviceQueue
,
1679 &Irp
->Tail
.Overlay
.DeviceQueueEntry
);
1682 /* Check if this was a first insert */
1686 DeviceObject
->CurrentIrp
= Irp
;
1688 /* Check if this is a cancelable packet */
1691 /* Check if the caller requested no cancellation */
1692 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
1695 /* He did, so remove the cancel routine */
1696 Irp
->CancelRoutine
= NULL
;
1699 /* Release the cancel lock */
1700 IoReleaseCancelSpinLock(CancelIrql
);
1703 /* Call the Start I/O function */
1704 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
1708 /* The packet was inserted... check if we have a cancel function */
1711 /* Check if the IRP got cancelled */
1715 * Set the cancel IRQL, clear the currnet cancel routine and
1718 Irp
->CancelIrql
= CancelIrql
;
1719 Irp
->CancelRoutine
= NULL
;
1720 CancelFunction(DeviceObject
, Irp
);
1724 /* Otherwise, release the lock */
1725 IoReleaseCancelSpinLock(CancelIrql
);
1730 /* Return back to previous IRQL */
1731 KeLowerIrql(OldIrql
);
1734 #if defined (_WIN64)
1737 IoWMIDeviceObjectToProviderId(
1738 IN PDEVICE_OBJECT DeviceObject
)