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 *******************************************************************/
15 #include <internal/debug.h>
17 /* GLOBALS ********************************************************************/
19 ULONG IopDeviceObjectNumber
= 0;
21 LIST_ENTRY ShutdownListHead
, LastChanceShutdownListHead
;
22 KSPIN_LOCK ShutdownListLock
;
24 /* PRIVATE FUNCTIONS **********************************************************/
28 IoShutdownRegisteredDevices(VOID
)
30 PLIST_ENTRY ListEntry
;
31 PDEVICE_OBJECT DeviceObject
;
32 PSHUTDOWN_ENTRY ShutdownEntry
;
33 IO_STATUS_BLOCK StatusBlock
;
38 /* Initialize an event to wait on */
39 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
41 /* Get the first entry and start looping */
42 ListEntry
= ExInterlockedRemoveHeadList(&ShutdownListHead
,
46 /* Get the shutdown entry */
47 ShutdownEntry
= CONTAINING_RECORD(ListEntry
,
51 /* Get the attached device */
52 DeviceObject
= IoGetAttachedDevice(ShutdownEntry
->DeviceObject
);
54 /* Build the shutdown IRP and call the driver */
55 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_SHUTDOWN
,
62 Status
= IoCallDriver(DeviceObject
, Irp
);
63 if (Status
== STATUS_PENDING
)
65 /* Wait on the driver */
66 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
69 /* Free the shutdown entry and reset the event */
70 ExFreePool(ShutdownEntry
);
73 /* Go to the next entry */
74 ListEntry
= ExInterlockedRemoveHeadList(&ShutdownListHead
,
81 IopGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName
,
82 IN ACCESS_MASK DesiredAccess
,
83 OUT PFILE_OBJECT
*FileObject
,
84 OUT PDEVICE_OBJECT
*DeviceObject
,
87 OBJECT_ATTRIBUTES ObjectAttributes
;
88 IO_STATUS_BLOCK StatusBlock
;
89 PFILE_OBJECT LocalFileObject
;
94 InitializeObjectAttributes(&ObjectAttributes
,
99 Status
= ZwOpenFile(&FileHandle
,
104 FILE_NON_DIRECTORY_FILE
| AttachFlag
);
105 if (!NT_SUCCESS(Status
)) return Status
;
107 /* Get File Object */
108 Status
= ObReferenceObjectByHandle(FileHandle
,
112 (PVOID
*)&LocalFileObject
,
114 if (NT_SUCCESS(Status
))
116 /* Return the requested data */
117 *DeviceObject
= IoGetRelatedDeviceObject(LocalFileObject
);
118 *FileObject
= LocalFileObject
;
122 /* Close the handle */
128 IopGetLowestDevice(IN PDEVICE_OBJECT DeviceObject
)
130 PDEVICE_OBJECT LowestDevice
;
131 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
133 /* Get the current device and its extension */
134 LowestDevice
= DeviceObject
;
135 DeviceExtension
= IoGetDevObjExtension(LowestDevice
);
137 /* Keep looping as long as we're attached */
138 while (DeviceExtension
->AttachedTo
)
140 /* Get the lowest device and its extension */
141 LowestDevice
= DeviceExtension
->AttachedTo
;
142 DeviceExtension
= IoGetDevObjExtension(LowestDevice
);
145 /* Return the lowest device */
151 IopEditDeviceList(IN PDRIVER_OBJECT DriverObject
,
152 IN PDEVICE_OBJECT DeviceObject
,
153 IN IOP_DEVICE_LIST_OPERATION Type
)
155 PDEVICE_OBJECT Previous
;
157 /* Check the type of operation */
158 if (Type
== IopRemove
)
160 /* Get the current device and check if it's the current one */
161 Previous
= DeviceObject
->DriverObject
->DeviceObject
;
162 if (Previous
== DeviceObject
)
164 /* It is, simply unlink this one directly */
165 DeviceObject
->DriverObject
->DeviceObject
=
166 DeviceObject
->NextDevice
;
170 /* It's not, so loop until we find the device */
171 while (Previous
->NextDevice
!= DeviceObject
)
173 /* Not this one, keep moving */
174 Previous
= Previous
->NextDevice
;
177 /* We found it, now unlink us */
178 Previous
->NextDevice
= DeviceObject
->NextDevice
;
183 /* Link the device object and the driver object */
184 DeviceObject
->NextDevice
= DriverObject
->DeviceObject
;
185 DriverObject
->DeviceObject
= DeviceObject
;
191 IopUnloadDevice(IN PDEVICE_OBJECT DeviceObject
)
193 PDRIVER_OBJECT DriverObject
= DeviceObject
->DriverObject
;
194 PDEVICE_OBJECT AttachedDeviceObject
, LowestDeviceObject
;
195 PEXTENDED_DEVOBJ_EXTENSION ThisExtension
, DeviceExtension
;
196 PDEVICE_NODE DeviceNode
;
197 BOOLEAN SafeToUnload
= TRUE
;
199 /* Check if removal is pending */
200 ThisExtension
= IoGetDevObjExtension(DeviceObject
);
201 if (ThisExtension
->ExtensionFlags
& DOE_REMOVE_PENDING
)
203 /* Get the PDO, extension, and node */
204 LowestDeviceObject
= IopGetLowestDevice(DeviceObject
);
205 DeviceExtension
= IoGetDevObjExtension(LowestDeviceObject
);
206 DeviceNode
= DeviceExtension
->DeviceNode
;
208 /* The PDO needs a device node */
209 ASSERT(DeviceNode
!= NULL
);
211 /* Loop all attached objects */
212 AttachedDeviceObject
= LowestDeviceObject
;
213 while (AttachedDeviceObject
)
215 /* Make sure they're dereferenced */
216 if (AttachedDeviceObject
->ReferenceCount
) return;
217 AttachedDeviceObject
= AttachedDeviceObject
->AttachedDevice
;
220 /* Loop all attached objects */
221 AttachedDeviceObject
= LowestDeviceObject
;
222 while (AttachedDeviceObject
)
224 /* Get the device extension */
225 DeviceExtension
= IoGetDevObjExtension(AttachedDeviceObject
);
227 /* Remove the pending flag and set processed */
228 DeviceExtension
->ExtensionFlags
&= ~DOE_REMOVE_PENDING
;
229 DeviceExtension
->ExtensionFlags
|= DOE_REMOVE_PROCESSED
;
230 AttachedDeviceObject
= AttachedDeviceObject
->AttachedDevice
;
234 * FIXME: TODO HPOUSSIN
235 * We need to parse/lock the device node, and if we have any pending
236 * surprise removals, query all relationships and send IRP_MN_REMOVE_
237 * _DEVICE to the devices related...
242 /* Check if deletion is pending */
243 if (ThisExtension
->ExtensionFlags
& DOE_DELETE_PENDING
)
245 /* Make sure unload is pending */
246 if (!(ThisExtension
->ExtensionFlags
& DOE_UNLOAD_PENDING
) ||
247 (DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
))
249 /* We can't unload anymore */
250 SafeToUnload
= FALSE
;
254 * Check if we have an attached device and fail if we're attached
255 * and still have a reference count.
257 AttachedDeviceObject
= DeviceObject
->AttachedDevice
;
258 if ((AttachedDeviceObject
) && (DeviceObject
->ReferenceCount
)) return;
260 /* Check if we have a Security Descriptor */
261 if (DeviceObject
->SecurityDescriptor
)
264 ExFreePool(DeviceObject
->SecurityDescriptor
);
267 /* Remove the device from the list */
268 IopEditDeviceList(DeviceObject
->DriverObject
, DeviceObject
, IopRemove
);
270 /* Dereference the keep-alive */
271 ObDereferenceObject(DeviceObject
);
273 /* If we're not unloading, stop here */
274 if (!SafeToUnload
) return;
277 /* Loop all the device objects */
278 DeviceObject
= DriverObject
->DeviceObject
;
282 * Make sure we're not attached, having a reference count
283 * or already deleting
285 if ((DeviceObject
->ReferenceCount
) ||
286 (DeviceObject
->AttachedDevice
) ||
287 (IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
&
288 (DOE_DELETE_PENDING
| DOE_REMOVE_PENDING
)))
290 /* We're not safe to unload, quit */
294 /* Check the next device */
295 DeviceObject
= DeviceObject
->NextDevice
;
298 /* Set the unload invoked flag */
299 DriverObject
->Flags
|= DRVO_UNLOAD_INVOKED
;
302 if (DriverObject
->DriverUnload
) DriverObject
->DriverUnload(DriverObject
);
307 IopDereferenceDeviceObject(IN PDEVICE_OBJECT DeviceObject
,
308 IN BOOLEAN ForceUnload
)
311 ASSERT(DeviceObject
->ReferenceCount
);
313 /* Dereference the device */
314 DeviceObject
->ReferenceCount
--;
317 * Check if we can unload it and it's safe to unload (or if we're forcing
318 * an unload, which is OK too).
320 if (!(DeviceObject
->ReferenceCount
) &&
321 ((ForceUnload
) || (IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
&
322 (DOE_UNLOAD_PENDING
|
325 DOE_REMOVE_PROCESSED
))))
328 IopUnloadDevice(DeviceObject
);
334 IopReferenceDeviceObject(IN PDEVICE_OBJECT DeviceObject
)
336 /* Make sure the object is valid */
337 if ((IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
&
338 (DOE_UNLOAD_PENDING
|
341 DOE_REMOVE_PROCESSED
)) ||
342 (DeviceObject
->Flags
& DO_DEVICE_INITIALIZING
))
344 /* It's unloading or initializing, so fail */
345 DPRINT1("You are seeing this because the following ROS driver: %wZ\n"
346 " sucks. Please fix it's AddDevice Routine\n",
347 &DeviceObject
->DriverObject
->DriverName
);
348 return STATUS_NO_SUCH_DEVICE
;
352 /* Increase reference count */
353 DeviceObject
->ReferenceCount
++;
354 return STATUS_SUCCESS
;
360 IopStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
361 IN BOOLEAN Cancelable
,
364 PKDEVICE_QUEUE_ENTRY Entry
;
368 /* Acquire the cancel lock if this is cancelable */
369 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
371 /* Clear the current IRP */
372 DeviceObject
->CurrentIrp
= NULL
;
374 /* Remove an entry from the queue */
375 Entry
= KeRemoveByKeyDeviceQueue(&DeviceObject
->DeviceQueue
, Key
);
378 /* Get the IRP and set it */
379 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
380 DeviceObject
->CurrentIrp
= Irp
;
382 /* Check if this is a cancelable packet */
385 /* Check if the caller requested no cancellation */
386 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
389 /* He did, so remove the cancel routine */
390 Irp
->CancelRoutine
= NULL
;
393 /* Release the cancel lock */
394 IoReleaseCancelSpinLock(OldIrql
);
397 /* Call the Start I/O Routine */
398 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
402 /* Otherwise, release the cancel lock if we had acquired it */
403 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
409 IopStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
410 IN BOOLEAN Cancelable
)
412 PKDEVICE_QUEUE_ENTRY Entry
;
416 /* Acquire the cancel lock if this is cancelable */
417 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
419 /* Clear the current IRP */
420 DeviceObject
->CurrentIrp
= NULL
;
422 /* Remove an entry from the queue */
423 Entry
= KeRemoveDeviceQueue(&DeviceObject
->DeviceQueue
);
426 /* Get the IRP and set it */
427 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
428 DeviceObject
->CurrentIrp
= Irp
;
430 /* Check if this is a cancelable packet */
433 /* Check if the caller requested no cancellation */
434 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
437 /* He did, so remove the cancel routine */
438 Irp
->CancelRoutine
= NULL
;
441 /* Release the cancel lock */
442 IoReleaseCancelSpinLock(OldIrql
);
445 /* Call the Start I/O Routine */
446 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
450 /* Otherwise, release the cancel lock if we had acquired it */
451 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
457 IopStartNextPacketByKeyEx(IN PDEVICE_OBJECT DeviceObject
,
461 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
462 ULONG CurrentKey
= Key
;
463 ULONG CurrentFlags
= Flags
;
465 /* Get the device extension and start the packet loop */
466 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
469 /* Increase the count */
470 if (InterlockedIncrement(&DeviceExtension
->StartIoCount
) > 1)
473 * We've already called the routine once...
474 * All we have to do is save the key and add the new flags
476 DeviceExtension
->StartIoFlags
|= CurrentFlags
;
477 DeviceExtension
->StartIoKey
= CurrentKey
;
481 /* Mask out the current packet flags and key */
482 DeviceExtension
->StartIoFlags
&= ~(DOE_SIO_WITH_KEY
|
485 DeviceExtension
->StartIoKey
= 0;
487 /* Check if this is a packet start with key */
488 if (Flags
& DOE_SIO_WITH_KEY
)
490 /* Start the packet with a key */
491 IopStartNextPacketByKey(DeviceObject
,
492 (DOE_SIO_CANCELABLE
) ? TRUE
: FALSE
,
495 else if (Flags
& DOE_SIO_NO_KEY
)
497 /* Start the packet */
498 IopStartNextPacket(DeviceObject
,
499 (DOE_SIO_CANCELABLE
) ? TRUE
: FALSE
);
503 /* Decrease the Start I/O count and check if it's 0 now */
504 if (!InterlockedDecrement(&DeviceExtension
->StartIoCount
))
506 /* Get the current active key and flags */
507 CurrentKey
= DeviceExtension
->StartIoKey
;
508 CurrentFlags
= DeviceExtension
->StartIoFlags
& (DOE_SIO_WITH_KEY
|
512 /* Check if we should still loop */
513 if (!(CurrentFlags
& (DOE_SIO_WITH_KEY
| DOE_SIO_NO_KEY
))) break;
517 /* There are still Start I/Os active, so quit this loop */
523 /* PUBLIC FUNCTIONS ***********************************************************/
528 * Layers a device over the highest device in a device stack.
532 * Device to be attached.
535 * Name of the target device.
538 * Caller storage for the device attached to.
545 IoAttachDevice(PDEVICE_OBJECT SourceDevice
,
546 PUNICODE_STRING TargetDeviceName
,
547 PDEVICE_OBJECT
*AttachedDevice
)
550 PFILE_OBJECT FileObject
= NULL
;
551 PDEVICE_OBJECT TargetDevice
= NULL
;
553 /* Call the helper routine for an attach operation */
554 Status
= IopGetDeviceObjectPointer(TargetDeviceName
,
555 FILE_READ_ATTRIBUTES
,
558 IO_ATTACH_DEVICE_API
);
559 if (!NT_SUCCESS(Status
)) return Status
;
561 /* Attach the device */
562 Status
= IoAttachDeviceToDeviceStackSafe(SourceDevice
,
565 if (!*AttachedDevice
) Status
= STATUS_NO_SUCH_DEVICE
;
568 ObDereferenceObject(FileObject
);
573 * IoAttachDeviceByPointer
580 IoAttachDeviceByPointer(IN PDEVICE_OBJECT SourceDevice
,
581 IN PDEVICE_OBJECT TargetDevice
)
583 PDEVICE_OBJECT AttachedDevice
;
584 NTSTATUS Status
= STATUS_SUCCESS
;
587 AttachedDevice
= IoAttachDeviceToDeviceStack(SourceDevice
, TargetDevice
);
588 if (!AttachedDevice
) Status
= STATUS_NO_SUCH_DEVICE
;
590 /* Return the status */
595 * IoAttachDeviceToDeviceStack
602 IoAttachDeviceToDeviceStack(PDEVICE_OBJECT SourceDevice
,
603 PDEVICE_OBJECT TargetDevice
)
606 PDEVICE_OBJECT LocalAttach
;
608 /* Attach it safely */
609 Status
= IoAttachDeviceToDeviceStackSafe(SourceDevice
,
622 IoAttachDeviceToDeviceStackSafe(IN PDEVICE_OBJECT SourceDevice
,
623 IN PDEVICE_OBJECT TargetDevice
,
624 OUT PDEVICE_OBJECT
*AttachedToDeviceObject
)
626 PDEVICE_OBJECT AttachedDevice
;
627 PEXTENDED_DEVOBJ_EXTENSION SourceDeviceExtension
;
629 /* Get the Attached Device and source extension */
630 AttachedDevice
= IoGetAttachedDevice(TargetDevice
);
631 SourceDeviceExtension
= IoGetDevObjExtension(SourceDevice
);
633 /* Make sure that it's in a correct state */
634 if (!IoGetDevObjExtension(AttachedDevice
)->ExtensionFlags
&
635 (DOE_UNLOAD_PENDING
|
638 DOE_REMOVE_PROCESSED
))
640 /* Update atached device fields */
641 AttachedDevice
->AttachedDevice
= SourceDevice
;
642 AttachedDevice
->Spare1
++;
644 /* Update the source with the attached data */
645 SourceDevice
->StackSize
= AttachedDevice
->StackSize
+ 1;
646 SourceDevice
->AlignmentRequirement
= AttachedDevice
->
647 AlignmentRequirement
;
648 SourceDevice
->SectorSize
= AttachedDevice
->SectorSize
;
650 /* Set the attachment in the device extension */
651 SourceDeviceExtension
->AttachedTo
= AttachedDevice
;
655 /* Device was unloading or being removed */
656 AttachedDevice
= NULL
;
659 /* Return the attached device */
660 *AttachedToDeviceObject
= AttachedDevice
;
661 return STATUS_SUCCESS
;
667 * Allocates memory for and intializes a device object for use for
672 * Driver object passed by IO Manager when the driver was loaded.
674 * DeviceExtensionSize
675 * Number of bytes for the device extension.
678 * Unicode name of device.
681 * Device type of the new device.
683 * DeviceCharacteristics
684 * Bit mask of device characteristics.
687 * TRUE if only one thread can access the device at a time.
690 * On successful return this parameter is filled by pointer to
691 * allocated device object.
698 IoCreateDevice(IN PDRIVER_OBJECT DriverObject
,
699 IN ULONG DeviceExtensionSize
,
700 IN PUNICODE_STRING DeviceName
,
701 IN DEVICE_TYPE DeviceType
,
702 IN ULONG DeviceCharacteristics
,
703 IN BOOLEAN Exclusive
,
704 OUT PDEVICE_OBJECT
*DeviceObject
)
706 WCHAR AutoNameBuffer
[20];
707 UNICODE_STRING AutoName
;
708 PDEVICE_OBJECT CreatedDeviceObject
;
709 PDEVOBJ_EXTENSION DeviceObjectExtension
;
710 OBJECT_ATTRIBUTES ObjectAttributes
;
712 ULONG AlignedDeviceExtensionSize
;
717 /* Check if we have to generate a name */
718 if (DeviceCharacteristics
& FILE_AUTOGENERATED_DEVICE_NAME
)
721 swprintf(AutoNameBuffer
,
723 InterlockedIncrementUL(&IopDeviceObjectNumber
));
725 /* Initialize the name */
726 RtlInitUnicodeString(&AutoName
, AutoNameBuffer
);
727 DeviceName
= &AutoName
;
730 /* Initialize the Object Attributes */
731 InitializeObjectAttributes(&ObjectAttributes
, DeviceName
, 0, NULL
, NULL
);
733 /* Honor exclusive flag */
734 if (Exclusive
) ObjectAttributes
.Attributes
|= OBJ_EXCLUSIVE
;
736 /* Create a permanent object for named devices */
737 if (DeviceName
) ObjectAttributes
.Attributes
|= OBJ_PERMANENT
;
739 /* Align the Extension Size to 8-bytes */
740 AlignedDeviceExtensionSize
= (DeviceExtensionSize
+ 7) &~ 7;
743 TotalSize
= AlignedDeviceExtensionSize
+
744 sizeof(DEVICE_OBJECT
) +
745 sizeof(EXTENDED_DEVOBJ_EXTENSION
);
747 /* Create the Device Object */
748 *DeviceObject
= NULL
;
749 Status
= ObCreateObject(KernelMode
,
757 (PVOID
*)&CreatedDeviceObject
);
758 if (!NT_SUCCESS(Status
)) return Status
;
760 /* Clear the whole Object and extension so we don't null stuff manually */
761 RtlZeroMemory(CreatedDeviceObject
, TotalSize
);
764 * Setup the Type and Size. Note that we don't use the aligned size,
765 * because that's only padding for the DevObjExt and not part of the Object.
767 CreatedDeviceObject
->Type
= IO_TYPE_DEVICE
;
768 CreatedDeviceObject
->Size
= sizeof(DEVICE_OBJECT
) + DeviceExtensionSize
;
770 /* The kernel extension is after the driver internal extension */
771 DeviceObjectExtension
= (PDEVOBJ_EXTENSION
)
772 ((ULONG_PTR
)(CreatedDeviceObject
+ 1) +
773 AlignedDeviceExtensionSize
);
775 /* Set the Type and Size. Question: why is Size 0 on Windows? */
776 DeviceObjectExtension
->Type
= IO_TYPE_DEVICE_OBJECT_EXTENSION
;
777 DeviceObjectExtension
->Size
= 0;
779 /* Link the Object and Extension */
780 DeviceObjectExtension
->DeviceObject
= CreatedDeviceObject
;
781 CreatedDeviceObject
->DeviceObjectExtension
= DeviceObjectExtension
;
783 /* Set Device Object Data */
784 CreatedDeviceObject
->DeviceType
= DeviceType
;
785 CreatedDeviceObject
->Characteristics
= DeviceCharacteristics
;
786 CreatedDeviceObject
->DeviceExtension
= DeviceExtensionSize
?
787 CreatedDeviceObject
+ 1 :
789 CreatedDeviceObject
->StackSize
= 1;
790 CreatedDeviceObject
->AlignmentRequirement
= 0;
793 CreatedDeviceObject
->Flags
= DO_DEVICE_INITIALIZING
;
794 if (Exclusive
) CreatedDeviceObject
->Flags
|= DO_EXCLUSIVE
;
795 if (DeviceName
) CreatedDeviceObject
->Flags
|= DO_DEVICE_HAS_NAME
;
797 /* Attach a Vpb for Disks and Tapes, and create the Device Lock */
798 if (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK
||
799 CreatedDeviceObject
->DeviceType
== FILE_DEVICE_VIRTUAL_DISK
||
800 CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM
||
801 CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE
)
804 IopCreateVpb(CreatedDeviceObject
);
806 /* Initialize Lock Event */
807 KeInitializeEvent(&CreatedDeviceObject
->DeviceLock
,
808 SynchronizationEvent
,
812 /* Set the right Sector Size */
815 /* All disk systems */
816 case FILE_DEVICE_DISK_FILE_SYSTEM
:
817 case FILE_DEVICE_DISK
:
818 case FILE_DEVICE_VIRTUAL_DISK
:
820 /* The default is 512 bytes */
821 CreatedDeviceObject
->SectorSize
= 512;
824 /* CD-ROM file systems */
825 case FILE_DEVICE_CD_ROM_FILE_SYSTEM
:
827 /* The default is 2048 bytes */
828 CreatedDeviceObject
->SectorSize
= 2048;
831 /* Create the Device Queue */
832 if (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK_FILE_SYSTEM
||
833 CreatedDeviceObject
->DeviceType
== FILE_DEVICE_FILE_SYSTEM
||
834 CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM_FILE_SYSTEM
||
835 CreatedDeviceObject
->DeviceType
== FILE_DEVICE_NETWORK_FILE_SYSTEM
||
836 CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE_FILE_SYSTEM
)
838 /* Simple FS Devices, they don't need a real Device Queue */
839 InitializeListHead(&CreatedDeviceObject
->Queue
.ListEntry
);
843 /* An actual Device, initialize its DQ */
844 KeInitializeDeviceQueue(&CreatedDeviceObject
->DeviceQueue
);
847 /* Insert the Object */
848 Status
= ObInsertObject(CreatedDeviceObject
,
850 FILE_READ_DATA
| FILE_WRITE_DATA
,
852 (PVOID
*)&CreatedDeviceObject
,
854 if (!NT_SUCCESS(Status
))
856 /* Clear the device object and fail */
857 *DeviceObject
= NULL
;
861 /* Now do the final linking */
862 ObReferenceObject(DriverObject
);
863 CreatedDeviceObject
->DriverObject
= DriverObject
;
864 IopEditDeviceList(DriverObject
, CreatedDeviceObject
, IopAdd
);
866 /* Close the temporary handle and return to caller */
868 *DeviceObject
= CreatedDeviceObject
;
869 return STATUS_SUCCESS
;
880 IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject
)
884 /* Check if the device is registered for shutdown notifications */
885 if (DeviceObject
->Flags
& DO_SHUTDOWN_REGISTERED
)
887 /* Call the shutdown notifications */
888 IoUnregisterShutdownNotification(DeviceObject
);
891 /* Check if it has a timer */
892 Timer
= DeviceObject
->Timer
;
895 /* Remove it and free it */
896 IopRemoveTimerFromTimerList(Timer
);
897 ExFreePoolWithTag(Timer
, TAG_IO_TIMER
);
900 /* Check if the device has a name */
901 if (DeviceObject
->Flags
& DO_DEVICE_HAS_NAME
)
903 /* It does, make it temporary so we can remove it */
904 ObMakeTemporaryObject(DeviceObject
);
907 /* Set the pending delete flag */
908 IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
|= DOE_DELETE_PENDING
;
910 /* Check if the device object can be unloaded */
911 if (!DeviceObject
->ReferenceCount
) IopUnloadDevice(DeviceObject
);
922 IoDetachDevice(IN PDEVICE_OBJECT TargetDevice
)
924 /* Remove the attachment */
925 IoGetDevObjExtension(TargetDevice
->AttachedDevice
)->AttachedTo
= NULL
;
926 TargetDevice
->AttachedDevice
= NULL
;
928 /* Check if it's ok to delete this device */
929 if ((IoGetDevObjExtension(TargetDevice
)->ExtensionFlags
&
930 (DOE_UNLOAD_PENDING
| DOE_DELETE_PENDING
| DOE_REMOVE_PENDING
)) &&
931 !(TargetDevice
->ReferenceCount
))
934 IopUnloadDevice(TargetDevice
);
943 IoEnumerateDeviceObjectList(IN PDRIVER_OBJECT DriverObject
,
944 IN PDEVICE_OBJECT
*DeviceObjectList
,
945 IN ULONG DeviceObjectListSize
,
946 OUT PULONG ActualNumberDeviceObjects
)
948 ULONG ActualDevices
= 1;
949 PDEVICE_OBJECT CurrentDevice
= DriverObject
->DeviceObject
;
951 /* Find out how many devices we'll enumerate */
952 while ((CurrentDevice
= CurrentDevice
->NextDevice
)) ActualDevices
++;
954 /* Go back to the first */
955 CurrentDevice
= DriverObject
->DeviceObject
;
957 /* Start by at least returning this */
958 *ActualNumberDeviceObjects
= ActualDevices
;
960 /* Check if we can support so many */
961 if ((ActualDevices
* 4) > DeviceObjectListSize
)
963 /* Fail because the buffer was too small */
964 return STATUS_BUFFER_TOO_SMALL
;
967 /* Check if the caller only wanted the size */
968 if (DeviceObjectList
)
970 /* Loop through all the devices */
971 while (ActualDevices
)
973 /* Reference each Device */
974 ObReferenceObject(CurrentDevice
);
976 /* Add it to the list */
977 *DeviceObjectList
= CurrentDevice
;
979 /* Go to the next one */
980 CurrentDevice
= CurrentDevice
->NextDevice
;
986 /* Return the status */
987 return STATUS_SUCCESS
;
991 * IoGetAttachedDevice
998 IoGetAttachedDevice(PDEVICE_OBJECT DeviceObject
)
1000 /* Get the last attached device */
1001 while (DeviceObject
->AttachedDevice
)
1003 /* Move to the next one */
1004 DeviceObject
= DeviceObject
->AttachedDevice
;
1008 return DeviceObject
;
1012 * IoGetAttachedDeviceReference
1019 IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject
)
1021 /* Reference the Attached Device */
1022 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1023 ObReferenceObject(DeviceObject
);
1024 return DeviceObject
;
1032 IoGetDeviceAttachmentBaseRef(IN PDEVICE_OBJECT DeviceObject
)
1034 /* Return the attached Device */
1035 return IoGetDevObjExtension(DeviceObject
)->AttachedTo
;
1039 * IoGetDeviceObjectPointer
1046 IoGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName
,
1047 IN ACCESS_MASK DesiredAccess
,
1048 OUT PFILE_OBJECT
*FileObject
,
1049 OUT PDEVICE_OBJECT
*DeviceObject
)
1051 /* Call the helper routine for a normal operation */
1052 return IopGetDeviceObjectPointer(ObjectName
,
1064 IoGetDiskDeviceObject(IN PDEVICE_OBJECT FileSystemDeviceObject
,
1065 OUT PDEVICE_OBJECT
*DiskDeviceObject
)
1067 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1071 /* Make sure there's a VPB */
1072 if (!FileSystemDeviceObject
->Vpb
) return STATUS_INVALID_PARAMETER
;
1075 IoAcquireVpbSpinLock(&OldIrql
);
1077 /* Get the Device Extension */
1078 DeviceExtension
= IoGetDevObjExtension(FileSystemDeviceObject
);
1080 /* Make sure this one has a VPB too */
1081 Vpb
= DeviceExtension
->Vpb
;
1082 if (!Vpb
) return STATUS_INVALID_PARAMETER
;
1084 /* Make sure that it's mounted */
1085 if ((!Vpb
->ReferenceCount
) || (Vpb
->Flags
& VPB_MOUNTED
))
1087 /* It's not, so return failure */
1088 return STATUS_VOLUME_DISMOUNTED
;
1091 /* Return the Disk Device Object */
1092 *DiskDeviceObject
= Vpb
->RealDevice
;
1094 /* Release the lock */
1095 IoReleaseVpbSpinLock(OldIrql
);
1096 return STATUS_SUCCESS
;
1104 IoGetLowerDeviceObject(IN PDEVICE_OBJECT DeviceObject
)
1106 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1107 PDEVICE_OBJECT LowerDeviceObject
= NULL
;
1109 /* Make sure it's not getting deleted */
1110 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1111 if (DeviceExtension
->ExtensionFlags
& (DOE_UNLOAD_PENDING
|
1112 DOE_DELETE_PENDING
|
1113 DOE_REMOVE_PENDING
|
1114 DOE_REMOVE_PROCESSED
))
1116 /* Get the Lower Device Object */
1117 LowerDeviceObject
= DeviceExtension
->AttachedTo
;
1120 ObReferenceObject(LowerDeviceObject
);
1124 return LowerDeviceObject
;
1128 * IoGetRelatedDeviceObject
1131 * See "Windows NT File System Internals", page 633 - 634.
1138 IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject
)
1140 PDEVICE_OBJECT DeviceObject
= FileObject
->DeviceObject
;
1142 /* Check if we have a VPB with a device object */
1143 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1145 /* Then use the DO from the VPB */
1146 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1147 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1149 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1150 (FileObject
->DeviceObject
->Vpb
) &&
1151 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1153 /* The disk device actually has a VPB, so get the DO from there */
1154 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1158 /* Otherwise, this was a direct open */
1159 DeviceObject
= FileObject
->DeviceObject
;
1162 /* Check if we were attached */
1163 if (DeviceObject
->AttachedDevice
)
1165 /* Return the highest attached device */
1166 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1169 /* Return the DO we found */
1170 return DeviceObject
;
1178 IoGetBaseFileSystemDeviceObject(IN PFILE_OBJECT FileObject
)
1180 PDEVICE_OBJECT DeviceObject
;
1183 * If the FILE_OBJECT's VPB is defined,
1184 * get the device from it.
1186 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1188 /* Use the VPB's Device Object's */
1189 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1191 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1192 (FileObject
->DeviceObject
->Vpb
) &&
1193 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1195 /* Use the VPB's File System Object */
1196 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1200 /* Use the FO's Device Object */
1201 DeviceObject
= FileObject
->DeviceObject
;
1204 /* Return the device object we found */
1205 return DeviceObject
;
1213 IoRegisterLastChanceShutdownNotification(IN PDEVICE_OBJECT DeviceObject
)
1215 PSHUTDOWN_ENTRY Entry
;
1217 /* Allocate the shutdown entry */
1218 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1219 sizeof(SHUTDOWN_ENTRY
),
1220 TAG_SHUTDOWN_ENTRY
);
1221 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1224 Entry
->DeviceObject
= DeviceObject
;
1226 /* Insert it into the list */
1227 ExInterlockedInsertHeadList(&LastChanceShutdownListHead
,
1228 &Entry
->ShutdownList
,
1231 /* Set the shutdown registered flag */
1232 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1233 return STATUS_SUCCESS
;
1241 IoRegisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1243 PSHUTDOWN_ENTRY Entry
;
1245 /* Allocate the shutdown entry */
1246 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1247 sizeof(SHUTDOWN_ENTRY
),
1248 TAG_SHUTDOWN_ENTRY
);
1249 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1252 Entry
->DeviceObject
= DeviceObject
;
1254 /* Insert it into the list */
1255 ExInterlockedInsertHeadList(&ShutdownListHead
,
1256 &Entry
->ShutdownList
,
1259 /* Set the shutdown registered flag */
1260 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1261 return STATUS_SUCCESS
;
1269 IoUnregisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1271 PSHUTDOWN_ENTRY ShutdownEntry
;
1272 PLIST_ENTRY NextEntry
;
1275 /* Acquire the shutdown lock and loop the shutdown list */
1276 KeAcquireSpinLock(&ShutdownListLock
, &OldIrql
);
1277 NextEntry
= ShutdownListHead
.Flink
;
1278 while (NextEntry
!= &ShutdownListHead
)
1281 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1285 /* Get if the DO matches */
1286 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1288 /* Remove it from the list */
1289 RemoveEntryList(NextEntry
);
1290 NextEntry
= NextEntry
->Blink
;
1292 /* Free the entry */
1293 ExFreePool(ShutdownEntry
);
1296 /* Go to the next entry */
1297 NextEntry
= NextEntry
->Flink
;
1300 /* Now loop the last chance list */
1301 NextEntry
= LastChanceShutdownListHead
.Flink
;
1302 while (NextEntry
!= &LastChanceShutdownListHead
)
1305 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1309 /* Get if the DO matches */
1310 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1312 /* Remove it from the list */
1313 RemoveEntryList(NextEntry
);
1314 NextEntry
= NextEntry
->Blink
;
1316 /* Free the entry */
1317 ExFreePool(ShutdownEntry
);
1320 /* Go to the next entry */
1321 NextEntry
= NextEntry
->Flink
;
1324 /* Now remove the flag */
1325 DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
1333 IoSetStartIoAttributes(IN PDEVICE_OBJECT DeviceObject
,
1334 IN BOOLEAN DeferredStartIo
,
1335 IN BOOLEAN NonCancelable
)
1337 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1339 /* Get the Device Extension */
1340 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1342 /* Set the flags the caller requested */
1343 DeviceExtension
->StartIoFlags
|= (DeferredStartIo
) ? DOE_SIO_DEFERRED
: 0;
1344 DeviceExtension
->StartIoFlags
|= (NonCancelable
) ? DOE_SIO_NO_CANCEL
: 0;
1352 IoStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
1353 IN BOOLEAN Cancelable
,
1356 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1358 /* Get the Device Extension */
1359 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1361 /* Check if deferred start was requested */
1362 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1364 /* Call our internal function to handle the defered case */
1365 return IopStartNextPacketByKeyEx(DeviceObject
,
1368 (Cancelable
) ? DOE_SIO_CANCELABLE
: 0);
1372 /* Call the normal routine */
1373 return IopStartNextPacketByKey(DeviceObject
, Cancelable
, Key
);
1382 IoStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
1383 IN BOOLEAN Cancelable
)
1385 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1387 /* Get the Device Extension */
1388 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1390 /* Check if deferred start was requested */
1391 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1393 /* Call our internal function to handle the defered case */
1394 return IopStartNextPacketByKeyEx(DeviceObject
,
1397 (Cancelable
) ? DOE_SIO_CANCELABLE
: 0);
1401 /* Call the normal routine */
1402 return IopStartNextPacket(DeviceObject
, Cancelable
);
1411 IoStartPacket(IN PDEVICE_OBJECT DeviceObject
,
1414 IN PDRIVER_CANCEL CancelFunction
)
1417 KIRQL OldIrql
, CancelIrql
;
1419 /* Raise to dispatch level */
1420 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1422 /* Check if we should acquire the cancel lock */
1425 /* Acquire and set it */
1426 IoAcquireCancelSpinLock(&CancelIrql
);
1427 Irp
->CancelRoutine
= CancelFunction
;
1430 /* Check if we have a key */
1434 Stat
= KeInsertByKeyDeviceQueue(&DeviceObject
->DeviceQueue
,
1435 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
1440 /* Insert without a key */
1441 Stat
= KeInsertDeviceQueue(&DeviceObject
->DeviceQueue
,
1442 &Irp
->Tail
.Overlay
.DeviceQueueEntry
);
1445 /* Check if this was a first insert */
1449 DeviceObject
->CurrentIrp
= Irp
;
1451 /* Check if this is a cancelable packet */
1454 /* Check if the caller requested no cancellation */
1455 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
1458 /* He did, so remove the cancel routine */
1459 Irp
->CancelRoutine
= NULL
;
1462 /* Release the cancel lock */
1463 IoReleaseCancelSpinLock(OldIrql
);
1466 /* Call the Start I/O function */
1467 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
1471 /* The packet was inserted... check if we have a cancel function */
1474 /* Check if the IRP got cancelled */
1478 * Set the cancel IRQL, clear the currnet cancel routine and
1481 Irp
->CancelIrql
= CancelIrql
;
1482 Irp
->CancelRoutine
= NULL
;
1483 CancelFunction(DeviceObject
, Irp
);
1487 /* Otherwise, release the lock */
1488 IoReleaseCancelSpinLock(CancelIrql
);
1493 /* Return back to previous IRQL */
1494 KeLowerIrql(OldIrql
);