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 on driver %wZ (not found)\n",
362 &DeviceObject
->DriverObject
->DriverName
);
365 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock
, OldIrql
);
368 Previous
= Previous
->NextDevice
;
371 /* We found it, now unlink us */
372 Previous
->NextDevice
= DeviceObject
->NextDevice
;
377 /* Link the device object and the driver object */
378 DeviceObject
->NextDevice
= DriverObject
->DeviceObject
;
379 DriverObject
->DeviceObject
= DeviceObject
;
382 /* Release the device list lock */
383 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock
, OldIrql
);
388 IopUnloadDevice(IN PDEVICE_OBJECT DeviceObject
)
390 PDRIVER_OBJECT DriverObject
= DeviceObject
->DriverObject
;
391 PEXTENDED_DEVOBJ_EXTENSION ThisExtension
= IoGetDevObjExtension(DeviceObject
);
393 /* Check if deletion is pending */
394 if (ThisExtension
->ExtensionFlags
& DOE_DELETE_PENDING
)
396 if (DeviceObject
->AttachedDevice
)
398 DPRINT("Device object is in the middle of a device stack\n");
402 if (DeviceObject
->ReferenceCount
)
404 DPRINT("Device object still has %d references\n", DeviceObject
->ReferenceCount
);
408 /* Check if we have a Security Descriptor */
409 if (DeviceObject
->SecurityDescriptor
)
412 ObDereferenceSecurityDescriptor(DeviceObject
->SecurityDescriptor
, 1);
415 /* Remove the device from the list */
416 IopEditDeviceList(DeviceObject
->DriverObject
, DeviceObject
, IopRemove
);
418 /* Dereference the keep-alive */
419 ObDereferenceObject(DeviceObject
);
422 /* We can't unload a non-PnP driver here */
423 if (DriverObject
->Flags
& DRVO_LEGACY_DRIVER
)
425 DPRINT("Not a PnP driver! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
429 /* Return if we've already called unload (maybe we're in it?) */
430 if (DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
) return;
432 /* We can't unload unless there's an unload handler */
433 if (!DriverObject
->DriverUnload
)
435 DPRINT1("No DriverUnload function on PnP driver! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
439 /* Bail if there are still devices present */
440 if (DriverObject
->DeviceObject
)
442 DPRINT("Devices still present! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
446 DPRINT1("Unloading driver '%wZ' (automatic)\n", &DriverObject
->DriverName
);
448 /* Set the unload invoked flag */
449 DriverObject
->Flags
|= DRVO_UNLOAD_INVOKED
;
452 DriverObject
->DriverUnload(DriverObject
);
454 /* Make object temporary so it can be deleted */
455 ObMakeTemporaryObject(DriverObject
);
460 IopDereferenceDeviceObject(IN PDEVICE_OBJECT DeviceObject
,
461 IN BOOLEAN ForceUnload
)
464 ASSERT(DeviceObject
->ReferenceCount
);
466 /* Dereference the device */
467 InterlockedDecrement(&DeviceObject
->ReferenceCount
);
470 * Check if we can unload it and it's safe to unload (or if we're forcing
471 * an unload, which is OK too).
473 ASSERT(!ForceUnload
);
474 if (!(DeviceObject
->ReferenceCount
) &&
475 (IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
& DOE_DELETE_PENDING
))
478 IopUnloadDevice(DeviceObject
);
484 IopStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
485 IN BOOLEAN Cancelable
,
488 PKDEVICE_QUEUE_ENTRY Entry
;
492 /* Acquire the cancel lock if this is cancelable */
493 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
495 /* Clear the current IRP */
496 DeviceObject
->CurrentIrp
= NULL
;
498 /* Remove an entry from the queue */
499 Entry
= KeRemoveByKeyDeviceQueue(&DeviceObject
->DeviceQueue
, Key
);
502 /* Get the IRP and set it */
503 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
504 DeviceObject
->CurrentIrp
= Irp
;
506 /* Check if this is a cancelable packet */
509 /* Check if the caller requested no cancellation */
510 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
513 /* He did, so remove the cancel routine */
514 Irp
->CancelRoutine
= NULL
;
517 /* Release the cancel lock */
518 IoReleaseCancelSpinLock(OldIrql
);
521 /* Call the Start I/O Routine */
522 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
526 /* Otherwise, release the cancel lock if we had acquired it */
527 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
533 IopStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
534 IN BOOLEAN Cancelable
)
536 PKDEVICE_QUEUE_ENTRY Entry
;
540 /* Acquire the cancel lock if this is cancelable */
541 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
543 /* Clear the current IRP */
544 DeviceObject
->CurrentIrp
= NULL
;
546 /* Remove an entry from the queue */
547 Entry
= KeRemoveDeviceQueue(&DeviceObject
->DeviceQueue
);
550 /* Get the IRP and set it */
551 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
552 DeviceObject
->CurrentIrp
= Irp
;
554 /* Check if this is a cancelable packet */
557 /* Check if the caller requested no cancellation */
558 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
561 /* He did, so remove the cancel routine */
562 Irp
->CancelRoutine
= NULL
;
565 /* Release the cancel lock */
566 IoReleaseCancelSpinLock(OldIrql
);
569 /* Call the Start I/O Routine */
570 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
574 /* Otherwise, release the cancel lock if we had acquired it */
575 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
581 IopStartNextPacketByKeyEx(IN PDEVICE_OBJECT DeviceObject
,
585 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
586 ULONG CurrentKey
= Key
;
587 ULONG CurrentFlags
= Flags
;
589 /* Get the device extension and start the packet loop */
590 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
593 /* Increase the count */
594 if (InterlockedIncrement(&DeviceExtension
->StartIoCount
) > 1)
597 * We've already called the routine once...
598 * All we have to do is save the key and add the new flags
600 DeviceExtension
->StartIoFlags
|= CurrentFlags
;
601 DeviceExtension
->StartIoKey
= CurrentKey
;
605 /* Mask out the current packet flags and key */
606 DeviceExtension
->StartIoFlags
&= ~(DOE_SIO_WITH_KEY
|
609 DeviceExtension
->StartIoKey
= 0;
611 /* Check if this is a packet start with key */
612 if (Flags
& DOE_SIO_WITH_KEY
)
614 /* Start the packet with a key */
615 IopStartNextPacketByKey(DeviceObject
,
616 (Flags
& DOE_SIO_CANCELABLE
) ?
620 else if (Flags
& DOE_SIO_NO_KEY
)
622 /* Start the packet */
623 IopStartNextPacket(DeviceObject
,
624 (Flags
& DOE_SIO_CANCELABLE
) ?
629 /* Decrease the Start I/O count and check if it's 0 now */
630 if (!InterlockedDecrement(&DeviceExtension
->StartIoCount
))
632 /* Get the current active key and flags */
633 CurrentKey
= DeviceExtension
->StartIoKey
;
634 CurrentFlags
= DeviceExtension
->StartIoFlags
& (DOE_SIO_WITH_KEY
|
638 /* Check if we should still loop */
639 if (!(CurrentFlags
& (DOE_SIO_WITH_KEY
| DOE_SIO_NO_KEY
))) break;
643 /* There are still Start I/Os active, so quit this loop */
651 IopGetRelatedTargetDevice(IN PFILE_OBJECT FileObject
,
652 OUT PDEVICE_NODE
*DeviceNode
)
655 IO_STACK_LOCATION Stack
= {0};
656 PDEVICE_RELATIONS DeviceRelations
;
657 PDEVICE_OBJECT DeviceObject
= NULL
;
661 /* Get DeviceObject related to given FileObject */
662 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
663 if (!DeviceObject
) return STATUS_NO_SUCH_DEVICE
;
665 /* Define input parameters */
666 Stack
.MajorFunction
= IRP_MJ_PNP
;
667 Stack
.MinorFunction
= IRP_MN_QUERY_DEVICE_RELATIONS
;
668 Stack
.Parameters
.QueryDeviceRelations
.Type
= TargetDeviceRelation
;
669 Stack
.FileObject
= FileObject
;
671 /* Call the driver to query all relations (IRP_MJ_PNP) */
672 Status
= IopSynchronousCall(DeviceObject
,
674 (PVOID
)&DeviceRelations
);
675 if (!NT_SUCCESS(Status
)) return Status
;
677 /* Make sure it's not NULL and contains only one object */
678 ASSERT(DeviceRelations
);
679 ASSERT(DeviceRelations
->Count
== 1);
681 /* Finally get the device node */
682 *DeviceNode
= IopGetDeviceNode(DeviceRelations
->Objects
[0]);
683 if (!*DeviceNode
) Status
= STATUS_NO_SUCH_DEVICE
;
685 /* Free the DEVICE_RELATIONS structure, it's not needed anymore */
686 ExFreePool(DeviceRelations
);
691 /* PUBLIC FUNCTIONS ***********************************************************/
696 * Layers a device over the highest device in a device stack.
700 * Device to be attached.
703 * Name of the target device.
706 * Caller storage for the device attached to.
713 IoAttachDevice(PDEVICE_OBJECT SourceDevice
,
714 PUNICODE_STRING TargetDeviceName
,
715 PDEVICE_OBJECT
*AttachedDevice
)
718 PFILE_OBJECT FileObject
= NULL
;
719 PDEVICE_OBJECT TargetDevice
= NULL
;
721 /* Call the helper routine for an attach operation */
722 Status
= IopGetDeviceObjectPointer(TargetDeviceName
,
723 FILE_READ_ATTRIBUTES
,
726 IO_ATTACH_DEVICE_API
);
727 if (!NT_SUCCESS(Status
)) return Status
;
729 /* Attach the device */
730 Status
= IoAttachDeviceToDeviceStackSafe(SourceDevice
,
735 ObDereferenceObject(FileObject
);
740 * IoAttachDeviceByPointer
747 IoAttachDeviceByPointer(IN PDEVICE_OBJECT SourceDevice
,
748 IN PDEVICE_OBJECT TargetDevice
)
750 PDEVICE_OBJECT AttachedDevice
;
751 NTSTATUS Status
= STATUS_SUCCESS
;
754 AttachedDevice
= IoAttachDeviceToDeviceStack(SourceDevice
, TargetDevice
);
755 if (!AttachedDevice
) Status
= STATUS_NO_SUCH_DEVICE
;
757 /* Return the status */
766 IoAttachDeviceToDeviceStack(IN PDEVICE_OBJECT SourceDevice
,
767 IN PDEVICE_OBJECT TargetDevice
)
769 /* Attach it safely */
770 return IopAttachDeviceToDeviceStackSafe(SourceDevice
,
780 IoAttachDeviceToDeviceStackSafe(IN PDEVICE_OBJECT SourceDevice
,
781 IN PDEVICE_OBJECT TargetDevice
,
782 IN OUT PDEVICE_OBJECT
*AttachedToDeviceObject
)
784 /* Call the internal function */
785 if (!IopAttachDeviceToDeviceStackSafe(SourceDevice
,
787 AttachedToDeviceObject
))
790 return STATUS_NO_SUCH_DEVICE
;
794 return STATUS_SUCCESS
;
800 * Allocates memory for and intializes a device object for use for
805 * Driver object passed by IO Manager when the driver was loaded.
807 * DeviceExtensionSize
808 * Number of bytes for the device extension.
811 * Unicode name of device.
814 * Device type of the new device.
816 * DeviceCharacteristics
817 * Bit mask of device characteristics.
820 * TRUE if only one thread can access the device at a time.
823 * On successful return this parameter is filled by pointer to
824 * allocated device object.
831 IoCreateDevice(IN PDRIVER_OBJECT DriverObject
,
832 IN ULONG DeviceExtensionSize
,
833 IN PUNICODE_STRING DeviceName
,
834 IN DEVICE_TYPE DeviceType
,
835 IN ULONG DeviceCharacteristics
,
836 IN BOOLEAN Exclusive
,
837 OUT PDEVICE_OBJECT
*DeviceObject
)
839 WCHAR AutoNameBuffer
[20];
840 UNICODE_STRING AutoName
;
841 PDEVICE_OBJECT CreatedDeviceObject
;
842 PDEVOBJ_EXTENSION DeviceObjectExtension
;
843 OBJECT_ATTRIBUTES ObjectAttributes
;
845 ULONG AlignedDeviceExtensionSize
;
850 /* Check if we have to generate a name */
851 if (DeviceCharacteristics
& FILE_AUTOGENERATED_DEVICE_NAME
)
854 swprintf(AutoNameBuffer
,
856 InterlockedIncrementUL(&IopDeviceObjectNumber
));
858 /* Initialize the name */
859 RtlInitUnicodeString(&AutoName
, AutoNameBuffer
);
860 DeviceName
= &AutoName
;
863 /* Initialize the Object Attributes */
864 InitializeObjectAttributes(&ObjectAttributes
,
868 SePublicOpenUnrestrictedSd
);
870 /* Honor exclusive flag */
871 if (Exclusive
) ObjectAttributes
.Attributes
|= OBJ_EXCLUSIVE
;
873 /* Create a permanent object for named devices */
874 if (DeviceName
) ObjectAttributes
.Attributes
|= OBJ_PERMANENT
;
876 /* Align the Extension Size to 8-bytes */
877 AlignedDeviceExtensionSize
= (DeviceExtensionSize
+ 7) &~ 7;
880 TotalSize
= AlignedDeviceExtensionSize
+
881 sizeof(DEVICE_OBJECT
) +
882 sizeof(EXTENDED_DEVOBJ_EXTENSION
);
884 /* Create the Device Object */
885 *DeviceObject
= NULL
;
886 Status
= ObCreateObject(KernelMode
,
894 (PVOID
*)&CreatedDeviceObject
);
895 if (!NT_SUCCESS(Status
)) return Status
;
897 /* Clear the whole Object and extension so we don't null stuff manually */
898 RtlZeroMemory(CreatedDeviceObject
, TotalSize
);
901 * Setup the Type and Size. Note that we don't use the aligned size,
902 * because that's only padding for the DevObjExt and not part of the Object.
904 CreatedDeviceObject
->Type
= IO_TYPE_DEVICE
;
905 CreatedDeviceObject
->Size
= sizeof(DEVICE_OBJECT
) + (USHORT
)DeviceExtensionSize
;
907 /* The kernel extension is after the driver internal extension */
908 DeviceObjectExtension
= (PDEVOBJ_EXTENSION
)
909 ((ULONG_PTR
)(CreatedDeviceObject
+ 1) +
910 AlignedDeviceExtensionSize
);
912 /* Set the Type and Size. Question: why is Size 0 on Windows? */
913 DeviceObjectExtension
->Type
= IO_TYPE_DEVICE_OBJECT_EXTENSION
;
914 DeviceObjectExtension
->Size
= 0;
916 /* Initialize with Power Manager */
917 PoInitializeDeviceObject(DeviceObjectExtension
);
919 /* Link the Object and Extension */
920 DeviceObjectExtension
->DeviceObject
= CreatedDeviceObject
;
921 CreatedDeviceObject
->DeviceObjectExtension
= DeviceObjectExtension
;
923 /* Set Device Object Data */
924 CreatedDeviceObject
->DeviceType
= DeviceType
;
925 CreatedDeviceObject
->Characteristics
= DeviceCharacteristics
;
926 CreatedDeviceObject
->DeviceExtension
= DeviceExtensionSize
?
927 CreatedDeviceObject
+ 1 :
929 CreatedDeviceObject
->StackSize
= 1;
930 CreatedDeviceObject
->AlignmentRequirement
= 0;
933 CreatedDeviceObject
->Flags
= DO_DEVICE_INITIALIZING
;
934 if (Exclusive
) CreatedDeviceObject
->Flags
|= DO_EXCLUSIVE
;
935 if (DeviceName
) CreatedDeviceObject
->Flags
|= DO_DEVICE_HAS_NAME
;
937 /* Attach a Vpb for Disks and Tapes, and create the Device Lock */
938 if ((CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK
) ||
939 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_VIRTUAL_DISK
) ||
940 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM
) ||
941 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE
))
944 Status
= IopCreateVpb(CreatedDeviceObject
);
945 if (!NT_SUCCESS(Status
))
947 /* Dereference the device object and fail */
948 ObDereferenceObject(CreatedDeviceObject
);
952 /* Initialize Lock Event */
953 KeInitializeEvent(&CreatedDeviceObject
->DeviceLock
,
954 SynchronizationEvent
,
958 /* Set the right Sector Size */
961 /* All disk systems */
962 case FILE_DEVICE_DISK_FILE_SYSTEM
:
963 case FILE_DEVICE_DISK
:
964 case FILE_DEVICE_VIRTUAL_DISK
:
966 /* The default is 512 bytes */
967 CreatedDeviceObject
->SectorSize
= 512;
970 /* CD-ROM file systems */
971 case FILE_DEVICE_CD_ROM_FILE_SYSTEM
:
973 /* The default is 2048 bytes */
974 CreatedDeviceObject
->SectorSize
= 2048;
977 /* Create the Device Queue */
978 if ((CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK_FILE_SYSTEM
) ||
979 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_FILE_SYSTEM
) ||
980 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM_FILE_SYSTEM
) ||
981 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_NETWORK_FILE_SYSTEM
) ||
982 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE_FILE_SYSTEM
))
984 /* Simple FS Devices, they don't need a real Device Queue */
985 InitializeListHead(&CreatedDeviceObject
->Queue
.ListEntry
);
989 /* An actual Device, initialize its DQ */
990 KeInitializeDeviceQueue(&CreatedDeviceObject
->DeviceQueue
);
993 /* Insert the Object */
994 Status
= ObInsertObject(CreatedDeviceObject
,
996 FILE_READ_DATA
| FILE_WRITE_DATA
,
998 (PVOID
*)&CreatedDeviceObject
,
1000 if (!NT_SUCCESS(Status
)) return Status
;
1002 /* Now do the final linking */
1003 ObReferenceObject(DriverObject
);
1004 ASSERT((DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
) == 0);
1005 CreatedDeviceObject
->DriverObject
= DriverObject
;
1006 IopEditDeviceList(DriverObject
, CreatedDeviceObject
, IopAdd
);
1008 /* Link with the power manager */
1009 if (CreatedDeviceObject
->Vpb
) PoVolumeDevice(CreatedDeviceObject
);
1011 /* Close the temporary handle and return to caller */
1012 ObCloseHandle(TempHandle
, KernelMode
);
1013 *DeviceObject
= CreatedDeviceObject
;
1014 return STATUS_SUCCESS
;
1025 IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject
)
1029 /* Check if the device is registered for shutdown notifications */
1030 if (DeviceObject
->Flags
& DO_SHUTDOWN_REGISTERED
)
1032 /* Call the shutdown notifications */
1033 IoUnregisterShutdownNotification(DeviceObject
);
1036 /* Check if it has a timer */
1037 Timer
= DeviceObject
->Timer
;
1040 /* Remove it and free it */
1041 IopRemoveTimerFromTimerList(Timer
);
1042 ExFreePoolWithTag(Timer
, TAG_IO_TIMER
);
1045 /* Check if the device has a name */
1046 if (DeviceObject
->Flags
& DO_DEVICE_HAS_NAME
)
1048 /* It does, make it temporary so we can remove it */
1049 ObMakeTemporaryObject(DeviceObject
);
1052 /* Set the pending delete flag */
1053 IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
|= DOE_DELETE_PENDING
;
1055 /* Unlink with the power manager */
1056 if (DeviceObject
->Vpb
) PoRemoveVolumeDevice(DeviceObject
);
1058 /* Check if the device object can be unloaded */
1059 if (!DeviceObject
->ReferenceCount
) IopUnloadDevice(DeviceObject
);
1070 IoDetachDevice(IN PDEVICE_OBJECT TargetDevice
)
1072 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1075 DeviceExtension
= IoGetDevObjExtension(TargetDevice
->AttachedDevice
);
1076 ASSERT(DeviceExtension
->AttachedTo
== TargetDevice
);
1078 /* Remove the attachment */
1079 DeviceExtension
->AttachedTo
= NULL
;
1080 TargetDevice
->AttachedDevice
= NULL
;
1082 /* Check if it's ok to delete this device */
1083 if ((IoGetDevObjExtension(TargetDevice
)->ExtensionFlags
& DOE_DELETE_PENDING
) &&
1084 !(TargetDevice
->ReferenceCount
))
1087 IopUnloadDevice(TargetDevice
);
1096 IoEnumerateDeviceObjectList(IN PDRIVER_OBJECT DriverObject
,
1097 IN PDEVICE_OBJECT
*DeviceObjectList
,
1098 IN ULONG DeviceObjectListSize
,
1099 OUT PULONG ActualNumberDeviceObjects
)
1101 ULONG ActualDevices
= 1;
1102 PDEVICE_OBJECT CurrentDevice
= DriverObject
->DeviceObject
;
1105 /* Lock the Device list while we enumerate it */
1106 OldIrql
= KeAcquireQueuedSpinLock(LockQueueIoDatabaseLock
);
1108 /* Find out how many devices we'll enumerate */
1109 while ((CurrentDevice
= CurrentDevice
->NextDevice
)) ActualDevices
++;
1111 /* Go back to the first */
1112 CurrentDevice
= DriverObject
->DeviceObject
;
1114 /* Start by at least returning this */
1115 *ActualNumberDeviceObjects
= ActualDevices
;
1117 /* Check if we can support so many */
1118 if ((ActualDevices
* sizeof(PDEVICE_OBJECT
)) > DeviceObjectListSize
)
1120 /* Fail because the buffer was too small */
1121 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock
, OldIrql
);
1122 return STATUS_BUFFER_TOO_SMALL
;
1125 /* Check if the caller wanted the device list */
1126 if (DeviceObjectList
)
1128 /* Loop through all the devices */
1129 while (ActualDevices
)
1131 /* Reference each Device */
1132 ObReferenceObject(CurrentDevice
);
1134 /* Add it to the list */
1135 *DeviceObjectList
= CurrentDevice
;
1137 /* Go to the next one */
1138 CurrentDevice
= CurrentDevice
->NextDevice
;
1144 /* Release the device list lock */
1145 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock
, OldIrql
);
1147 /* Return the status */
1148 return STATUS_SUCCESS
;
1152 * IoGetAttachedDevice
1159 IoGetAttachedDevice(PDEVICE_OBJECT DeviceObject
)
1161 /* Get the last attached device */
1162 while (DeviceObject
->AttachedDevice
)
1164 /* Move to the next one */
1165 DeviceObject
= DeviceObject
->AttachedDevice
;
1169 return DeviceObject
;
1173 * IoGetAttachedDeviceReference
1180 IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject
)
1182 /* Reference the Attached Device */
1183 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1184 ObReferenceObject(DeviceObject
);
1185 return DeviceObject
;
1193 IoGetDeviceAttachmentBaseRef(IN PDEVICE_OBJECT DeviceObject
)
1195 /* Reference the lowest attached device */
1196 DeviceObject
= IopGetLowestDevice(DeviceObject
);
1197 ObReferenceObject(DeviceObject
);
1198 return DeviceObject
;
1202 * IoGetDeviceObjectPointer
1209 IoGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName
,
1210 IN ACCESS_MASK DesiredAccess
,
1211 OUT PFILE_OBJECT
*FileObject
,
1212 OUT PDEVICE_OBJECT
*DeviceObject
)
1214 /* Call the helper routine for a normal operation */
1215 return IopGetDeviceObjectPointer(ObjectName
,
1227 IoGetDiskDeviceObject(IN PDEVICE_OBJECT FileSystemDeviceObject
,
1228 OUT PDEVICE_OBJECT
*DiskDeviceObject
)
1230 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1235 /* Make sure there's a VPB */
1236 if (!FileSystemDeviceObject
->Vpb
) return STATUS_INVALID_PARAMETER
;
1239 IoAcquireVpbSpinLock(&OldIrql
);
1241 /* Get the Device Extension */
1242 DeviceExtension
= IoGetDevObjExtension(FileSystemDeviceObject
);
1244 /* Make sure this one has a VPB too */
1245 Vpb
= DeviceExtension
->Vpb
;
1248 /* Make sure that it's mounted */
1249 if ((Vpb
->ReferenceCount
) &&
1250 (Vpb
->Flags
& VPB_MOUNTED
))
1252 /* Return the Disk Device Object */
1253 *DiskDeviceObject
= Vpb
->RealDevice
;
1255 /* Reference it and return success */
1256 ObReferenceObject(Vpb
->RealDevice
);
1257 Status
= STATUS_SUCCESS
;
1261 /* It's not, so return failure */
1262 Status
= STATUS_VOLUME_DISMOUNTED
;
1268 Status
= STATUS_INVALID_PARAMETER
;
1271 /* Release the lock */
1272 IoReleaseVpbSpinLock(OldIrql
);
1281 IoGetLowerDeviceObject(IN PDEVICE_OBJECT DeviceObject
)
1283 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1284 PDEVICE_OBJECT LowerDeviceObject
= NULL
;
1286 /* Make sure it's not getting deleted */
1287 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1288 if (!(DeviceExtension
->ExtensionFlags
& (DOE_UNLOAD_PENDING
|
1289 DOE_DELETE_PENDING
|
1290 DOE_REMOVE_PENDING
|
1291 DOE_REMOVE_PROCESSED
)))
1293 /* Get the Lower Device Object */
1294 LowerDeviceObject
= DeviceExtension
->AttachedTo
;
1296 /* Check that we got a valid device object */
1297 if (LowerDeviceObject
)
1299 /* We did so let's reference it */
1300 ObReferenceObject(LowerDeviceObject
);
1305 return LowerDeviceObject
;
1313 IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject
)
1315 PDEVICE_OBJECT DeviceObject
= FileObject
->DeviceObject
;
1317 /* Check if we have a VPB with a device object */
1318 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1320 /* Then use the DO from the VPB */
1321 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1322 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1324 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1325 (FileObject
->DeviceObject
->Vpb
) &&
1326 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1328 /* The disk device actually has a VPB, so get the DO from there */
1329 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1333 /* Otherwise, this was a direct open */
1334 DeviceObject
= FileObject
->DeviceObject
;
1338 ASSERT(DeviceObject
!= NULL
);
1340 /* Check if we were attached */
1341 if (DeviceObject
->AttachedDevice
)
1343 /* Check if the file object has an extension present */
1344 if (FileObject
->Flags
& FO_FILE_OBJECT_HAS_EXTENSION
)
1346 /* Sanity check, direct open files can't have this */
1347 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1349 /* Check if the extension is really present */
1350 if (FileObject
->FileObjectExtension
)
1352 /* FIXME: Unhandled yet */
1353 DPRINT1("FOEs not supported\n");
1358 /* Return the highest attached device */
1359 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1362 /* Return the DO we found */
1363 return DeviceObject
;
1371 IoGetRelatedTargetDevice(IN PFILE_OBJECT FileObject
,
1372 OUT PDEVICE_OBJECT
*DeviceObject
)
1375 PDEVICE_NODE DeviceNode
= NULL
;
1377 /* Call the internal helper function */
1378 Status
= IopGetRelatedTargetDevice(FileObject
, &DeviceNode
);
1379 if (NT_SUCCESS(Status
) && DeviceNode
)
1381 *DeviceObject
= DeviceNode
->PhysicalDeviceObject
;
1391 IoGetBaseFileSystemDeviceObject(IN PFILE_OBJECT FileObject
)
1393 PDEVICE_OBJECT DeviceObject
;
1396 * If the FILE_OBJECT's VPB is defined,
1397 * get the device from it.
1399 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1401 /* Use the VPB's Device Object's */
1402 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1404 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1405 (FileObject
->DeviceObject
->Vpb
) &&
1406 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1408 /* Use the VPB's File System Object */
1409 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1413 /* Use the FO's Device Object */
1414 DeviceObject
= FileObject
->DeviceObject
;
1417 /* Return the device object we found */
1418 ASSERT(DeviceObject
!= NULL
);
1419 return DeviceObject
;
1427 IoRegisterLastChanceShutdownNotification(IN PDEVICE_OBJECT DeviceObject
)
1429 PSHUTDOWN_ENTRY Entry
;
1431 /* Allocate the shutdown entry */
1432 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1433 sizeof(SHUTDOWN_ENTRY
),
1434 TAG_SHUTDOWN_ENTRY
);
1435 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1438 Entry
->DeviceObject
= DeviceObject
;
1440 /* Reference it so it doesn't go away */
1441 ObReferenceObject(DeviceObject
);
1443 /* Insert it into the list */
1444 ExInterlockedInsertHeadList(&LastChanceShutdownListHead
,
1445 &Entry
->ShutdownList
,
1448 /* Set the shutdown registered flag */
1449 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1450 return STATUS_SUCCESS
;
1458 IoRegisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1460 PSHUTDOWN_ENTRY Entry
;
1462 /* Allocate the shutdown entry */
1463 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1464 sizeof(SHUTDOWN_ENTRY
),
1465 TAG_SHUTDOWN_ENTRY
);
1466 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1469 Entry
->DeviceObject
= DeviceObject
;
1471 /* Reference it so it doesn't go away */
1472 ObReferenceObject(DeviceObject
);
1474 /* Insert it into the list */
1475 ExInterlockedInsertHeadList(&ShutdownListHead
,
1476 &Entry
->ShutdownList
,
1479 /* Set the shutdown registered flag */
1480 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1481 return STATUS_SUCCESS
;
1489 IoUnregisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1491 PSHUTDOWN_ENTRY ShutdownEntry
;
1492 PLIST_ENTRY NextEntry
;
1495 /* Remove the flag */
1496 DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
1498 /* Acquire the shutdown lock and loop the shutdown list */
1499 KeAcquireSpinLock(&ShutdownListLock
, &OldIrql
);
1500 NextEntry
= ShutdownListHead
.Flink
;
1501 while (NextEntry
!= &ShutdownListHead
)
1504 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1508 /* Get if the DO matches */
1509 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1511 /* Remove it from the list */
1512 RemoveEntryList(NextEntry
);
1513 NextEntry
= NextEntry
->Blink
;
1515 /* Free the entry */
1516 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
1518 /* Get rid of our reference to it */
1519 ObDereferenceObject(DeviceObject
);
1522 /* Go to the next entry */
1523 NextEntry
= NextEntry
->Flink
;
1526 /* Now loop the last chance list */
1527 NextEntry
= LastChanceShutdownListHead
.Flink
;
1528 while (NextEntry
!= &LastChanceShutdownListHead
)
1531 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1535 /* Get if the DO matches */
1536 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1538 /* Remove it from the list */
1539 RemoveEntryList(NextEntry
);
1540 NextEntry
= NextEntry
->Blink
;
1542 /* Free the entry */
1543 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
1545 /* Get rid of our reference to it */
1546 ObDereferenceObject(DeviceObject
);
1549 /* Go to the next entry */
1550 NextEntry
= NextEntry
->Flink
;
1553 /* Release the shutdown lock */
1554 KeReleaseSpinLock(&ShutdownListLock
, OldIrql
);
1562 IoSetStartIoAttributes(IN PDEVICE_OBJECT DeviceObject
,
1563 IN BOOLEAN DeferredStartIo
,
1564 IN BOOLEAN NonCancelable
)
1566 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1568 /* Get the Device Extension */
1569 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1571 /* Set the flags the caller requested */
1572 DeviceExtension
->StartIoFlags
|= (DeferredStartIo
) ? DOE_SIO_DEFERRED
: 0;
1573 DeviceExtension
->StartIoFlags
|= (NonCancelable
) ? DOE_SIO_NO_CANCEL
: 0;
1581 IoStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
1582 IN BOOLEAN Cancelable
,
1585 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1587 /* Get the Device Extension */
1588 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1590 /* Check if deferred start was requested */
1591 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1593 /* Call our internal function to handle the defered case */
1594 IopStartNextPacketByKeyEx(DeviceObject
,
1597 (Cancelable
? DOE_SIO_CANCELABLE
: 0));
1601 /* Call the normal routine */
1602 IopStartNextPacketByKey(DeviceObject
, Cancelable
, Key
);
1611 IoStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
1612 IN BOOLEAN Cancelable
)
1614 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1616 /* Get the Device Extension */
1617 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1619 /* Check if deferred start was requested */
1620 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1622 /* Call our internal function to handle the defered case */
1623 IopStartNextPacketByKeyEx(DeviceObject
,
1626 (Cancelable
? DOE_SIO_CANCELABLE
: 0));
1630 /* Call the normal routine */
1631 IopStartNextPacket(DeviceObject
, Cancelable
);
1640 IoStartPacket(IN PDEVICE_OBJECT DeviceObject
,
1643 IN PDRIVER_CANCEL CancelFunction
)
1646 KIRQL OldIrql
, CancelIrql
;
1648 /* Raise to dispatch level */
1649 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1651 /* Check if we should acquire the cancel lock */
1654 /* Acquire and set it */
1655 IoAcquireCancelSpinLock(&CancelIrql
);
1656 Irp
->CancelRoutine
= CancelFunction
;
1659 /* Check if we have a key */
1663 Stat
= KeInsertByKeyDeviceQueue(&DeviceObject
->DeviceQueue
,
1664 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
1669 /* Insert without a key */
1670 Stat
= KeInsertDeviceQueue(&DeviceObject
->DeviceQueue
,
1671 &Irp
->Tail
.Overlay
.DeviceQueueEntry
);
1674 /* Check if this was a first insert */
1678 DeviceObject
->CurrentIrp
= Irp
;
1680 /* Check if this is a cancelable packet */
1683 /* Check if the caller requested no cancellation */
1684 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
1687 /* He did, so remove the cancel routine */
1688 Irp
->CancelRoutine
= NULL
;
1691 /* Release the cancel lock */
1692 IoReleaseCancelSpinLock(CancelIrql
);
1695 /* Call the Start I/O function */
1696 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
1700 /* The packet was inserted... check if we have a cancel function */
1703 /* Check if the IRP got cancelled */
1707 * Set the cancel IRQL, clear the currnet cancel routine and
1710 Irp
->CancelIrql
= CancelIrql
;
1711 Irp
->CancelRoutine
= NULL
;
1712 CancelFunction(DeviceObject
, Irp
);
1716 /* Otherwise, release the lock */
1717 IoReleaseCancelSpinLock(CancelIrql
);
1722 /* Return back to previous IRQL */
1723 KeLowerIrql(OldIrql
);
1726 #if defined (_WIN64)
1729 IoWMIDeviceObjectToProviderId(
1730 IN PDEVICE_OBJECT DeviceObject
)