2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/iomgr/irp.c
5 * PURPOSE: IRP Handling Functions
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
8 * Filip Navara (navaraf@reactos.org)
11 /* INCLUDES ****************************************************************/
17 /* Undefine some macros we implement here */
19 #undef IoCompleteRequest
21 /* PRIVATE FUNCTIONS ********************************************************/
25 IopFreeIrpKernelApc(IN PKAPC Apc
,
26 IN PKNORMAL_ROUTINE
*NormalRoutine
,
27 IN PVOID
*NormalContext
,
28 IN PVOID
*SystemArgument1
,
29 IN PVOID
*SystemArgument2
)
32 IoFreeIrp(CONTAINING_RECORD(Apc
, IRP
, Tail
.Apc
));
37 IopAbortIrpKernelApc(IN PKAPC Apc
)
40 IoFreeIrp(CONTAINING_RECORD(Apc
, IRP
, Tail
.Apc
));
45 IopCleanupFailedIrp(IN PFILE_OBJECT FileObject
,
46 IN PKEVENT EventObject OPTIONAL
,
47 IN PVOID Buffer OPTIONAL
)
51 /* Dereference the event */
52 if (EventObject
) ObDereferenceObject(EventObject
);
54 /* Free a buffer, if any */
55 if (Buffer
) ExFreePool(Buffer
);
57 /* If this was a file opened for synch I/O, then unlock it */
58 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
) IopUnlockFileObject(FileObject
);
60 /* Now dereference it and return */
61 ObDereferenceObject(FileObject
);
62 return STATUS_INSUFFICIENT_RESOURCES
;
67 IopAbortInterruptedIrp(IN PKEVENT EventObject
,
75 /* Raise IRQL to APC */
76 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
78 /* Check if nobody completed it yet */
79 if (!KeReadStateEvent(EventObject
))
81 /* First, cancel it */
82 CancelResult
= IoCancelIrp(Irp
);
85 /* Check if we cancelled it */
88 /* Wait for the IRP to be cancelled */
89 Wait
.QuadPart
= -100000;
90 while (!KeReadStateEvent(EventObject
))
92 /* Delay indefintely */
93 KeDelayExecutionThread(KernelMode
, FALSE
, &Wait
);
98 /* No cancellation done, so wait for the I/O system to kill it */
99 KeWaitForSingleObject(EventObject
,
108 /* We got preempted, so give up */
109 KeLowerIrql(OldIrql
);
115 IopRemoveThreadIrp(VOID
)
120 PLIST_ENTRY IrpEntry
;
121 PIO_ERROR_LOG_PACKET ErrorLogEntry
;
122 PDEVICE_OBJECT DeviceObject
= NULL
;
123 PIO_STACK_LOCATION IoStackLocation
;
125 /* First, raise to APC to protect IrpList */
126 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
128 /* Get the Thread and check the list */
129 IrpThread
= PsGetCurrentThread();
130 if (IsListEmpty(&IrpThread
->IrpList
))
132 /* It got completed now, so quit */
133 KeLowerIrql(OldIrql
);
137 /* Get the misbehaving IRP */
138 IrpEntry
= IrpThread
->IrpList
.Flink
;
139 DeadIrp
= CONTAINING_RECORD(IrpEntry
, IRP
, ThreadListEntry
);
140 IOTRACE(IO_IRP_DEBUG
,
141 "%s - Deassociating IRP %p for %p\n",
146 /* Don't cancel the IRP if it's already been completed far */
147 if (DeadIrp
->CurrentLocation
== (DeadIrp
->StackCount
+ 2))
150 KeLowerIrql(OldIrql
);
154 /* Disown the IRP! */
155 DeadIrp
->Tail
.Overlay
.Thread
= NULL
;
156 RemoveHeadList(&IrpThread
->IrpList
);
157 InitializeListHead(&DeadIrp
->ThreadListEntry
);
159 /* Get the stack location and check if it's valid */
160 IoStackLocation
= IoGetCurrentIrpStackLocation(DeadIrp
);
161 if (DeadIrp
->CurrentLocation
<= DeadIrp
->StackCount
)
163 /* Get the device object */
164 DeviceObject
= IoStackLocation
->DeviceObject
;
167 /* Lower IRQL now, since we have the pointers we need */
168 KeLowerIrql(OldIrql
);
170 /* Check if we can send an Error Log Entry*/
173 /* Allocate an entry */
174 ErrorLogEntry
= IoAllocateErrorLogEntry(DeviceObject
,
175 sizeof(IO_ERROR_LOG_PACKET
));
178 /* Write the entry */
179 ErrorLogEntry
->ErrorCode
= 0xBAADF00D; /* FIXME */
180 IoWriteErrorLogEntry(ErrorLogEntry
);
187 IopCleanupIrp(IN PIRP Irp
,
188 IN PFILE_OBJECT FileObject
)
191 IOTRACE(IO_IRP_DEBUG
,
192 "%s - Cleaning IRP %p for %p\n",
197 /* Check if there's an MDL */
198 while ((Mdl
= Irp
->MdlAddress
))
200 /* Clear all of them */
201 Irp
->MdlAddress
= Mdl
->Next
;
205 /* Check if the IRP has system buffer */
206 if (Irp
->Flags
& IRP_DEALLOCATE_BUFFER
)
208 /* Free the buffer */
209 ExFreePoolWithTag(Irp
->AssociatedIrp
.SystemBuffer
, TAG_SYS_BUF
);
212 /* Check if this IRP has a user event, a file object, and is async */
213 if ((Irp
->UserEvent
) &&
214 !(Irp
->Flags
& IRP_SYNCHRONOUS_API
) &&
217 /* Dereference the User Event */
218 ObDereferenceObject(Irp
->UserEvent
);
221 /* Check if we have a file object and this isn't a create operation */
222 if ((FileObject
) && !(Irp
->Flags
& IRP_CREATE_OPERATION
))
224 /* Dereference the file object */
225 ObDereferenceObject(FileObject
);
234 IopCompleteRequest(IN PKAPC Apc
,
235 IN PKNORMAL_ROUTINE
* NormalRoutine
,
236 IN PVOID
* NormalContext
,
237 IN PVOID
* SystemArgument1
,
238 IN PVOID
* SystemArgument2
)
240 PFILE_OBJECT FileObject
;
243 PVOID Port
= NULL
, Key
= NULL
;
244 BOOLEAN SignaledCreateRequest
= FALSE
;
246 /* Get data from the APC */
247 FileObject
= (PFILE_OBJECT
)*SystemArgument1
;
248 Irp
= CONTAINING_RECORD(Apc
, IRP
, Tail
.Apc
);
249 IOTRACE(IO_IRP_DEBUG
,
250 "%s - Completing IRP %p for %p\n",
256 ASSERT(Irp
->IoStatus
.Status
!= (NTSTATUS
)0xFFFFFFFF);
258 /* Check if we have a file object */
259 if (*SystemArgument2
)
261 /* Check if we're reparsing */
262 if ((Irp
->IoStatus
.Status
== STATUS_REPARSE
) &&
263 (Irp
->IoStatus
.Information
== IO_REPARSE_TAG_MOUNT_POINT
))
265 /* We should never get this yet */
266 DPRINT1("Reparse support not yet present!\n");
271 /* Handle Buffered case first */
272 if (Irp
->Flags
& IRP_BUFFERED_IO
)
274 /* Check if we have an input buffer and if we succeeded */
275 if ((Irp
->Flags
& IRP_INPUT_OPERATION
) &&
276 (Irp
->IoStatus
.Status
!= STATUS_VERIFY_REQUIRED
) &&
277 !(NT_ERROR(Irp
->IoStatus
.Status
)))
279 /* Copy the buffer back to the user */
280 RtlCopyMemory(Irp
->UserBuffer
,
281 Irp
->AssociatedIrp
.SystemBuffer
,
282 Irp
->IoStatus
.Information
);
285 /* Also check if we should de-allocate it */
286 if (Irp
->Flags
& IRP_DEALLOCATE_BUFFER
)
289 ExFreePool(Irp
->AssociatedIrp
.SystemBuffer
);
293 /* Now we got rid of these two... */
294 Irp
->Flags
&= ~(IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
);
296 /* Check if there's an MDL */
297 for (Mdl
= Irp
->MdlAddress
; Mdl
; Mdl
= NextMdl
)
305 Irp
->MdlAddress
= NULL
;
308 * Check if either the request was completed without any errors
309 * (but warnings are OK!), or if it was completed with an error, but
310 * did return from a pending I/O Operation and is not synchronous.
312 if (!(NT_ERROR(Irp
->IoStatus
.Status
)) ||
313 (NT_ERROR(Irp
->IoStatus
.Status
) &&
314 (Irp
->PendingReturned
) &&
315 !(IsIrpSynchronous(Irp
, FileObject
))))
317 /* Get any information we need from the FO before we kill it */
318 if ((FileObject
) && (FileObject
->CompletionContext
))
320 /* Save Completion Data */
321 Port
= FileObject
->CompletionContext
->Port
;
322 Key
= FileObject
->CompletionContext
->Key
;
325 /* Use SEH to make sure we don't write somewhere invalid */
328 /* Save the IOSB Information */
329 *Irp
->UserIosb
= Irp
->IoStatus
;
331 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
333 /* Ignore any error */
337 /* Check if we have an event or a file object */
340 /* At the very least, this is a PKEVENT, so signal it always */
341 KeSetEvent(Irp
->UserEvent
, 0, FALSE
);
343 /* Check if we also have a File Object */
346 /* Check if this is an Asynch API */
347 if (!(Irp
->Flags
& IRP_SYNCHRONOUS_API
))
349 /* Dereference the event */
350 ObDereferenceObject(Irp
->UserEvent
);
354 * Now, if this is a Synch I/O File Object, then this event is
355 * NOT an actual Executive Event, so we won't dereference it,
356 * and instead, we will signal the File Object
358 if ((FileObject
->Flags
& FO_SYNCHRONOUS_IO
) &&
359 !(Irp
->Flags
& IRP_OB_QUERY_NAME
))
361 /* Signal the file object and set the status */
362 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
363 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
367 * This could also be a create operation, in which case we want
368 * to make sure there's no APC fired.
370 if (Irp
->Flags
& IRP_CREATE_OPERATION
)
372 /* Clear the APC Routine and remember this */
373 Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
= NULL
;
374 SignaledCreateRequest
= TRUE
;
380 /* Signal the file object and set the status */
381 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
382 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
385 * This could also be a create operation, in which case we want
386 * to make sure there's no APC fired.
388 if (Irp
->Flags
& IRP_CREATE_OPERATION
)
390 /* Clear the APC Routine and remember this */
391 Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
= NULL
;
392 SignaledCreateRequest
= TRUE
;
396 /* Now that we've signaled the events, de-associate the IRP */
397 IopUnQueueIrpFromThread(Irp
);
399 /* Now check if a User APC Routine was requested */
400 if (Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
)
403 KeInitializeApc(&Irp
->Tail
.Apc
,
404 KeGetCurrentThread(),
405 CurrentApcEnvironment
,
407 IopAbortIrpKernelApc
,
408 (PKNORMAL_ROUTINE
)Irp
->
409 Overlay
.AsynchronousParameters
.UserApcRoutine
,
412 Overlay
.AsynchronousParameters
.UserApcContext
);
415 KeInsertQueueApc(&Irp
->Tail
.Apc
, Irp
->UserIosb
, NULL
, 2);
418 (Irp
->Overlay
.AsynchronousParameters
.UserApcContext
))
420 /* We have an I/O Completion setup... create the special Overlay */
421 Irp
->Tail
.CompletionKey
= Key
;
422 Irp
->Tail
.Overlay
.PacketType
= IopCompletionPacketIrp
;
423 KeInsertQueue(Port
, &Irp
->Tail
.Overlay
.ListEntry
);
427 /* Free the IRP since we don't need it anymore */
431 /* Check if we have a file object that wasn't part of a create */
432 if ((FileObject
) && !(SignaledCreateRequest
))
434 /* Dereference it, since it's not needed anymore either */
435 ObDereferenceObjectDeferDelete(FileObject
);
441 * Either we didn't return from the request, or we did return but this
442 * request was synchronous.
444 if ((Irp
->PendingReturned
) && (FileObject
))
446 /* So we did return with a synch operation, was it the IRP? */
447 if (Irp
->Flags
& IRP_SYNCHRONOUS_API
)
449 /* Yes, this IRP was synchronous, so return the I/O Status */
450 *Irp
->UserIosb
= Irp
->IoStatus
;
452 /* Now check if the user gave an event */
456 KeSetEvent(Irp
->UserEvent
, 0, FALSE
);
460 /* No event was given, so signal the FO instead */
461 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
467 * It's not the IRP that was synchronous, it was the FO
468 * that was opened this way. Signal its event.
470 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
471 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
475 /* Now that we got here, we do this for incomplete I/Os as well */
476 if ((FileObject
) && !(Irp
->Flags
& IRP_CREATE_OPERATION
))
478 /* Dereference the File Object unless this was a create */
479 ObDereferenceObjectDeferDelete(FileObject
);
483 * Check if this was an Executive Event (remember that we know this
484 * by checking if the IRP is synchronous)
486 if ((Irp
->UserEvent
) &&
488 !(Irp
->Flags
& IRP_SYNCHRONOUS_API
))
490 /* This isn't a PKEVENT, so dereference it */
491 ObDereferenceObject(Irp
->UserEvent
);
494 /* Now that we've signaled the events, de-associate the IRP */
495 IopUnQueueIrpFromThread(Irp
);
497 /* Free the IRP as well */
502 /* FUNCTIONS *****************************************************************/
509 IoAllocateIrp(IN CCHAR StackSize
,
510 IN BOOLEAN ChargeQuota
)
513 USHORT Size
= IoSizeOfIrp(StackSize
);
516 PNPAGED_LOOKASIDE_LIST List
= NULL
;
517 PP_NPAGED_LOOKASIDE_NUMBER ListType
= LookasideSmallIrpList
;
519 /* Set Charge Quota Flag */
520 if (ChargeQuota
) Flags
|= IRP_QUOTA_CHARGED
;
522 /* FIXME: Implement Lookaside Floats */
524 /* Figure out which Lookaside List to use */
525 if ((StackSize
<= 8) && (ChargeQuota
== FALSE
))
527 /* Set Fixed Size Flag */
528 Flags
= IRP_ALLOCATED_FIXED_SIZE
;
530 /* See if we should use big list */
533 Size
= IoSizeOfIrp(8);
534 ListType
= LookasideLargeIrpList
;
538 Prcb
= KeGetCurrentPrcb();
540 /* Get the P List First */
541 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].P
;
543 /* Attempt allocation */
544 List
->L
.TotalAllocates
++;
545 Irp
= (PIRP
)InterlockedPopEntrySList(&List
->L
.ListHead
);
547 /* Check if the P List failed */
550 /* Let the balancer know */
551 List
->L
.AllocateMisses
++;
554 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].L
;
555 List
->L
.TotalAllocates
++;
556 Irp
= (PIRP
)InterlockedPopEntrySList(&List
->L
.ListHead
);
560 /* Check if we have to use the pool */
563 /* Did we try lookaside and fail? */
564 if (Flags
& IRP_ALLOCATED_FIXED_SIZE
) List
->L
.AllocateMisses
++;
566 /* Check if we should charge quota */
569 /* Irp = ExAllocatePoolWithQuotaTag(NonPagedPool, Size, TAG_IRP); */
571 Irp
= ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_IRP
);
575 /* Allocate the IRP With no Quota charge */
576 Irp
= ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_IRP
);
579 /* Make sure it was sucessful */
580 if (!Irp
) return(NULL
);
584 /* In this case there is no charge quota */
585 Flags
&= ~IRP_QUOTA_CHARGED
;
588 /* Now Initialize it */
589 IoInitializeIrp(Irp
, Size
, StackSize
);
591 /* Set the Allocation Flags */
592 Irp
->AllocationFlags
= Flags
;
595 IOTRACE(IO_IRP_DEBUG
,
596 "%s - Allocated IRP %p with allocation flags %lx\n",
608 IoBuildAsynchronousFsdRequest(IN ULONG MajorFunction
,
609 IN PDEVICE_OBJECT DeviceObject
,
612 IN PLARGE_INTEGER StartingOffset
,
613 IN PIO_STATUS_BLOCK IoStatusBlock
)
616 PIO_STACK_LOCATION StackPtr
;
619 Irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
620 if (!Irp
) return NULL
;
623 StackPtr
= IoGetNextIrpStackLocation(Irp
);
625 /* Write the Major function and then deal with it */
626 StackPtr
->MajorFunction
= (UCHAR
)MajorFunction
;
628 /* Do not handle the following here */
629 if ((MajorFunction
!= IRP_MJ_FLUSH_BUFFERS
) &&
630 (MajorFunction
!= IRP_MJ_SHUTDOWN
) &&
631 (MajorFunction
!= IRP_MJ_PNP
) &&
632 (MajorFunction
!= IRP_MJ_POWER
))
634 /* Check if this is Buffered IO */
635 if (DeviceObject
->Flags
& DO_BUFFERED_IO
)
637 /* Allocate the System Buffer */
638 Irp
->AssociatedIrp
.SystemBuffer
=
639 ExAllocatePoolWithTag(NonPagedPool
, Length
, TAG_SYS_BUF
);
640 if (!Irp
->AssociatedIrp
.SystemBuffer
)
642 /* Free the IRP and fail */
648 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
650 /* Handle special IRP_MJ_WRITE Case */
651 if (MajorFunction
== IRP_MJ_WRITE
)
653 /* Copy the buffer data */
654 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
, Buffer
, Length
);
658 /* Set the Input Operation flag and set this as a User Buffer */
659 Irp
->Flags
|= IRP_INPUT_OPERATION
;
660 Irp
->UserBuffer
= Buffer
;
663 else if (DeviceObject
->Flags
& DO_DIRECT_IO
)
665 /* Use an MDL for Direct I/O */
666 Irp
->MdlAddress
= IoAllocateMdl(Buffer
,
671 if (!Irp
->MdlAddress
)
673 /* Free the IRP and fail */
682 MmProbeAndLockPages(Irp
->MdlAddress
,
684 MajorFunction
== IRP_MJ_READ
?
685 IoWriteAccess
: IoReadAccess
);
687 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
689 /* Free the IRP and its MDL */
690 IoFreeMdl(Irp
->MdlAddress
);
696 /* This is how we know if we failed during the probe */
697 if (!Irp
) return NULL
;
701 /* Neither, use the buffer */
702 Irp
->UserBuffer
= Buffer
;
705 /* Check if this is a read */
706 if (MajorFunction
== IRP_MJ_READ
)
708 /* Set the parameters for a read */
709 StackPtr
->Parameters
.Read
.Length
= Length
;
710 StackPtr
->Parameters
.Read
.ByteOffset
= *StartingOffset
;
712 else if (MajorFunction
== IRP_MJ_WRITE
)
714 /* Otherwise, set write parameters */
715 StackPtr
->Parameters
.Write
.Length
= Length
;
716 StackPtr
->Parameters
.Write
.ByteOffset
= *StartingOffset
;
720 /* Set the Current Thread and IOSB */
721 Irp
->UserIosb
= IoStatusBlock
;
722 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
725 IOTRACE(IO_IRP_DEBUG
,
726 "%s - Built IRP %p with Major, Buffer, DO %lx %p %p\n",
740 IoBuildDeviceIoControlRequest(IN ULONG IoControlCode
,
741 IN PDEVICE_OBJECT DeviceObject
,
742 IN PVOID InputBuffer
,
743 IN ULONG InputBufferLength
,
744 IN PVOID OutputBuffer
,
745 IN ULONG OutputBufferLength
,
746 IN BOOLEAN InternalDeviceIoControl
,
748 IN PIO_STATUS_BLOCK IoStatusBlock
)
751 PIO_STACK_LOCATION StackPtr
;
755 Irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
756 if (!Irp
) return NULL
;
759 StackPtr
= IoGetNextIrpStackLocation(Irp
);
761 /* Set the DevCtl Type */
762 StackPtr
->MajorFunction
= InternalDeviceIoControl
?
763 IRP_MJ_INTERNAL_DEVICE_CONTROL
:
764 IRP_MJ_DEVICE_CONTROL
;
766 /* Set the IOCTL Data */
767 StackPtr
->Parameters
.DeviceIoControl
.IoControlCode
= IoControlCode
;
768 StackPtr
->Parameters
.DeviceIoControl
.InputBufferLength
= InputBufferLength
;
769 StackPtr
->Parameters
.DeviceIoControl
.OutputBufferLength
=
772 /* Handle the Methods */
773 switch (IO_METHOD_FROM_CTL_CODE(IoControlCode
))
776 case METHOD_BUFFERED
:
778 /* Select the right Buffer Length */
779 BufferLength
= InputBufferLength
> OutputBufferLength
?
780 InputBufferLength
: OutputBufferLength
;
782 /* Make sure there is one */
785 /* Allocate the System Buffer */
786 Irp
->AssociatedIrp
.SystemBuffer
=
787 ExAllocatePoolWithTag(NonPagedPool
,
790 if (!Irp
->AssociatedIrp
.SystemBuffer
)
792 /* Free the IRP and fail */
797 /* Check if we got a buffer */
800 /* Copy into the System Buffer */
801 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
806 /* Write the flags */
807 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
808 if (OutputBuffer
) Irp
->Flags
|= IRP_INPUT_OPERATION
;
810 /* Save the Buffer */
811 Irp
->UserBuffer
= OutputBuffer
;
815 /* Clear the Flags and Buffer */
817 Irp
->UserBuffer
= NULL
;
822 case METHOD_IN_DIRECT
:
823 case METHOD_OUT_DIRECT
:
825 /* Check if we got an input buffer */
828 /* Allocate the System Buffer */
829 Irp
->AssociatedIrp
.SystemBuffer
=
830 ExAllocatePoolWithTag(NonPagedPool
,
833 if (!Irp
->AssociatedIrp
.SystemBuffer
)
835 /* Free the IRP and fail */
840 /* Copy into the System Buffer */
841 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
845 /* Write the flags */
846 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
850 /* Clear the flags */
854 /* Check if we got an output buffer */
857 /* Allocate the System Buffer */
858 Irp
->MdlAddress
= IoAllocateMdl(OutputBuffer
,
863 if (!Irp
->MdlAddress
)
865 /* Free the IRP and fail */
874 MmProbeAndLockPages(Irp
->MdlAddress
,
876 IO_METHOD_FROM_CTL_CODE(IoControlCode
) ==
878 IoReadAccess
: IoWriteAccess
);
880 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
883 IoFreeMdl(Irp
->MdlAddress
);
885 /* Free the input buffer and IRP */
886 if (InputBuffer
) ExFreePool(Irp
->AssociatedIrp
.SystemBuffer
);
892 /* This is how we know if probing failed */
893 if (!Irp
) return NULL
;
899 /* Just save the Buffer */
900 Irp
->UserBuffer
= OutputBuffer
;
901 StackPtr
->Parameters
.DeviceIoControl
.Type3InputBuffer
= InputBuffer
;
904 /* Now write the Event and IoSB */
905 Irp
->UserIosb
= IoStatusBlock
;
906 Irp
->UserEvent
= Event
;
908 /* Sync IRPs are queued to requestor thread's irp cancel/cleanup list */
909 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
910 IoQueueThreadIrp(Irp
);
913 IOTRACE(IO_IRP_DEBUG
,
914 "%s - Built IRP %p with IOCTL, Buffers, DO %lx %p %p %p\n",
929 IoBuildSynchronousFsdRequest(IN ULONG MajorFunction
,
930 IN PDEVICE_OBJECT DeviceObject
,
933 IN PLARGE_INTEGER StartingOffset
,
935 IN PIO_STATUS_BLOCK IoStatusBlock
)
939 /* Do the big work to set up the IRP */
940 Irp
= IoBuildAsynchronousFsdRequest(MajorFunction
,
946 if (!Irp
) return NULL
;
948 /* Set the Event which makes it Syncronous */
949 Irp
->UserEvent
= Event
;
951 /* Sync IRPs are queued to requestor thread's irp cancel/cleanup list */
952 IoQueueThreadIrp(Irp
);
961 IoCancelIrp(IN PIRP Irp
)
965 PDRIVER_CANCEL CancelRoutine
;
966 IOTRACE(IO_IRP_DEBUG
,
967 "%s - Canceling IRP %p\n",
970 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
971 IrqlAtEntry
= KeGetCurrentIrql();
973 /* Acquire the cancel lock and cancel the IRP */
974 IoAcquireCancelSpinLock(&OldIrql
);
977 /* Clear the cancel routine and get the old one */
978 CancelRoutine
= (PVOID
)IoSetCancelRoutine(Irp
, NULL
);
981 /* We had a routine, make sure the IRP isn't completed */
982 if (Irp
->CurrentLocation
> (Irp
->StackCount
+ 1))
984 /* It is, bugcheck */
985 KeBugCheckEx(CANCEL_STATE_IN_COMPLETED_IRP
,
992 /* Set the cancel IRQL And call the routine */
993 Irp
->CancelIrql
= OldIrql
;
994 CancelRoutine(IoGetCurrentIrpStackLocation(Irp
)->DeviceObject
, Irp
);
995 ASSERT(IrqlAtEntry
== KeGetCurrentIrql());
999 /* Otherwise, release the cancel lock and fail */
1000 IoReleaseCancelSpinLock(OldIrql
);
1009 IoCancelThreadIo(IN PETHREAD Thread
)
1012 ULONG Retries
= 3000;
1013 LARGE_INTEGER Interval
;
1014 PLIST_ENTRY ListHead
, NextEntry
;
1016 IOTRACE(IO_IRP_DEBUG
,
1017 "%s - Canceling IRPs for Thread %p\n",
1021 /* Raise to APC to protect the IrpList */
1022 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1024 /* Start by cancelling all the IRPs in the current thread queue. */
1025 ListHead
= &Thread
->IrpList
;
1026 NextEntry
= ListHead
->Flink
;
1027 while (ListHead
!= NextEntry
)
1030 Irp
= CONTAINING_RECORD(NextEntry
, IRP
, ThreadListEntry
);
1035 /* Move to the next entry */
1036 NextEntry
= NextEntry
->Flink
;
1039 /* Wait 100 milliseconds */
1040 Interval
.QuadPart
= -1000000;
1042 /* Wait till all the IRPs are completed or cancelled. */
1043 while (!IsListEmpty(&Thread
->IrpList
))
1045 /* Now we can lower */
1046 KeLowerIrql(OldIrql
);
1048 /* Wait a short while and then look if all our IRPs were completed. */
1049 KeDelayExecutionThread(KernelMode
, FALSE
, &Interval
);
1052 * Don't stay here forever if some broken driver doesn't complete
1057 /* Print out a message and remove the IRP */
1058 DPRINT1("Broken driver did not complete!\n");
1059 IopRemoveThreadIrp();
1062 /* Raise the IRQL Again */
1063 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1066 /* We're done, lower the IRQL */
1067 KeLowerIrql(OldIrql
);
1075 IoCallDriver(IN PDEVICE_OBJECT DeviceObject
,
1079 return IofCallDriver(DeviceObject
, Irp
);
1087 IoCompleteRequest(IN PIRP Irp
,
1088 IN CCHAR PriorityBoost
)
1090 /* Call the fastcall */
1091 IofCompleteRequest(Irp
, PriorityBoost
);
1099 IoEnqueueIrp(IN PIRP Irp
)
1101 /* This is the same as calling IoQueueThreadIrp */
1102 IoQueueThreadIrp(Irp
);
1110 IofCallDriver(IN PDEVICE_OBJECT DeviceObject
,
1113 PDRIVER_OBJECT DriverObject
;
1114 PIO_STACK_LOCATION StackPtr
;
1116 /* Get the Driver Object */
1117 DriverObject
= DeviceObject
->DriverObject
;
1119 /* Decrease the current location and check if */
1120 Irp
->CurrentLocation
--;
1121 if (Irp
->CurrentLocation
<= 0)
1123 /* This IRP ran out of stack, bugcheck */
1124 KeBugCheckEx(NO_MORE_IRP_STACK_LOCATIONS
, (ULONG_PTR
)Irp
, 0, 0, 0);
1127 /* Now update the stack location */
1128 StackPtr
= IoGetNextIrpStackLocation(Irp
);
1129 Irp
->Tail
.Overlay
.CurrentStackLocation
= StackPtr
;
1131 /* Get the Device Object */
1132 StackPtr
->DeviceObject
= DeviceObject
;
1135 return DriverObject
->MajorFunction
[StackPtr
->MajorFunction
](DeviceObject
,
1141 IopClearStackLocation(IN PIO_STACK_LOCATION IoStackLocation
)
1143 IoStackLocation
->MinorFunction
= 0;
1144 IoStackLocation
->Flags
= 0;
1145 IoStackLocation
->Control
&= SL_ERROR_RETURNED
;
1146 IoStackLocation
->Parameters
.Others
.Argument1
= 0;
1147 IoStackLocation
->Parameters
.Others
.Argument2
= 0;
1148 IoStackLocation
->Parameters
.Others
.Argument3
= 0;
1149 IoStackLocation
->FileObject
= NULL
;
1157 IofCompleteRequest(IN PIRP Irp
,
1158 IN CCHAR PriorityBoost
)
1160 PIO_STACK_LOCATION StackPtr
, LastStackPtr
;
1161 PDEVICE_OBJECT DeviceObject
;
1162 PFILE_OBJECT FileObject
;
1169 NTSTATUS ErrorCode
= STATUS_SUCCESS
;
1170 IOTRACE(IO_IRP_DEBUG
,
1171 "%s - Completing IRP %p\n",
1175 /* Make sure this IRP isn't getting completed twice or is invalid */
1176 if ((Irp
->CurrentLocation
) > (Irp
->StackCount
+ 1))
1179 KeBugCheckEx(MULTIPLE_IRP_COMPLETE_REQUESTS
, (ULONG_PTR
)Irp
, 0, 0, 0);
1182 /* Some sanity checks */
1183 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1184 ASSERT(!Irp
->CancelRoutine
);
1185 ASSERT(Irp
->IoStatus
.Status
!= STATUS_PENDING
);
1186 ASSERT(Irp
->IoStatus
.Status
!= (NTSTATUS
)0xFFFFFFFF);
1188 /* Get the last stack */
1189 LastStackPtr
= (PIO_STACK_LOCATION
)(Irp
+ 1);
1190 if (LastStackPtr
->Control
& SL_ERROR_RETURNED
)
1192 /* Get the error code */
1193 ErrorCode
= PtrToUlong(LastStackPtr
->Parameters
.Others
.Argument4
);
1196 /* Get the Current Stack and skip it */
1197 StackPtr
= IoGetCurrentIrpStackLocation(Irp
);
1198 IoSkipCurrentIrpStackLocation(Irp
);
1200 /* Loop the Stacks and complete the IRPs */
1203 /* Set Pending Returned */
1204 Irp
->PendingReturned
= StackPtr
->Control
& SL_PENDING_RETURNED
;
1206 /* Check if we failed */
1207 if (!NT_SUCCESS(Irp
->IoStatus
.Status
))
1209 /* Check if it was changed by a completion routine */
1210 if (Irp
->IoStatus
.Status
!= ErrorCode
)
1212 /* Update the error for the current stack */
1213 ErrorCode
= Irp
->IoStatus
.Status
;
1214 StackPtr
->Control
|= SL_ERROR_RETURNED
;
1215 LastStackPtr
->Parameters
.Others
.Argument4
= UlongToPtr(ErrorCode
);
1216 LastStackPtr
->Control
|= SL_ERROR_RETURNED
;
1220 /* Check if there is a Completion Routine to Call */
1221 if ((NT_SUCCESS(Irp
->IoStatus
.Status
) &&
1222 (StackPtr
->Control
& SL_INVOKE_ON_SUCCESS
)) ||
1223 (!NT_SUCCESS(Irp
->IoStatus
.Status
) &&
1224 (StackPtr
->Control
& SL_INVOKE_ON_ERROR
)) ||
1226 (StackPtr
->Control
& SL_INVOKE_ON_CANCEL
)))
1228 /* Clear the stack location */
1229 IopClearStackLocation(StackPtr
);
1231 /* Check for highest-level device completion routines */
1232 if (Irp
->CurrentLocation
== (Irp
->StackCount
+ 1))
1234 /* Clear the DO, since the current stack location is invalid */
1235 DeviceObject
= NULL
;
1239 /* Otherwise, return the real one */
1240 DeviceObject
= IoGetCurrentIrpStackLocation(Irp
)->DeviceObject
;
1243 /* Call the completion routine */
1244 Status
= StackPtr
->CompletionRoutine(DeviceObject
,
1248 /* Don't touch the Packet in this case, since it might be gone! */
1249 if (Status
== STATUS_MORE_PROCESSING_REQUIRED
) return;
1253 /* Otherwise, check if this is a completed IRP */
1254 if ((Irp
->CurrentLocation
<= Irp
->StackCount
) &&
1255 (Irp
->PendingReturned
))
1257 /* Mark it as pending */
1258 IoMarkIrpPending(Irp
);
1261 /* Clear the stack location */
1262 IopClearStackLocation(StackPtr
);
1265 /* Move to next stack location and pointer */
1266 IoSkipCurrentIrpStackLocation(Irp
);
1268 } while (Irp
->CurrentLocation
<= (Irp
->StackCount
+ 1));
1270 /* Check if the IRP is an associated IRP */
1271 if (Irp
->Flags
& IRP_ASSOCIATED_IRP
)
1273 /* Get the master IRP and count */
1274 MasterIrp
= Irp
->AssociatedIrp
.MasterIrp
;
1275 MasterCount
= InterlockedDecrement(&MasterIrp
->AssociatedIrp
.IrpCount
);
1278 for (Mdl
= Irp
->MdlAddress
; Mdl
; Mdl
= NextMdl
)
1280 /* Go to the next one */
1281 NextMdl
= Mdl
->Next
;
1285 /* Free the IRP itself */
1288 /* Complete the Master IRP */
1289 if (!MasterCount
) IofCompleteRequest(MasterIrp
, PriorityBoost
);
1293 /* We don't support this yet */
1294 ASSERT(Irp
->IoStatus
.Status
!= STATUS_REPARSE
);
1296 /* Check if we have an auxiliary buffer */
1297 if (Irp
->Tail
.Overlay
.AuxiliaryBuffer
)
1300 ExFreePool(Irp
->Tail
.Overlay
.AuxiliaryBuffer
);
1301 Irp
->Tail
.Overlay
.AuxiliaryBuffer
= NULL
;
1304 /* Check if this is a Paging I/O or Close Operation */
1305 if (Irp
->Flags
& (IRP_PAGING_IO
| IRP_CLOSE_OPERATION
))
1307 /* Handle a Close Operation or Sync Paging I/O */
1308 if (Irp
->Flags
& (IRP_SYNCHRONOUS_PAGING_IO
| IRP_CLOSE_OPERATION
))
1310 /* Set the I/O Status and Signal the Event */
1311 Flags
= Irp
->Flags
& (IRP_SYNCHRONOUS_PAGING_IO
| IRP_PAGING_IO
);
1312 *Irp
->UserIosb
= Irp
->IoStatus
;
1313 KeSetEvent(Irp
->UserEvent
, PriorityBoost
, FALSE
);
1315 /* Free the IRP for a Paging I/O Only, Close is handled by us */
1316 if (Flags
) IoFreeIrp(Irp
);
1322 KeInitializeApc(&Irp
->Tail
.Apc
1323 &Irp
->Tail
.Overlay
.Thread
->Tcb
,
1324 Irp
->ApcEnvironment
,
1325 IopCompletePageWrite
,
1330 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1335 /* Not implemented yet. */
1336 DPRINT1("Not supported!\n");
1341 /* Get out of here */
1345 /* Unlock MDL Pages, page 167. */
1346 Mdl
= Irp
->MdlAddress
;
1353 /* Check if we should exit because of a Deferred I/O (page 168) */
1354 if ((Irp
->Flags
& IRP_DEFER_IO_COMPLETION
) && !(Irp
->PendingReturned
))
1357 * Return without queuing the completion APC, since the caller will
1358 * take care of doing its own optimized completion at PASSIVE_LEVEL.
1363 /* Get the thread and file object */
1364 Thread
= Irp
->Tail
.Overlay
.Thread
;
1365 FileObject
= Irp
->Tail
.Overlay
.OriginalFileObject
;
1367 /* Make sure the IRP isn't canceled */
1370 /* Initialize the APC */
1371 KeInitializeApc(&Irp
->Tail
.Apc
,
1373 Irp
->ApcEnvironment
,
1381 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1383 NULL
, /* This is used for REPARSE stuff */
1388 /* The IRP just got canceled... does a thread still own it? */
1389 Thread
= Irp
->Tail
.Overlay
.Thread
;
1392 /* Yes! There is still hope! Initialize the APC */
1393 KeInitializeApc(&Irp
->Tail
.Apc
,
1395 Irp
->ApcEnvironment
,
1403 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1405 NULL
, /* This is used for REPARSE stuff */
1410 /* Nothing left for us to do, kill it */
1411 ASSERT(Irp
->Cancel
);
1412 IopCleanupIrp(Irp
, FileObject
);
1419 IopSynchronousCompletion(IN PDEVICE_OBJECT DeviceObject
,
1423 if (Irp
->PendingReturned
)
1424 KeSetEvent((PKEVENT
)Context
, IO_NO_INCREMENT
, FALSE
);
1425 return STATUS_MORE_PROCESSING_REQUIRED
;
1433 IoForwardIrpSynchronously(IN PDEVICE_OBJECT DeviceObject
,
1439 /* Check if next stack location is available */
1440 if (Irp
->CurrentLocation
< Irp
->StackCount
)
1442 /* No more stack location */
1446 /* Initialize event */
1447 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
1449 /* Copy stack location for next driver */
1450 IoCopyCurrentIrpStackLocationToNext(Irp
);
1452 /* Set a completion routine, which will signal the event */
1453 IoSetCompletionRoutine(Irp
, IopSynchronousCompletion
, &Event
, TRUE
, TRUE
, TRUE
);
1455 /* Call next driver */
1456 Status
= IoCallDriver(DeviceObject
, Irp
);
1458 /* Check if irp is pending */
1459 if (Status
== STATUS_PENDING
)
1461 /* Yes, wait for its completion */
1462 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
1465 /* Return success */
1474 IoFreeIrp(IN PIRP Irp
)
1476 PNPAGED_LOOKASIDE_LIST List
;
1477 PP_NPAGED_LOOKASIDE_NUMBER ListType
= LookasideSmallIrpList
;
1479 IOTRACE(IO_IRP_DEBUG
,
1480 "%s - Freeing IRPs %p\n",
1484 /* Make sure the Thread IRP list is empty and that it OK to free it */
1485 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1486 ASSERT(IsListEmpty(&Irp
->ThreadListEntry
));
1487 ASSERT(Irp
->CurrentLocation
>= Irp
->StackCount
);
1489 /* If this was a pool alloc, free it with the pool */
1490 if (!(Irp
->AllocationFlags
& IRP_ALLOCATED_FIXED_SIZE
))
1493 ExFreePoolWithTag(Irp
, TAG_IRP
);
1497 /* Check if this was a Big IRP */
1498 if (Irp
->StackCount
!= 1) ListType
= LookasideLargeIrpList
;
1501 Prcb
= KeGetCurrentPrcb();
1503 /* Use the P List */
1504 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].P
;
1505 List
->L
.TotalFrees
++;
1507 /* Check if the Free was within the Depth or not */
1508 if (ExQueryDepthSList(&List
->L
.ListHead
) >= List
->L
.Depth
)
1510 /* Let the balancer know */
1511 List
->L
.FreeMisses
++;
1513 /* Use the L List */
1514 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].L
;
1515 List
->L
.TotalFrees
++;
1517 /* Check if the Free was within the Depth or not */
1518 if (ExQueryDepthSList(&List
->L
.ListHead
) >= List
->L
.Depth
)
1520 /* All lists failed, use the pool */
1521 List
->L
.FreeMisses
++;
1522 ExFreePoolWithTag(Irp
, TAG_IRP
);
1527 /* The free was within the Depth */
1530 InterlockedPushEntrySList(&List
->L
.ListHead
,
1531 (PSINGLE_LIST_ENTRY
)Irp
);
1541 IoGetPagingIoPriority(IN PIRP Irp
)
1543 IO_PAGING_PRIORITY Priority
;
1549 /* Check what priority it has */
1550 if (Flags
& 0x8000) // FIXME: Undocumented flag
1553 Priority
= IoPagingPriorityHigh
;
1555 else if (Flags
& IRP_PAGING_IO
)
1557 /* Normal priority */
1558 Priority
= IoPagingPriorityNormal
;
1562 /* Invalid -- not a paging IRP */
1563 Priority
= IoPagingPriorityInvalid
;
1566 /* Return the priority */
1575 IoGetRequestorProcess(IN PIRP Irp
)
1577 /* Return the requestor process */
1578 return Irp
->Tail
.Overlay
.Thread
->ThreadsProcess
;
1586 IoGetRequestorProcessId(IN PIRP Irp
)
1588 /* Return the requestor process' id */
1589 return PtrToUlong(IoGetRequestorProcess(Irp
)->UniqueProcessId
);
1597 IoGetRequestorSessionId(IN PIRP Irp
,
1598 OUT PULONG pSessionId
)
1600 /* Return the session */
1601 *pSessionId
= IoGetRequestorProcess(Irp
)->Session
;
1602 return STATUS_SUCCESS
;
1610 IoGetTopLevelIrp(VOID
)
1612 /* Return the IRP */
1613 return (PIRP
)PsGetCurrentThread()->TopLevelIrp
;
1621 IoInitializeIrp(IN PIRP Irp
,
1622 IN USHORT PacketSize
,
1626 IOTRACE(IO_IRP_DEBUG
,
1627 "%s - Initializing IRP %p\n",
1630 RtlZeroMemory(Irp
, PacketSize
);
1632 /* Set the Header and other data */
1633 Irp
->Type
= IO_TYPE_IRP
;
1634 Irp
->Size
= PacketSize
;
1635 Irp
->StackCount
= StackSize
;
1636 Irp
->CurrentLocation
= StackSize
+ 1;
1637 Irp
->ApcEnvironment
= KeGetCurrentThread()->ApcStateIndex
;
1638 Irp
->Tail
.Overlay
.CurrentStackLocation
= (PIO_STACK_LOCATION
)(Irp
+ 1) + StackSize
;
1640 /* Initialize the Thread List */
1641 InitializeListHead(&Irp
->ThreadListEntry
);
1649 IoIsOperationSynchronous(IN PIRP Irp
)
1651 /* Check the flags */
1652 if (!(Irp
->Flags
& (IRP_PAGING_IO
| IRP_SYNCHRONOUS_PAGING_IO
)) &&
1653 ((Irp
->Flags
& IRP_SYNCHRONOUS_PAGING_IO
) ||
1654 (Irp
->Flags
& IRP_SYNCHRONOUS_API
) ||
1655 (IoGetCurrentIrpStackLocation(Irp
)->FileObject
->Flags
&
1656 FO_SYNCHRONOUS_IO
)))
1658 /* Synch API or Paging I/O is OK, as is Sync File I/O */
1662 /* Otherwise, it is an asynchronous operation. */
1671 IoIsValidNameGraftingBuffer(IN PIRP Irp
,
1672 IN PREPARSE_DATA_BUFFER ReparseBuffer
)
1683 IoMakeAssociatedIrp(IN PIRP Irp
,
1687 IOTRACE(IO_IRP_DEBUG
,
1688 "%s - Associating IRP %p\n",
1692 /* Allocate the IRP */
1693 AssocIrp
= IoAllocateIrp(StackSize
, FALSE
);
1694 if (!AssocIrp
) return NULL
;
1697 AssocIrp
->Flags
|= IRP_ASSOCIATED_IRP
;
1699 /* Set the Thread */
1700 AssocIrp
->Tail
.Overlay
.Thread
= Irp
->Tail
.Overlay
.Thread
;
1702 /* Associate them */
1703 AssocIrp
->AssociatedIrp
.MasterIrp
= Irp
;
1712 IoQueueThreadIrp(IN PIRP Irp
)
1714 IOTRACE(IO_IRP_DEBUG
,
1715 "%s - Queueing IRP %p\n",
1719 /* Use our inlined routine */
1720 IopQueueIrpToThread(Irp
);
1725 * Reference: Chris Cant's "Writing WDM Device Drivers"
1729 IoReuseIrp(IN OUT PIRP Irp
,
1732 UCHAR AllocationFlags
;
1733 IOTRACE(IO_IRP_DEBUG
,
1734 "%s - Reusing IRP %p\n",
1738 /* Make sure it's OK to reuse it */
1739 ASSERT(!Irp
->CancelRoutine
);
1740 ASSERT(IsListEmpty(&Irp
->ThreadListEntry
));
1742 /* Get the old flags */
1743 AllocationFlags
= Irp
->AllocationFlags
;
1745 /* Reinitialize the IRP */
1746 IoInitializeIrp(Irp
, Irp
->Size
, Irp
->StackCount
);
1748 /* Duplicate the data */
1749 Irp
->IoStatus
.Status
= Status
;
1750 Irp
->AllocationFlags
= AllocationFlags
;
1758 IoSetTopLevelIrp(IN PIRP Irp
)
1761 PsGetCurrentThread()->TopLevelIrp
= (ULONG_PTR
)Irp
;