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 ****************************************************************/
19 /* PRIVATE FUNCTIONS ********************************************************/
23 IopFreeIrpKernelApc(IN PKAPC Apc
,
24 IN PKNORMAL_ROUTINE
*NormalRoutine
,
25 IN PVOID
*NormalContext
,
26 IN PVOID
*SystemArgument1
,
27 IN PVOID
*SystemArgument2
)
30 IoFreeIrp(CONTAINING_RECORD(Apc
, IRP
, Tail
.Apc
));
35 IopAbortIrpKernelApc(IN PKAPC Apc
)
38 IoFreeIrp(CONTAINING_RECORD(Apc
, IRP
, Tail
.Apc
));
43 IopCleanupFailedIrp(IN PFILE_OBJECT FileObject
,
44 IN PKEVENT EventObject OPTIONAL
,
45 IN PVOID Buffer OPTIONAL
)
49 /* Dereference the event */
50 if (EventObject
) ObDereferenceObject(EventObject
);
52 /* Free a buffer, if any */
53 if (Buffer
) ExFreePool(Buffer
);
55 /* If this was a file opened for synch I/O, then unlock it */
56 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
) IopUnlockFileObject(FileObject
);
58 /* Now dereference it and return */
59 ObDereferenceObject(FileObject
);
60 return STATUS_INSUFFICIENT_RESOURCES
;
65 IopAbortInterruptedIrp(IN PKEVENT EventObject
,
73 /* Raise IRQL to APC */
74 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
76 /* Check if nobody completed it yet */
77 if (!KeReadStateEvent(EventObject
))
79 /* First, cancel it */
80 CancelResult
= IoCancelIrp(Irp
);
83 /* Check if we cancelled it */
86 /* Wait for the IRP to be cancelled */
87 Wait
.QuadPart
= -100000;
88 while (!KeReadStateEvent(EventObject
))
90 /* Delay indefintely */
91 KeDelayExecutionThread(KernelMode
, FALSE
, &Wait
);
96 /* No cancellation done, so wait for the I/O system to kill it */
97 KeWaitForSingleObject(EventObject
,
106 /* We got preempted, so give up */
107 KeLowerIrql(OldIrql
);
113 IopDisassociateThreadIrp(VOID
)
115 KIRQL OldIrql
, LockIrql
;
117 PLIST_ENTRY IrpEntry
;
118 PIO_ERROR_LOG_PACKET ErrorLogEntry
;
119 PDEVICE_OBJECT DeviceObject
= NULL
;
120 PIO_STACK_LOCATION IoStackLocation
;
122 /* First, raise to APC to protect IrpList */
123 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
125 /* Get the Thread and check the list */
126 IrpThread
= PsGetCurrentThread();
127 if (IsListEmpty(&IrpThread
->IrpList
))
129 /* It got completed now, so quit */
130 KeLowerIrql(OldIrql
);
134 /* Ensure no one will come disturb */
135 LockIrql
= KeAcquireQueuedSpinLock(LockQueueIoCompletionLock
);
137 /* Get the misbehaving IRP */
138 IrpEntry
= IrpThread
->IrpList
.Flink
;
139 IopDeadIrp
= 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 (IopDeadIrp
->CurrentLocation
== (IopDeadIrp
->StackCount
+ 2))
150 KeReleaseQueuedSpinLock(LockQueueIoCompletionLock
, LockIrql
);
151 KeLowerIrql(OldIrql
);
155 /* Disown the IRP! */
156 IopDeadIrp
->Tail
.Overlay
.Thread
= NULL
;
157 RemoveHeadList(&IrpThread
->IrpList
);
158 InitializeListHead(&IopDeadIrp
->ThreadListEntry
);
160 /* Get the stack location and check if it's valid */
161 IoStackLocation
= IoGetCurrentIrpStackLocation(IopDeadIrp
);
162 if (IopDeadIrp
->CurrentLocation
<= IopDeadIrp
->StackCount
)
164 /* Get the device object */
165 DeviceObject
= IoStackLocation
->DeviceObject
;
168 KeReleaseQueuedSpinLock(LockQueueIoCompletionLock
, LockIrql
);
169 /* Lower IRQL now, since we have the pointers we need */
170 KeLowerIrql(OldIrql
);
172 /* Check if we can send an Error Log Entry*/
175 /* Allocate an entry */
176 ErrorLogEntry
= IoAllocateErrorLogEntry(DeviceObject
,
177 sizeof(IO_ERROR_LOG_PACKET
));
180 /* Write the entry */
181 ErrorLogEntry
->ErrorCode
= IO_DRIVER_CANCEL_TIMEOUT
;
182 IoWriteErrorLogEntry(ErrorLogEntry
);
189 IopCleanupIrp(IN PIRP Irp
,
190 IN PFILE_OBJECT FileObject
)
193 IOTRACE(IO_IRP_DEBUG
,
194 "%s - Cleaning IRP %p for %p\n",
199 /* Check if there's an MDL */
200 while ((Mdl
= Irp
->MdlAddress
))
202 /* Clear all of them */
203 Irp
->MdlAddress
= Mdl
->Next
;
207 /* Check if the IRP has system buffer */
208 if (Irp
->Flags
& IRP_DEALLOCATE_BUFFER
)
210 /* Free the buffer */
211 ExFreePoolWithTag(Irp
->AssociatedIrp
.SystemBuffer
, TAG_SYS_BUF
);
214 /* Check if this IRP has a user event, a file object, and is async */
215 if ((Irp
->UserEvent
) &&
216 !(Irp
->Flags
& IRP_SYNCHRONOUS_API
) &&
219 /* Dereference the User Event */
220 ObDereferenceObject(Irp
->UserEvent
);
223 /* Check if we have a file object and this isn't a create operation */
224 if ((FileObject
) && !(Irp
->Flags
& IRP_CREATE_OPERATION
))
226 /* Dereference the file object */
227 ObDereferenceObject(FileObject
);
236 IopCompleteRequest(IN PKAPC Apc
,
237 IN PKNORMAL_ROUTINE
* NormalRoutine
,
238 IN PVOID
* NormalContext
,
239 IN PVOID
* SystemArgument1
,
240 IN PVOID
* SystemArgument2
)
242 PFILE_OBJECT FileObject
;
245 PVOID Port
= NULL
, Key
= NULL
;
246 BOOLEAN SignaledCreateRequest
= FALSE
;
248 /* Get data from the APC */
249 FileObject
= (PFILE_OBJECT
)*SystemArgument1
;
250 Irp
= CONTAINING_RECORD(Apc
, IRP
, Tail
.Apc
);
251 IOTRACE(IO_IRP_DEBUG
,
252 "%s - Completing IRP %p for %p\n",
258 ASSERT(Irp
->IoStatus
.Status
!= (NTSTATUS
)0xFFFFFFFF);
260 /* Check if we have a file object */
261 if (*SystemArgument2
)
263 /* Check if we're reparsing */
264 if ((Irp
->IoStatus
.Status
== STATUS_REPARSE
) &&
265 (Irp
->IoStatus
.Information
== IO_REPARSE_TAG_MOUNT_POINT
))
267 /* We should never get this yet */
268 UNIMPLEMENTED_DBGBREAK("Reparse support not yet present!\n");
273 /* Handle Buffered case first */
274 if (Irp
->Flags
& IRP_BUFFERED_IO
)
276 /* Check if we have an input buffer and if we succeeded */
277 if ((Irp
->Flags
& IRP_INPUT_OPERATION
) &&
278 (Irp
->IoStatus
.Status
!= STATUS_VERIFY_REQUIRED
) &&
279 !(NT_ERROR(Irp
->IoStatus
.Status
)))
281 /* Copy the buffer back to the user */
282 RtlCopyMemory(Irp
->UserBuffer
,
283 Irp
->AssociatedIrp
.SystemBuffer
,
284 Irp
->IoStatus
.Information
);
287 /* Also check if we should de-allocate it */
288 if (Irp
->Flags
& IRP_DEALLOCATE_BUFFER
)
291 ExFreePool(Irp
->AssociatedIrp
.SystemBuffer
);
295 /* Now we got rid of these two... */
296 Irp
->Flags
&= ~(IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
);
298 /* Check if there's an MDL */
299 for (Mdl
= Irp
->MdlAddress
; Mdl
; Mdl
= NextMdl
)
307 Irp
->MdlAddress
= NULL
;
310 * Check if either the request was completed without any errors
311 * (but warnings are OK!), or if it was completed with an error, but
312 * did return from a pending I/O Operation and is not synchronous.
314 if (!(NT_ERROR(Irp
->IoStatus
.Status
)) ||
315 (NT_ERROR(Irp
->IoStatus
.Status
) &&
316 (Irp
->PendingReturned
) &&
317 !(IsIrpSynchronous(Irp
, FileObject
))))
319 /* Get any information we need from the FO before we kill it */
320 if ((FileObject
) && (FileObject
->CompletionContext
))
322 /* Save Completion Data */
323 Port
= FileObject
->CompletionContext
->Port
;
324 Key
= FileObject
->CompletionContext
->Key
;
327 /* Use SEH to make sure we don't write somewhere invalid */
330 /* Save the IOSB Information */
331 *Irp
->UserIosb
= Irp
->IoStatus
;
333 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
335 /* Ignore any error */
339 /* Check if we have an event or a file object */
342 /* At the very least, this is a PKEVENT, so signal it always */
343 KeSetEvent(Irp
->UserEvent
, 0, FALSE
);
345 /* Check if we also have a File Object */
348 /* Check if this is an Asynch API */
349 if (!(Irp
->Flags
& IRP_SYNCHRONOUS_API
))
351 /* Dereference the event */
352 ObDereferenceObject(Irp
->UserEvent
);
356 * Now, if this is a Synch I/O File Object, then this event is
357 * NOT an actual Executive Event, so we won't dereference it,
358 * and instead, we will signal the File Object
360 if ((FileObject
->Flags
& FO_SYNCHRONOUS_IO
) &&
361 !(Irp
->Flags
& IRP_OB_QUERY_NAME
))
363 /* Signal the file object and set the status */
364 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
365 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
369 * This could also be a create operation, in which case we want
370 * to make sure there's no APC fired.
372 if (Irp
->Flags
& IRP_CREATE_OPERATION
)
374 /* Clear the APC Routine and remember this */
375 Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
= NULL
;
376 SignaledCreateRequest
= TRUE
;
382 /* Signal the file object and set the status */
383 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
384 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
387 * This could also be a create operation, in which case we want
388 * to make sure there's no APC fired.
390 if (Irp
->Flags
& IRP_CREATE_OPERATION
)
392 /* Clear the APC Routine and remember this */
393 Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
= NULL
;
394 SignaledCreateRequest
= TRUE
;
398 /* Update transfer count for everything but create operation */
399 if (!(Irp
->Flags
& IRP_CREATE_OPERATION
))
401 if (Irp
->Flags
& IRP_WRITE_OPERATION
)
403 /* Update write transfer count */
404 IopUpdateTransferCount(IopWriteTransfer
,
405 (ULONG
)Irp
->IoStatus
.Information
);
407 else if (Irp
->Flags
& IRP_READ_OPERATION
)
409 /* Update read transfer count */
410 IopUpdateTransferCount(IopReadTransfer
,
411 (ULONG
)Irp
->IoStatus
.Information
);
415 /* Update other transfer count */
416 IopUpdateTransferCount(IopOtherTransfer
,
417 (ULONG
)Irp
->IoStatus
.Information
);
421 /* Now that we've signaled the events, de-associate the IRP */
422 IopUnQueueIrpFromThread(Irp
);
424 /* Now check if a User APC Routine was requested */
425 if (Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
)
428 KeInitializeApc(&Irp
->Tail
.Apc
,
429 KeGetCurrentThread(),
430 CurrentApcEnvironment
,
432 IopAbortIrpKernelApc
,
433 (PKNORMAL_ROUTINE
)Irp
->
434 Overlay
.AsynchronousParameters
.UserApcRoutine
,
437 Overlay
.AsynchronousParameters
.UserApcContext
);
440 KeInsertQueueApc(&Irp
->Tail
.Apc
, Irp
->UserIosb
, NULL
, 2);
443 (Irp
->Overlay
.AsynchronousParameters
.UserApcContext
))
445 /* We have an I/O Completion setup... create the special Overlay */
446 Irp
->Tail
.CompletionKey
= Key
;
447 Irp
->Tail
.Overlay
.PacketType
= IopCompletionPacketIrp
;
448 KeInsertQueue(Port
, &Irp
->Tail
.Overlay
.ListEntry
);
452 /* Free the IRP since we don't need it anymore */
456 /* Check if we have a file object that wasn't part of a create */
457 if ((FileObject
) && !(SignaledCreateRequest
))
459 /* Dereference it, since it's not needed anymore either */
460 ObDereferenceObjectDeferDelete(FileObject
);
466 * Either we didn't return from the request, or we did return but this
467 * request was synchronous.
469 if ((Irp
->PendingReturned
) && (FileObject
))
471 /* So we did return with a synch operation, was it the IRP? */
472 if (Irp
->Flags
& IRP_SYNCHRONOUS_API
)
474 /* Yes, this IRP was synchronous, so return the I/O Status */
475 *Irp
->UserIosb
= Irp
->IoStatus
;
477 /* Now check if the user gave an event */
481 KeSetEvent(Irp
->UserEvent
, 0, FALSE
);
485 /* No event was given, so signal the FO instead */
486 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
492 * It's not the IRP that was synchronous, it was the FO
493 * that was opened this way. Signal its event.
495 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
496 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
500 /* Now that we got here, we do this for incomplete I/Os as well */
501 if ((FileObject
) && !(Irp
->Flags
& IRP_CREATE_OPERATION
))
503 /* Dereference the File Object unless this was a create */
504 ObDereferenceObjectDeferDelete(FileObject
);
508 * Check if this was an Executive Event (remember that we know this
509 * by checking if the IRP is synchronous)
511 if ((Irp
->UserEvent
) &&
513 !(Irp
->Flags
& IRP_SYNCHRONOUS_API
))
515 /* This isn't a PKEVENT, so dereference it */
516 ObDereferenceObject(Irp
->UserEvent
);
519 /* Now that we've signaled the events, de-associate the IRP */
520 IopUnQueueIrpFromThread(Irp
);
522 /* Free the IRP as well */
527 /* FUNCTIONS *****************************************************************/
534 IoAllocateIrp(IN CCHAR StackSize
,
535 IN BOOLEAN ChargeQuota
)
538 USHORT Size
= IoSizeOfIrp(StackSize
);
541 PNPAGED_LOOKASIDE_LIST List
= NULL
;
542 PP_NPAGED_LOOKASIDE_NUMBER ListType
= LookasideSmallIrpList
;
544 /* Set Charge Quota Flag */
545 if (ChargeQuota
) Flags
|= IRP_QUOTA_CHARGED
;
547 /* FIXME: Implement Lookaside Floats */
549 /* Figure out which Lookaside List to use */
550 if ((StackSize
<= 8) && (ChargeQuota
== FALSE
))
552 /* Set Fixed Size Flag */
553 Flags
= IRP_ALLOCATED_FIXED_SIZE
;
555 /* See if we should use big list */
558 Size
= IoSizeOfIrp(8);
559 ListType
= LookasideLargeIrpList
;
563 Prcb
= KeGetCurrentPrcb();
565 /* Get the P List First */
566 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].P
;
568 /* Attempt allocation */
569 List
->L
.TotalAllocates
++;
570 Irp
= (PIRP
)InterlockedPopEntrySList(&List
->L
.ListHead
);
572 /* Check if the P List failed */
575 /* Let the balancer know */
576 List
->L
.AllocateMisses
++;
579 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].L
;
580 List
->L
.TotalAllocates
++;
581 Irp
= (PIRP
)InterlockedPopEntrySList(&List
->L
.ListHead
);
585 /* Check if we have to use the pool */
588 /* Did we try lookaside and fail? */
589 if (Flags
& IRP_ALLOCATED_FIXED_SIZE
) List
->L
.AllocateMisses
++;
591 /* Check if we should charge quota */
594 /* Irp = ExAllocatePoolWithQuotaTag(NonPagedPool, Size, TAG_IRP); */
596 Irp
= ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_IRP
);
600 /* Allocate the IRP With no Quota charge */
601 Irp
= ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_IRP
);
604 /* Make sure it was sucessful */
605 if (!Irp
) return(NULL
);
609 /* In this case there is no charge quota */
610 Flags
&= ~IRP_QUOTA_CHARGED
;
613 /* Now Initialize it */
614 IoInitializeIrp(Irp
, Size
, StackSize
);
616 /* Set the Allocation Flags */
617 Irp
->AllocationFlags
= Flags
;
620 IOTRACE(IO_IRP_DEBUG
,
621 "%s - Allocated IRP %p with allocation flags %lx\n",
633 IopAllocateIrpMustSucceed(IN CCHAR StackSize
)
639 /* Try to get an IRP */
640 Irp
= IoAllocateIrp(StackSize
, FALSE
);
644 /* If we fail, start looping till we may get one */
649 /* First, sleep for 10ms */
650 Sleep
.QuadPart
= -10 * 1000 * 10;;
651 KeDelayExecutionThread(KernelMode
, FALSE
, &Sleep
);
653 /* Then, retry allocation */
654 Irp
= IoAllocateIrp(StackSize
, FALSE
);
667 IoBuildAsynchronousFsdRequest(IN ULONG MajorFunction
,
668 IN PDEVICE_OBJECT DeviceObject
,
671 IN PLARGE_INTEGER StartingOffset
,
672 IN PIO_STATUS_BLOCK IoStatusBlock
)
675 PIO_STACK_LOCATION StackPtr
;
678 Irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
679 if (!Irp
) return NULL
;
682 StackPtr
= IoGetNextIrpStackLocation(Irp
);
684 /* Write the Major function and then deal with it */
685 StackPtr
->MajorFunction
= (UCHAR
)MajorFunction
;
687 /* Do not handle the following here */
688 if ((MajorFunction
!= IRP_MJ_FLUSH_BUFFERS
) &&
689 (MajorFunction
!= IRP_MJ_SHUTDOWN
) &&
690 (MajorFunction
!= IRP_MJ_PNP
) &&
691 (MajorFunction
!= IRP_MJ_POWER
))
693 /* Check if this is Buffered IO */
694 if (DeviceObject
->Flags
& DO_BUFFERED_IO
)
696 /* Allocate the System Buffer */
697 Irp
->AssociatedIrp
.SystemBuffer
=
698 ExAllocatePoolWithTag(NonPagedPool
, Length
, TAG_SYS_BUF
);
699 if (!Irp
->AssociatedIrp
.SystemBuffer
)
701 /* Free the IRP and fail */
707 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
709 /* Handle special IRP_MJ_WRITE Case */
710 if (MajorFunction
== IRP_MJ_WRITE
)
712 /* Copy the buffer data */
713 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
, Buffer
, Length
);
717 /* Set the Input Operation flag and set this as a User Buffer */
718 Irp
->Flags
|= IRP_INPUT_OPERATION
;
719 Irp
->UserBuffer
= Buffer
;
722 else if (DeviceObject
->Flags
& DO_DIRECT_IO
)
724 /* Use an MDL for Direct I/O */
725 Irp
->MdlAddress
= IoAllocateMdl(Buffer
,
730 if (!Irp
->MdlAddress
)
732 /* Free the IRP and fail */
741 MmProbeAndLockPages(Irp
->MdlAddress
,
743 MajorFunction
== IRP_MJ_READ
?
744 IoWriteAccess
: IoReadAccess
);
746 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
748 /* Free the IRP and its MDL */
749 IoFreeMdl(Irp
->MdlAddress
);
753 _SEH2_YIELD(return NULL
);
759 /* Neither, use the buffer */
760 Irp
->UserBuffer
= Buffer
;
763 /* Check if this is a read */
764 if (MajorFunction
== IRP_MJ_READ
)
766 /* Set the parameters for a read */
767 StackPtr
->Parameters
.Read
.Length
= Length
;
768 StackPtr
->Parameters
.Read
.ByteOffset
= *StartingOffset
;
770 else if (MajorFunction
== IRP_MJ_WRITE
)
772 /* Otherwise, set write parameters */
773 StackPtr
->Parameters
.Write
.Length
= Length
;
774 StackPtr
->Parameters
.Write
.ByteOffset
= *StartingOffset
;
778 /* Set the Current Thread and IOSB */
779 Irp
->UserIosb
= IoStatusBlock
;
780 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
783 IOTRACE(IO_IRP_DEBUG
,
784 "%s - Built IRP %p with Major, Buffer, DO %lx %p %p\n",
798 IoBuildDeviceIoControlRequest(IN ULONG IoControlCode
,
799 IN PDEVICE_OBJECT DeviceObject
,
800 IN PVOID InputBuffer
,
801 IN ULONG InputBufferLength
,
802 IN PVOID OutputBuffer
,
803 IN ULONG OutputBufferLength
,
804 IN BOOLEAN InternalDeviceIoControl
,
806 IN PIO_STATUS_BLOCK IoStatusBlock
)
809 PIO_STACK_LOCATION StackPtr
;
813 Irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
814 if (!Irp
) return NULL
;
817 StackPtr
= IoGetNextIrpStackLocation(Irp
);
819 /* Set the DevCtl Type */
820 StackPtr
->MajorFunction
= InternalDeviceIoControl
?
821 IRP_MJ_INTERNAL_DEVICE_CONTROL
:
822 IRP_MJ_DEVICE_CONTROL
;
824 /* Set the IOCTL Data */
825 StackPtr
->Parameters
.DeviceIoControl
.IoControlCode
= IoControlCode
;
826 StackPtr
->Parameters
.DeviceIoControl
.InputBufferLength
= InputBufferLength
;
827 StackPtr
->Parameters
.DeviceIoControl
.OutputBufferLength
=
830 /* Handle the Methods */
831 switch (IO_METHOD_FROM_CTL_CODE(IoControlCode
))
834 case METHOD_BUFFERED
:
836 /* Select the right Buffer Length */
837 BufferLength
= InputBufferLength
> OutputBufferLength
?
838 InputBufferLength
: OutputBufferLength
;
840 /* Make sure there is one */
843 /* Allocate the System Buffer */
844 Irp
->AssociatedIrp
.SystemBuffer
=
845 ExAllocatePoolWithTag(NonPagedPool
,
848 if (!Irp
->AssociatedIrp
.SystemBuffer
)
850 /* Free the IRP and fail */
855 /* Check if we got a buffer */
858 /* Copy into the System Buffer */
859 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
864 /* Write the flags */
865 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
866 if (OutputBuffer
) Irp
->Flags
|= IRP_INPUT_OPERATION
;
868 /* Save the Buffer */
869 Irp
->UserBuffer
= OutputBuffer
;
873 /* Clear the Flags and Buffer */
875 Irp
->UserBuffer
= NULL
;
880 case METHOD_IN_DIRECT
:
881 case METHOD_OUT_DIRECT
:
883 /* Check if we got an input buffer */
886 /* Allocate the System Buffer */
887 Irp
->AssociatedIrp
.SystemBuffer
=
888 ExAllocatePoolWithTag(NonPagedPool
,
891 if (!Irp
->AssociatedIrp
.SystemBuffer
)
893 /* Free the IRP and fail */
898 /* Copy into the System Buffer */
899 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
903 /* Write the flags */
904 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
908 /* Clear the flags */
912 /* Check if we got an output buffer */
915 /* Allocate the System Buffer */
916 Irp
->MdlAddress
= IoAllocateMdl(OutputBuffer
,
921 if (!Irp
->MdlAddress
)
923 /* Free the IRP and fail */
932 MmProbeAndLockPages(Irp
->MdlAddress
,
934 IO_METHOD_FROM_CTL_CODE(IoControlCode
) ==
936 IoReadAccess
: IoWriteAccess
);
938 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
941 IoFreeMdl(Irp
->MdlAddress
);
943 /* Free the input buffer and IRP */
944 if (InputBuffer
) ExFreePool(Irp
->AssociatedIrp
.SystemBuffer
);
948 _SEH2_YIELD(return NULL
);
956 /* Just save the Buffer */
957 Irp
->UserBuffer
= OutputBuffer
;
958 StackPtr
->Parameters
.DeviceIoControl
.Type3InputBuffer
= InputBuffer
;
961 /* Now write the Event and IoSB */
962 Irp
->UserIosb
= IoStatusBlock
;
963 Irp
->UserEvent
= Event
;
965 /* Sync IRPs are queued to requestor thread's irp cancel/cleanup list */
966 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
967 IoQueueThreadIrp(Irp
);
970 IOTRACE(IO_IRP_DEBUG
,
971 "%s - Built IRP %p with IOCTL, Buffers, DO %lx %p %p %p\n",
986 IoBuildSynchronousFsdRequest(IN ULONG MajorFunction
,
987 IN PDEVICE_OBJECT DeviceObject
,
990 IN PLARGE_INTEGER StartingOffset
,
992 IN PIO_STATUS_BLOCK IoStatusBlock
)
996 /* Do the big work to set up the IRP */
997 Irp
= IoBuildAsynchronousFsdRequest(MajorFunction
,
1003 if (!Irp
) return NULL
;
1005 /* Set the Event which makes it Syncronous */
1006 Irp
->UserEvent
= Event
;
1008 /* Sync IRPs are queued to requestor thread's irp cancel/cleanup list */
1009 IoQueueThreadIrp(Irp
);
1018 IoCancelIrp(IN PIRP Irp
)
1021 PDRIVER_CANCEL CancelRoutine
;
1022 IOTRACE(IO_IRP_DEBUG
,
1023 "%s - Canceling IRP %p\n",
1026 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1028 /* Acquire the cancel lock and cancel the IRP */
1029 IoAcquireCancelSpinLock(&OldIrql
);
1032 /* Clear the cancel routine and get the old one */
1033 CancelRoutine
= (PVOID
)IoSetCancelRoutine(Irp
, NULL
);
1036 /* We had a routine, make sure the IRP isn't completed */
1037 if (Irp
->CurrentLocation
> (Irp
->StackCount
+ 1))
1039 /* It is, bugcheck */
1040 KeBugCheckEx(CANCEL_STATE_IN_COMPLETED_IRP
,
1042 (ULONG_PTR
)CancelRoutine
,
1047 /* Set the cancel IRQL And call the routine */
1048 Irp
->CancelIrql
= OldIrql
;
1049 CancelRoutine(IoGetCurrentIrpStackLocation(Irp
)->DeviceObject
, Irp
);
1053 /* Otherwise, release the cancel lock and fail */
1054 IoReleaseCancelSpinLock(OldIrql
);
1063 IoCancelThreadIo(IN PETHREAD Thread
)
1066 ULONG Retries
= 3000;
1067 LARGE_INTEGER Interval
;
1068 PLIST_ENTRY ListHead
, NextEntry
;
1072 /* Windows isn't using given thread, but using current. */
1073 Thread
= PsGetCurrentThread();
1075 IOTRACE(IO_IRP_DEBUG
,
1076 "%s - Canceling IRPs for Thread %p\n",
1080 /* Raise to APC to protect the IrpList */
1081 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1083 /* Start by cancelling all the IRPs in the current thread queue. */
1084 ListHead
= &Thread
->IrpList
;
1085 NextEntry
= ListHead
->Flink
;
1086 while (ListHead
!= NextEntry
)
1089 Irp
= CONTAINING_RECORD(NextEntry
, IRP
, ThreadListEntry
);
1094 /* Move to the next entry */
1095 NextEntry
= NextEntry
->Flink
;
1098 /* Wait 100 milliseconds */
1099 Interval
.QuadPart
= -1000000;
1101 /* Wait till all the IRPs are completed or cancelled. */
1102 while (!IsListEmpty(&Thread
->IrpList
))
1104 /* Now we can lower */
1105 KeLowerIrql(OldIrql
);
1107 /* Wait a short while and then look if all our IRPs were completed. */
1108 KeDelayExecutionThread(KernelMode
, FALSE
, &Interval
);
1111 * Don't stay here forever if some broken driver doesn't complete
1116 /* Print out a message and remove the IRP */
1117 DPRINT1("Broken driver did not complete!\n");
1118 IopDisassociateThreadIrp();
1121 /* Raise the IRQL Again */
1122 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1125 /* We're done, lower the IRQL */
1126 KeLowerIrql(OldIrql
);
1135 IoCallDriver(IN PDEVICE_OBJECT DeviceObject
,
1139 return IofCallDriver(DeviceObject
, Irp
);
1142 #define IoCallDriver IofCallDriver
1147 #undef IoCompleteRequest
1150 IoCompleteRequest(IN PIRP Irp
,
1151 IN CCHAR PriorityBoost
)
1153 /* Call the fastcall */
1154 IofCompleteRequest(Irp
, PriorityBoost
);
1157 #define IoCompleteRequest IofCompleteRequest
1164 IoEnqueueIrp(IN PIRP Irp
)
1166 /* This is the same as calling IoQueueThreadIrp */
1167 IoQueueThreadIrp(Irp
);
1175 IofCallDriver(IN PDEVICE_OBJECT DeviceObject
,
1178 PDRIVER_OBJECT DriverObject
;
1179 PIO_STACK_LOCATION StackPtr
;
1181 /* Make sure this is a valid IRP */
1182 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1184 /* Get the Driver Object */
1185 DriverObject
= DeviceObject
->DriverObject
;
1187 /* Decrease the current location and check if */
1188 Irp
->CurrentLocation
--;
1189 if (Irp
->CurrentLocation
<= 0)
1191 /* This IRP ran out of stack, bugcheck */
1192 KeBugCheckEx(NO_MORE_IRP_STACK_LOCATIONS
, (ULONG_PTR
)Irp
, 0, 0, 0);
1195 /* Now update the stack location */
1196 StackPtr
= IoGetNextIrpStackLocation(Irp
);
1197 Irp
->Tail
.Overlay
.CurrentStackLocation
= StackPtr
;
1199 /* Get the Device Object */
1200 StackPtr
->DeviceObject
= DeviceObject
;
1203 return DriverObject
->MajorFunction
[StackPtr
->MajorFunction
](DeviceObject
,
1209 IopClearStackLocation(IN PIO_STACK_LOCATION IoStackLocation
)
1211 IoStackLocation
->MinorFunction
= 0;
1212 IoStackLocation
->Flags
= 0;
1213 IoStackLocation
->Control
&= SL_ERROR_RETURNED
;
1214 IoStackLocation
->Parameters
.Others
.Argument1
= 0;
1215 IoStackLocation
->Parameters
.Others
.Argument2
= 0;
1216 IoStackLocation
->Parameters
.Others
.Argument3
= 0;
1217 IoStackLocation
->FileObject
= NULL
;
1225 IofCompleteRequest(IN PIRP Irp
,
1226 IN CCHAR PriorityBoost
)
1228 PIO_STACK_LOCATION StackPtr
, LastStackPtr
;
1229 PDEVICE_OBJECT DeviceObject
;
1230 PFILE_OBJECT FileObject
;
1237 NTSTATUS ErrorCode
= STATUS_SUCCESS
;
1238 IOTRACE(IO_IRP_DEBUG
,
1239 "%s - Completing IRP %p\n",
1243 /* Make sure this IRP isn't getting completed twice or is invalid */
1244 if ((Irp
->CurrentLocation
) > (Irp
->StackCount
+ 1))
1247 KeBugCheckEx(MULTIPLE_IRP_COMPLETE_REQUESTS
, (ULONG_PTR
)Irp
, 0, 0, 0);
1250 /* Some sanity checks */
1251 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1252 ASSERT(!Irp
->CancelRoutine
);
1253 ASSERT(Irp
->IoStatus
.Status
!= STATUS_PENDING
);
1254 ASSERT(Irp
->IoStatus
.Status
!= (NTSTATUS
)0xFFFFFFFF);
1256 /* Get the last stack */
1257 LastStackPtr
= (PIO_STACK_LOCATION
)(Irp
+ 1);
1258 if (LastStackPtr
->Control
& SL_ERROR_RETURNED
)
1260 /* Get the error code */
1261 ErrorCode
= PtrToUlong(LastStackPtr
->Parameters
.Others
.Argument4
);
1265 * Start the loop with the current stack and point the IRP to the next stack
1266 * and then keep incrementing the stack as we loop through. The IRP should
1267 * always point to the next stack location w.r.t the one currently being
1268 * analyzed, so completion routine code will see the appropriate value.
1269 * Because of this, we must loop until the current stack location is +1 of
1270 * the stack count, because when StackPtr is at the end, CurrentLocation is +1.
1272 for (StackPtr
= IoGetCurrentIrpStackLocation(Irp
),
1273 Irp
->CurrentLocation
++,
1274 Irp
->Tail
.Overlay
.CurrentStackLocation
++;
1275 Irp
->CurrentLocation
<= (Irp
->StackCount
+ 1);
1277 Irp
->CurrentLocation
++,
1278 Irp
->Tail
.Overlay
.CurrentStackLocation
++)
1280 /* Set Pending Returned */
1281 Irp
->PendingReturned
= StackPtr
->Control
& SL_PENDING_RETURNED
;
1283 /* Check if we failed */
1284 if (!NT_SUCCESS(Irp
->IoStatus
.Status
))
1286 /* Check if it was changed by a completion routine */
1287 if (Irp
->IoStatus
.Status
!= ErrorCode
)
1289 /* Update the error for the current stack */
1290 ErrorCode
= Irp
->IoStatus
.Status
;
1291 StackPtr
->Control
|= SL_ERROR_RETURNED
;
1292 LastStackPtr
->Parameters
.Others
.Argument4
= UlongToPtr(ErrorCode
);
1293 LastStackPtr
->Control
|= SL_ERROR_RETURNED
;
1297 /* Check if there is a Completion Routine to Call */
1298 if ((NT_SUCCESS(Irp
->IoStatus
.Status
) &&
1299 (StackPtr
->Control
& SL_INVOKE_ON_SUCCESS
)) ||
1300 (!NT_SUCCESS(Irp
->IoStatus
.Status
) &&
1301 (StackPtr
->Control
& SL_INVOKE_ON_ERROR
)) ||
1303 (StackPtr
->Control
& SL_INVOKE_ON_CANCEL
)))
1305 /* Clear the stack location */
1306 IopClearStackLocation(StackPtr
);
1308 /* Check for highest-level device completion routines */
1309 if (Irp
->CurrentLocation
== (Irp
->StackCount
+ 1))
1311 /* Clear the DO, since the current stack location is invalid */
1312 DeviceObject
= NULL
;
1316 /* Otherwise, return the real one */
1317 DeviceObject
= IoGetCurrentIrpStackLocation(Irp
)->DeviceObject
;
1320 /* Call the completion routine */
1321 Status
= StackPtr
->CompletionRoutine(DeviceObject
,
1325 /* Don't touch the Packet in this case, since it might be gone! */
1326 if (Status
== STATUS_MORE_PROCESSING_REQUIRED
) return;
1330 /* Otherwise, check if this is a completed IRP */
1331 if ((Irp
->CurrentLocation
<= Irp
->StackCount
) &&
1332 (Irp
->PendingReturned
))
1334 /* Mark it as pending */
1335 IoMarkIrpPending(Irp
);
1338 /* Clear the stack location */
1339 IopClearStackLocation(StackPtr
);
1343 /* Check if the IRP is an associated IRP */
1344 if (Irp
->Flags
& IRP_ASSOCIATED_IRP
)
1346 /* Get the master IRP and count */
1347 MasterIrp
= Irp
->AssociatedIrp
.MasterIrp
;
1348 MasterCount
= InterlockedDecrement(&MasterIrp
->AssociatedIrp
.IrpCount
);
1351 for (Mdl
= Irp
->MdlAddress
; Mdl
; Mdl
= NextMdl
)
1353 /* Go to the next one */
1354 NextMdl
= Mdl
->Next
;
1358 /* Free the IRP itself */
1361 /* Complete the Master IRP */
1362 if (!MasterCount
) IofCompleteRequest(MasterIrp
, PriorityBoost
);
1366 /* We don't support this yet */
1367 ASSERT(Irp
->IoStatus
.Status
!= STATUS_REPARSE
);
1369 /* Check if we have an auxiliary buffer */
1370 if (Irp
->Tail
.Overlay
.AuxiliaryBuffer
)
1373 ExFreePool(Irp
->Tail
.Overlay
.AuxiliaryBuffer
);
1374 Irp
->Tail
.Overlay
.AuxiliaryBuffer
= NULL
;
1377 /* Check if this is a Paging I/O or Close Operation */
1378 if (Irp
->Flags
& (IRP_PAGING_IO
| IRP_CLOSE_OPERATION
))
1380 /* Handle a Close Operation or Sync Paging I/O */
1381 if (Irp
->Flags
& (IRP_SYNCHRONOUS_PAGING_IO
| IRP_CLOSE_OPERATION
))
1383 /* Set the I/O Status and Signal the Event */
1384 Flags
= Irp
->Flags
& (IRP_SYNCHRONOUS_PAGING_IO
| IRP_PAGING_IO
);
1385 *Irp
->UserIosb
= Irp
->IoStatus
;
1386 KeSetEvent(Irp
->UserEvent
, PriorityBoost
, FALSE
);
1388 /* Free the IRP for a Paging I/O Only, Close is handled by us */
1389 if (Flags
) IoFreeIrp(Irp
);
1395 KeInitializeApc(&Irp
->Tail
.Apc
1396 &Irp
->Tail
.Overlay
.Thread
->Tcb
,
1397 Irp
->ApcEnvironment
,
1398 IopCompletePageWrite
,
1403 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1408 /* Not implemented yet. */
1409 UNIMPLEMENTED_DBGBREAK("Not supported!\n");
1413 /* Get out of here */
1417 /* Unlock MDL Pages, page 167. */
1418 Mdl
= Irp
->MdlAddress
;
1425 /* Check if we should exit because of a Deferred I/O (page 168) */
1426 if ((Irp
->Flags
& IRP_DEFER_IO_COMPLETION
) && !(Irp
->PendingReturned
))
1429 * Return without queuing the completion APC, since the caller will
1430 * take care of doing its own optimized completion at PASSIVE_LEVEL.
1435 /* Get the thread and file object */
1436 Thread
= Irp
->Tail
.Overlay
.Thread
;
1437 FileObject
= Irp
->Tail
.Overlay
.OriginalFileObject
;
1439 /* Make sure the IRP isn't canceled */
1442 /* Initialize the APC */
1443 KeInitializeApc(&Irp
->Tail
.Apc
,
1445 Irp
->ApcEnvironment
,
1453 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1455 NULL
, /* This is used for REPARSE stuff */
1460 /* The IRP just got canceled... does a thread still own it? */
1463 /* Yes! There is still hope! Initialize the APC */
1464 KeInitializeApc(&Irp
->Tail
.Apc
,
1466 Irp
->ApcEnvironment
,
1474 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1476 NULL
, /* This is used for REPARSE stuff */
1481 /* Nothing left for us to do, kill it */
1482 ASSERT(Irp
->Cancel
);
1483 IopCleanupIrp(Irp
, FileObject
);
1490 IopSynchronousCompletion(IN PDEVICE_OBJECT DeviceObject
,
1494 if (Irp
->PendingReturned
)
1495 KeSetEvent((PKEVENT
)Context
, IO_NO_INCREMENT
, FALSE
);
1496 return STATUS_MORE_PROCESSING_REQUIRED
;
1504 IoForwardIrpSynchronously(IN PDEVICE_OBJECT DeviceObject
,
1510 /* Check if next stack location is available */
1511 if (Irp
->CurrentLocation
< Irp
->StackCount
)
1513 /* No more stack location */
1517 /* Initialize event */
1518 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
1520 /* Copy stack location for next driver */
1521 IoCopyCurrentIrpStackLocationToNext(Irp
);
1523 /* Set a completion routine, which will signal the event */
1524 IoSetCompletionRoutine(Irp
, IopSynchronousCompletion
, &Event
, TRUE
, TRUE
, TRUE
);
1526 /* Call next driver */
1527 Status
= IoCallDriver(DeviceObject
, Irp
);
1529 /* Check if irp is pending */
1530 if (Status
== STATUS_PENDING
)
1532 /* Yes, wait for its completion */
1533 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
1536 /* Return success */
1545 IoFreeIrp(IN PIRP Irp
)
1547 PNPAGED_LOOKASIDE_LIST List
;
1548 PP_NPAGED_LOOKASIDE_NUMBER ListType
= LookasideSmallIrpList
;
1550 IOTRACE(IO_IRP_DEBUG
,
1551 "%s - Freeing IRPs %p\n",
1555 /* Make sure the Thread IRP list is empty and that it OK to free it */
1556 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1557 ASSERT(IsListEmpty(&Irp
->ThreadListEntry
));
1558 ASSERT(Irp
->CurrentLocation
>= Irp
->StackCount
);
1560 /* If this was a pool alloc, free it with the pool */
1561 if (!(Irp
->AllocationFlags
& IRP_ALLOCATED_FIXED_SIZE
))
1564 ExFreePoolWithTag(Irp
, TAG_IRP
);
1568 /* Check if this was a Big IRP */
1569 if (Irp
->StackCount
!= 1) ListType
= LookasideLargeIrpList
;
1572 Prcb
= KeGetCurrentPrcb();
1574 /* Use the P List */
1575 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].P
;
1576 List
->L
.TotalFrees
++;
1578 /* Check if the Free was within the Depth or not */
1579 if (ExQueryDepthSList(&List
->L
.ListHead
) >= List
->L
.Depth
)
1581 /* Let the balancer know */
1582 List
->L
.FreeMisses
++;
1584 /* Use the L List */
1585 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].L
;
1586 List
->L
.TotalFrees
++;
1588 /* Check if the Free was within the Depth or not */
1589 if (ExQueryDepthSList(&List
->L
.ListHead
) >= List
->L
.Depth
)
1591 /* All lists failed, use the pool */
1592 List
->L
.FreeMisses
++;
1593 ExFreePoolWithTag(Irp
, TAG_IRP
);
1598 /* The free was within the Depth */
1601 InterlockedPushEntrySList(&List
->L
.ListHead
,
1612 IoGetPagingIoPriority(IN PIRP Irp
)
1614 IO_PAGING_PRIORITY Priority
;
1620 /* Check what priority it has */
1621 if (Flags
& IRP_CLASS_CACHE_OPERATION
)
1624 Priority
= IoPagingPriorityHigh
;
1626 else if (Flags
& IRP_PAGING_IO
)
1628 /* Normal priority */
1629 Priority
= IoPagingPriorityNormal
;
1633 /* Invalid -- not a paging IRP */
1634 Priority
= IoPagingPriorityInvalid
;
1637 /* Return the priority */
1646 IoGetRequestorProcess(IN PIRP Irp
)
1648 /* Return the requestor process */
1649 if (Irp
->Tail
.Overlay
.Thread
)
1651 if (Irp
->ApcEnvironment
== OriginalApcEnvironment
)
1653 return Irp
->Tail
.Overlay
.Thread
->ThreadsProcess
;
1655 else if (Irp
->ApcEnvironment
== AttachedApcEnvironment
)
1657 return (PEPROCESS
)Irp
->Tail
.Overlay
.Thread
->Tcb
.ApcState
.Process
;
1669 IoGetRequestorProcessId(IN PIRP Irp
)
1673 /* Return the requestor process' id */
1674 Process
= IoGetRequestorProcess(Irp
);
1675 if (Process
) return PtrToUlong(Process
->UniqueProcessId
);
1685 IoGetRequestorSessionId(IN PIRP Irp
,
1686 OUT PULONG pSessionId
)
1690 /* Return the session */
1691 if (Irp
->Tail
.Overlay
.Thread
)
1693 Process
= Irp
->Tail
.Overlay
.Thread
->ThreadsProcess
;
1694 *pSessionId
= MmGetSessionId(Process
);
1695 return STATUS_SUCCESS
;
1698 *pSessionId
= (ULONG
)-1;
1699 return STATUS_UNSUCCESSFUL
;
1707 IoGetTopLevelIrp(VOID
)
1709 /* Return the IRP */
1710 return (PIRP
)PsGetCurrentThread()->TopLevelIrp
;
1718 IoInitializeIrp(IN PIRP Irp
,
1719 IN USHORT PacketSize
,
1723 IOTRACE(IO_IRP_DEBUG
,
1724 "%s - Initializing IRP %p\n",
1727 RtlZeroMemory(Irp
, PacketSize
);
1729 /* Set the Header and other data */
1730 Irp
->Type
= IO_TYPE_IRP
;
1731 Irp
->Size
= PacketSize
;
1732 Irp
->StackCount
= StackSize
;
1733 Irp
->CurrentLocation
= StackSize
+ 1;
1734 Irp
->ApcEnvironment
= KeGetCurrentThread()->ApcStateIndex
;
1735 Irp
->Tail
.Overlay
.CurrentStackLocation
= (PIO_STACK_LOCATION
)(Irp
+ 1) + StackSize
;
1737 /* Initialize the Thread List */
1738 InitializeListHead(&Irp
->ThreadListEntry
);
1746 IoIsOperationSynchronous(IN PIRP Irp
)
1751 /* If the IRP requests synchronous paging I/O, if the file object was opened
1752 for synchronous I/O, if the IRP_SYNCHRONOUS_API flag is set in the IRP
1753 the operation is synchronous */
1754 SynchIO
= (IoGetCurrentIrpStackLocation(Irp
)->FileObject
->Flags
& FO_SYNCHRONOUS_IO
) ||
1755 (Irp
->Flags
& IRP_SYNCHRONOUS_API
) || (Irp
->Flags
& IRP_SYNCHRONOUS_PAGING_IO
);
1757 /* If the IRP requests asynchronous paging I/O, the operation is asynchronous,
1758 even if one of the above conditions is true */
1759 ForceAsync
= (Irp
->Flags
& IRP_PAGING_IO
) && !(Irp
->Flags
& IRP_SYNCHRONOUS_PAGING_IO
);
1761 /* Check the flags */
1762 if (SynchIO
&& !ForceAsync
)
1764 /* Synch API or Paging I/O is OK, as is Sync File I/O */
1768 /* Otherwise, it is an asynchronous operation. */
1777 IoIsValidNameGraftingBuffer(IN PIRP Irp
,
1778 IN PREPARSE_DATA_BUFFER ReparseBuffer
)
1789 IoMakeAssociatedIrp(IN PIRP Irp
,
1793 IOTRACE(IO_IRP_DEBUG
,
1794 "%s - Associating IRP %p\n",
1798 /* Allocate the IRP */
1799 AssocIrp
= IoAllocateIrp(StackSize
, FALSE
);
1800 if (!AssocIrp
) return NULL
;
1803 AssocIrp
->Flags
|= IRP_ASSOCIATED_IRP
;
1805 /* Set the Thread */
1806 AssocIrp
->Tail
.Overlay
.Thread
= Irp
->Tail
.Overlay
.Thread
;
1808 /* Associate them */
1809 AssocIrp
->AssociatedIrp
.MasterIrp
= Irp
;
1818 IoQueueThreadIrp(IN PIRP Irp
)
1820 IOTRACE(IO_IRP_DEBUG
,
1821 "%s - Queueing IRP %p\n",
1825 /* Use our inlined routine */
1826 IopQueueIrpToThread(Irp
);
1831 * Reference: Chris Cant's "Writing WDM Device Drivers"
1835 IoReuseIrp(IN OUT PIRP Irp
,
1838 UCHAR AllocationFlags
;
1839 IOTRACE(IO_IRP_DEBUG
,
1840 "%s - Reusing IRP %p\n",
1844 /* Make sure it's OK to reuse it */
1845 ASSERT(!Irp
->CancelRoutine
);
1846 ASSERT(IsListEmpty(&Irp
->ThreadListEntry
));
1848 /* Get the old flags */
1849 AllocationFlags
= Irp
->AllocationFlags
;
1851 /* Reinitialize the IRP */
1852 IoInitializeIrp(Irp
, Irp
->Size
, Irp
->StackCount
);
1854 /* Duplicate the data */
1855 Irp
->IoStatus
.Status
= Status
;
1856 Irp
->AllocationFlags
= AllocationFlags
;
1864 IoSetTopLevelIrp(IN PIRP Irp
)
1867 PsGetCurrentThread()->TopLevelIrp
= (ULONG_PTR
)Irp
;
1870 #if defined (_WIN64)
1874 IN PIRP Irp OPTIONAL
)