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 PREPARSE_DATA_BUFFER ReparseData
;
269 ReparseData
= (PREPARSE_DATA_BUFFER
)*SystemArgument2
;
271 ASSERT(ReparseData
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
);
272 ASSERT(ReparseData
->ReparseDataLength
< MAXIMUM_REPARSE_DATA_BUFFER_SIZE
);
273 ASSERT(ReparseData
->Reserved
< MAXIMUM_REPARSE_DATA_BUFFER_SIZE
);
275 IopDoNameTransmogrify(Irp
, FileObject
, ReparseData
);
279 /* Handle Buffered case first */
280 if (Irp
->Flags
& IRP_BUFFERED_IO
)
282 /* Check if we have an input buffer and if we succeeded */
283 if ((Irp
->Flags
& IRP_INPUT_OPERATION
) &&
284 (Irp
->IoStatus
.Status
!= STATUS_VERIFY_REQUIRED
) &&
285 !(NT_ERROR(Irp
->IoStatus
.Status
)))
287 /* Copy the buffer back to the user */
288 RtlCopyMemory(Irp
->UserBuffer
,
289 Irp
->AssociatedIrp
.SystemBuffer
,
290 Irp
->IoStatus
.Information
);
293 /* Also check if we should de-allocate it */
294 if (Irp
->Flags
& IRP_DEALLOCATE_BUFFER
)
297 ExFreePool(Irp
->AssociatedIrp
.SystemBuffer
);
301 /* Now we got rid of these two... */
302 Irp
->Flags
&= ~(IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
);
304 /* Check if there's an MDL */
305 for (Mdl
= Irp
->MdlAddress
; Mdl
; Mdl
= NextMdl
)
313 Irp
->MdlAddress
= NULL
;
316 * Check if either the request was completed without any errors
317 * (but warnings are OK!), or if it was completed with an error, but
318 * did return from a pending I/O Operation and is not synchronous.
320 if (!(NT_ERROR(Irp
->IoStatus
.Status
)) ||
321 (NT_ERROR(Irp
->IoStatus
.Status
) &&
322 (Irp
->PendingReturned
) &&
323 !(IsIrpSynchronous(Irp
, FileObject
))))
325 /* Get any information we need from the FO before we kill it */
326 if ((FileObject
) && (FileObject
->CompletionContext
))
328 /* Save Completion Data */
329 Port
= FileObject
->CompletionContext
->Port
;
330 Key
= FileObject
->CompletionContext
->Key
;
333 /* Check for UserIos */
334 if (Irp
->UserIosb
!= NULL
)
336 /* Use SEH to make sure we don't write somewhere invalid */
339 /* Save the IOSB Information */
340 *Irp
->UserIosb
= Irp
->IoStatus
;
342 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
344 /* Ignore any error */
349 /* Check if we have an event or a file object */
352 /* At the very least, this is a PKEVENT, so signal it always */
353 KeSetEvent(Irp
->UserEvent
, 0, FALSE
);
355 /* Check if we also have a File Object */
358 /* Check if this is an Asynch API */
359 if (!(Irp
->Flags
& IRP_SYNCHRONOUS_API
))
361 /* Dereference the event */
362 ObDereferenceObject(Irp
->UserEvent
);
366 * Now, if this is a Synch I/O File Object, then this event is
367 * NOT an actual Executive Event, so we won't dereference it,
368 * and instead, we will signal the File Object
370 if ((FileObject
->Flags
& FO_SYNCHRONOUS_IO
) &&
371 !(Irp
->Flags
& IRP_OB_QUERY_NAME
))
373 /* Signal the file object and set the status */
374 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
375 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
379 * This could also be a create operation, in which case we want
380 * to make sure there's no APC fired.
382 if (Irp
->Flags
& IRP_CREATE_OPERATION
)
384 /* Clear the APC Routine and remember this */
385 Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
= NULL
;
386 SignaledCreateRequest
= TRUE
;
392 /* Signal the file object and set the status */
393 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
394 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
397 * This could also be a create operation, in which case we want
398 * to make sure there's no APC fired.
400 if (Irp
->Flags
& IRP_CREATE_OPERATION
)
402 /* Clear the APC Routine and remember this */
403 Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
= NULL
;
404 SignaledCreateRequest
= TRUE
;
408 /* Update transfer count for everything but create operation */
409 if (!(Irp
->Flags
& IRP_CREATE_OPERATION
))
411 if (Irp
->Flags
& IRP_WRITE_OPERATION
)
413 /* Update write transfer count */
414 IopUpdateTransferCount(IopWriteTransfer
,
415 (ULONG
)Irp
->IoStatus
.Information
);
417 else if (Irp
->Flags
& IRP_READ_OPERATION
)
419 /* Update read transfer count */
420 IopUpdateTransferCount(IopReadTransfer
,
421 (ULONG
)Irp
->IoStatus
.Information
);
425 /* Update other transfer count */
426 IopUpdateTransferCount(IopOtherTransfer
,
427 (ULONG
)Irp
->IoStatus
.Information
);
431 /* Now that we've signaled the events, de-associate the IRP */
432 IopUnQueueIrpFromThread(Irp
);
434 /* Now check if a User APC Routine was requested */
435 if (Irp
->Overlay
.AsynchronousParameters
.UserApcRoutine
)
438 KeInitializeApc(&Irp
->Tail
.Apc
,
439 KeGetCurrentThread(),
440 CurrentApcEnvironment
,
442 IopAbortIrpKernelApc
,
443 (PKNORMAL_ROUTINE
)Irp
->
444 Overlay
.AsynchronousParameters
.UserApcRoutine
,
447 Overlay
.AsynchronousParameters
.UserApcContext
);
450 KeInsertQueueApc(&Irp
->Tail
.Apc
, Irp
->UserIosb
, NULL
, 2);
453 (Irp
->Overlay
.AsynchronousParameters
.UserApcContext
))
455 /* We have an I/O Completion setup... create the special Overlay */
456 Irp
->Tail
.CompletionKey
= Key
;
457 Irp
->Tail
.Overlay
.PacketType
= IopCompletionPacketIrp
;
458 KeInsertQueue(Port
, &Irp
->Tail
.Overlay
.ListEntry
);
462 /* Free the IRP since we don't need it anymore */
466 /* Check if we have a file object that wasn't part of a create */
467 if ((FileObject
) && !(SignaledCreateRequest
))
469 /* Dereference it, since it's not needed anymore either */
470 ObDereferenceObjectDeferDelete(FileObject
);
476 * Either we didn't return from the request, or we did return but this
477 * request was synchronous.
479 if ((Irp
->PendingReturned
) && (FileObject
))
481 /* So we did return with a synch operation, was it the IRP? */
482 if (Irp
->Flags
& IRP_SYNCHRONOUS_API
)
484 /* Yes, this IRP was synchronous, so return the I/O Status */
485 *Irp
->UserIosb
= Irp
->IoStatus
;
487 /* Now check if the user gave an event */
491 KeSetEvent(Irp
->UserEvent
, 0, FALSE
);
495 /* No event was given, so signal the FO instead */
496 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
502 * It's not the IRP that was synchronous, it was the FO
503 * that was opened this way. Signal its event.
505 FileObject
->FinalStatus
= Irp
->IoStatus
.Status
;
506 KeSetEvent(&FileObject
->Event
, 0, FALSE
);
510 /* Now that we got here, we do this for incomplete I/Os as well */
511 if ((FileObject
) && !(Irp
->Flags
& IRP_CREATE_OPERATION
))
513 /* Dereference the File Object unless this was a create */
514 ObDereferenceObjectDeferDelete(FileObject
);
518 * Check if this was an Executive Event (remember that we know this
519 * by checking if the IRP is synchronous)
521 if ((Irp
->UserEvent
) &&
523 !(Irp
->Flags
& IRP_SYNCHRONOUS_API
))
525 /* This isn't a PKEVENT, so dereference it */
526 ObDereferenceObject(Irp
->UserEvent
);
529 /* Now that we've signaled the events, de-associate the IRP */
530 IopUnQueueIrpFromThread(Irp
);
532 /* Free the IRP as well */
537 /* FUNCTIONS *****************************************************************/
544 IoAllocateIrp(IN CCHAR StackSize
,
545 IN BOOLEAN ChargeQuota
)
548 USHORT Size
= IoSizeOfIrp(StackSize
);
551 PNPAGED_LOOKASIDE_LIST List
= NULL
;
552 PP_NPAGED_LOOKASIDE_NUMBER ListType
= LookasideSmallIrpList
;
554 /* Set Charge Quota Flag */
555 if (ChargeQuota
) Flags
|= IRP_QUOTA_CHARGED
;
557 /* FIXME: Implement Lookaside Floats */
559 /* Figure out which Lookaside List to use */
560 if ((StackSize
<= 8) && (ChargeQuota
== FALSE
))
562 /* Set Fixed Size Flag */
563 Flags
= IRP_ALLOCATED_FIXED_SIZE
;
565 /* See if we should use big list */
568 Size
= IoSizeOfIrp(8);
569 ListType
= LookasideLargeIrpList
;
573 Prcb
= KeGetCurrentPrcb();
575 /* Get the P List First */
576 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].P
;
578 /* Attempt allocation */
579 List
->L
.TotalAllocates
++;
580 Irp
= (PIRP
)InterlockedPopEntrySList(&List
->L
.ListHead
);
582 /* Check if the P List failed */
585 /* Let the balancer know */
586 List
->L
.AllocateMisses
++;
589 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].L
;
590 List
->L
.TotalAllocates
++;
591 Irp
= (PIRP
)InterlockedPopEntrySList(&List
->L
.ListHead
);
595 /* Check if we have to use the pool */
598 /* Did we try lookaside and fail? */
599 if (Flags
& IRP_ALLOCATED_FIXED_SIZE
) List
->L
.AllocateMisses
++;
601 /* Check if we should charge quota */
604 Irp
= ExAllocatePoolWithQuotaTag(NonPagedPool
| POOL_QUOTA_FAIL_INSTEAD_OF_RAISE
,
610 /* Allocate the IRP with no quota charge */
611 Irp
= ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_IRP
);
614 /* Make sure it was sucessful */
615 if (!Irp
) return NULL
;
619 /* In this case there is no charge quota */
620 Flags
&= ~IRP_QUOTA_CHARGED
;
623 /* Now Initialize it */
624 IoInitializeIrp(Irp
, Size
, StackSize
);
626 /* Set the Allocation Flags */
627 Irp
->AllocationFlags
= Flags
;
630 IOTRACE(IO_IRP_DEBUG
,
631 "%s - Allocated IRP %p with allocation flags %lx\n",
643 IopAllocateIrpMustSucceed(IN CCHAR StackSize
)
649 /* Try to get an IRP */
650 Irp
= IoAllocateIrp(StackSize
, FALSE
);
654 /* If we fail, start looping till we may get one */
659 /* First, sleep for 10ms */
660 Sleep
.QuadPart
= -10 * 1000 * 10;;
661 KeDelayExecutionThread(KernelMode
, FALSE
, &Sleep
);
663 /* Then, retry allocation */
664 Irp
= IoAllocateIrp(StackSize
, FALSE
);
677 IoBuildAsynchronousFsdRequest(IN ULONG MajorFunction
,
678 IN PDEVICE_OBJECT DeviceObject
,
681 IN PLARGE_INTEGER StartingOffset
,
682 IN PIO_STATUS_BLOCK IoStatusBlock
)
685 PIO_STACK_LOCATION StackPtr
;
688 Irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
689 if (!Irp
) return NULL
;
692 StackPtr
= IoGetNextIrpStackLocation(Irp
);
694 /* Write the Major function and then deal with it */
695 StackPtr
->MajorFunction
= (UCHAR
)MajorFunction
;
697 /* Do not handle the following here */
698 if ((MajorFunction
!= IRP_MJ_FLUSH_BUFFERS
) &&
699 (MajorFunction
!= IRP_MJ_SHUTDOWN
) &&
700 (MajorFunction
!= IRP_MJ_PNP
) &&
701 (MajorFunction
!= IRP_MJ_POWER
))
703 /* Check if this is Buffered IO */
704 if (DeviceObject
->Flags
& DO_BUFFERED_IO
)
706 /* Allocate the System Buffer */
707 Irp
->AssociatedIrp
.SystemBuffer
=
708 ExAllocatePoolWithTag(NonPagedPool
, Length
, TAG_SYS_BUF
);
709 if (!Irp
->AssociatedIrp
.SystemBuffer
)
711 /* Free the IRP and fail */
717 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
719 /* Handle special IRP_MJ_WRITE Case */
720 if (MajorFunction
== IRP_MJ_WRITE
)
722 /* Copy the buffer data */
723 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
, Buffer
, Length
);
727 /* Set the Input Operation flag and set this as a User Buffer */
728 Irp
->Flags
|= IRP_INPUT_OPERATION
;
729 Irp
->UserBuffer
= Buffer
;
732 else if (DeviceObject
->Flags
& DO_DIRECT_IO
)
734 /* Use an MDL for Direct I/O */
735 Irp
->MdlAddress
= IoAllocateMdl(Buffer
,
740 if (!Irp
->MdlAddress
)
742 /* Free the IRP and fail */
751 MmProbeAndLockPages(Irp
->MdlAddress
,
753 MajorFunction
== IRP_MJ_READ
?
754 IoWriteAccess
: IoReadAccess
);
756 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
758 /* Free the IRP and its MDL */
759 IoFreeMdl(Irp
->MdlAddress
);
763 _SEH2_YIELD(return NULL
);
769 /* Neither, use the buffer */
770 Irp
->UserBuffer
= Buffer
;
773 /* Check if this is a read */
774 if (MajorFunction
== IRP_MJ_READ
)
776 /* Set the parameters for a read */
777 StackPtr
->Parameters
.Read
.Length
= Length
;
778 StackPtr
->Parameters
.Read
.ByteOffset
= *StartingOffset
;
780 else if (MajorFunction
== IRP_MJ_WRITE
)
782 /* Otherwise, set write parameters */
783 StackPtr
->Parameters
.Write
.Length
= Length
;
784 StackPtr
->Parameters
.Write
.ByteOffset
= *StartingOffset
;
788 /* Set the Current Thread and IOSB */
789 Irp
->UserIosb
= IoStatusBlock
;
790 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
793 IOTRACE(IO_IRP_DEBUG
,
794 "%s - Built IRP %p with Major, Buffer, DO %lx %p %p\n",
808 IoBuildDeviceIoControlRequest(IN ULONG IoControlCode
,
809 IN PDEVICE_OBJECT DeviceObject
,
810 IN PVOID InputBuffer
,
811 IN ULONG InputBufferLength
,
812 IN PVOID OutputBuffer
,
813 IN ULONG OutputBufferLength
,
814 IN BOOLEAN InternalDeviceIoControl
,
816 IN PIO_STATUS_BLOCK IoStatusBlock
)
819 PIO_STACK_LOCATION StackPtr
;
823 Irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
824 if (!Irp
) return NULL
;
827 StackPtr
= IoGetNextIrpStackLocation(Irp
);
829 /* Set the DevCtl Type */
830 StackPtr
->MajorFunction
= InternalDeviceIoControl
?
831 IRP_MJ_INTERNAL_DEVICE_CONTROL
:
832 IRP_MJ_DEVICE_CONTROL
;
834 /* Set the IOCTL Data */
835 StackPtr
->Parameters
.DeviceIoControl
.IoControlCode
= IoControlCode
;
836 StackPtr
->Parameters
.DeviceIoControl
.InputBufferLength
= InputBufferLength
;
837 StackPtr
->Parameters
.DeviceIoControl
.OutputBufferLength
=
840 /* Handle the Methods */
841 switch (IO_METHOD_FROM_CTL_CODE(IoControlCode
))
844 case METHOD_BUFFERED
:
846 /* Select the right Buffer Length */
847 BufferLength
= InputBufferLength
> OutputBufferLength
?
848 InputBufferLength
: OutputBufferLength
;
850 /* Make sure there is one */
853 /* Allocate the System Buffer */
854 Irp
->AssociatedIrp
.SystemBuffer
=
855 ExAllocatePoolWithTag(NonPagedPool
,
858 if (!Irp
->AssociatedIrp
.SystemBuffer
)
860 /* Free the IRP and fail */
865 /* Check if we got a buffer */
868 /* Copy into the System Buffer */
869 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
874 /* Write the flags */
875 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
876 if (OutputBuffer
) Irp
->Flags
|= IRP_INPUT_OPERATION
;
878 /* Save the Buffer */
879 Irp
->UserBuffer
= OutputBuffer
;
883 /* Clear the Flags and Buffer */
885 Irp
->UserBuffer
= NULL
;
890 case METHOD_IN_DIRECT
:
891 case METHOD_OUT_DIRECT
:
893 /* Check if we got an input buffer */
896 /* Allocate the System Buffer */
897 Irp
->AssociatedIrp
.SystemBuffer
=
898 ExAllocatePoolWithTag(NonPagedPool
,
901 if (!Irp
->AssociatedIrp
.SystemBuffer
)
903 /* Free the IRP and fail */
908 /* Copy into the System Buffer */
909 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
913 /* Write the flags */
914 Irp
->Flags
= IRP_BUFFERED_IO
| IRP_DEALLOCATE_BUFFER
;
918 /* Clear the flags */
922 /* Check if we got an output buffer */
925 /* Allocate the System Buffer */
926 Irp
->MdlAddress
= IoAllocateMdl(OutputBuffer
,
931 if (!Irp
->MdlAddress
)
933 /* Free the IRP and fail */
942 MmProbeAndLockPages(Irp
->MdlAddress
,
944 IO_METHOD_FROM_CTL_CODE(IoControlCode
) ==
946 IoReadAccess
: IoWriteAccess
);
948 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
951 IoFreeMdl(Irp
->MdlAddress
);
953 /* Free the input buffer and IRP */
954 if (InputBuffer
) ExFreePool(Irp
->AssociatedIrp
.SystemBuffer
);
958 _SEH2_YIELD(return NULL
);
966 /* Just save the Buffer */
967 Irp
->UserBuffer
= OutputBuffer
;
968 StackPtr
->Parameters
.DeviceIoControl
.Type3InputBuffer
= InputBuffer
;
971 /* Now write the Event and IoSB */
972 Irp
->UserIosb
= IoStatusBlock
;
973 Irp
->UserEvent
= Event
;
975 /* Sync IRPs are queued to requestor thread's irp cancel/cleanup list */
976 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
977 IoQueueThreadIrp(Irp
);
980 IOTRACE(IO_IRP_DEBUG
,
981 "%s - Built IRP %p with IOCTL, Buffers, DO %lx %p %p %p\n",
996 IoBuildSynchronousFsdRequest(IN ULONG MajorFunction
,
997 IN PDEVICE_OBJECT DeviceObject
,
1000 IN PLARGE_INTEGER StartingOffset
,
1002 IN PIO_STATUS_BLOCK IoStatusBlock
)
1006 /* Do the big work to set up the IRP */
1007 Irp
= IoBuildAsynchronousFsdRequest(MajorFunction
,
1013 if (!Irp
) return NULL
;
1015 /* Set the Event which makes it Syncronous */
1016 Irp
->UserEvent
= Event
;
1018 /* Sync IRPs are queued to requestor thread's irp cancel/cleanup list */
1019 IoQueueThreadIrp(Irp
);
1028 IoCancelIrp(IN PIRP Irp
)
1031 PDRIVER_CANCEL CancelRoutine
;
1032 IOTRACE(IO_IRP_DEBUG
,
1033 "%s - Canceling IRP %p\n",
1036 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1038 /* Acquire the cancel lock and cancel the IRP */
1039 IoAcquireCancelSpinLock(&OldIrql
);
1042 /* Clear the cancel routine and get the old one */
1043 CancelRoutine
= (PVOID
)IoSetCancelRoutine(Irp
, NULL
);
1046 /* We had a routine, make sure the IRP isn't completed */
1047 if (Irp
->CurrentLocation
> (Irp
->StackCount
+ 1))
1049 /* It is, bugcheck */
1050 KeBugCheckEx(CANCEL_STATE_IN_COMPLETED_IRP
,
1052 (ULONG_PTR
)CancelRoutine
,
1057 /* Set the cancel IRQL And call the routine */
1058 Irp
->CancelIrql
= OldIrql
;
1059 CancelRoutine(IoGetCurrentIrpStackLocation(Irp
)->DeviceObject
, Irp
);
1063 /* Otherwise, release the cancel lock and fail */
1064 IoReleaseCancelSpinLock(OldIrql
);
1073 IoCancelThreadIo(IN PETHREAD Thread
)
1076 ULONG Retries
= 3000;
1077 LARGE_INTEGER Interval
;
1078 PLIST_ENTRY ListHead
, NextEntry
;
1082 /* Windows isn't using given thread, but using current. */
1083 Thread
= PsGetCurrentThread();
1085 IOTRACE(IO_IRP_DEBUG
,
1086 "%s - Canceling IRPs for Thread %p\n",
1090 /* Raise to APC to protect the IrpList */
1091 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1093 /* Start by cancelling all the IRPs in the current thread queue. */
1094 ListHead
= &Thread
->IrpList
;
1095 NextEntry
= ListHead
->Flink
;
1096 while (ListHead
!= NextEntry
)
1099 Irp
= CONTAINING_RECORD(NextEntry
, IRP
, ThreadListEntry
);
1104 /* Move to the next entry */
1105 NextEntry
= NextEntry
->Flink
;
1108 /* Wait 100 milliseconds */
1109 Interval
.QuadPart
= -1000000;
1111 /* Wait till all the IRPs are completed or cancelled. */
1112 while (!IsListEmpty(&Thread
->IrpList
))
1114 /* Now we can lower */
1115 KeLowerIrql(OldIrql
);
1117 /* Wait a short while and then look if all our IRPs were completed. */
1118 KeDelayExecutionThread(KernelMode
, FALSE
, &Interval
);
1121 * Don't stay here forever if some broken driver doesn't complete
1126 /* Print out a message and remove the IRP */
1127 DPRINT1("Broken driver did not complete!\n");
1128 IopDisassociateThreadIrp();
1131 /* Raise the IRQL Again */
1132 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1135 /* We're done, lower the IRQL */
1136 KeLowerIrql(OldIrql
);
1145 IoCallDriver(IN PDEVICE_OBJECT DeviceObject
,
1149 return IofCallDriver(DeviceObject
, Irp
);
1152 #define IoCallDriver IofCallDriver
1157 #undef IoCompleteRequest
1160 IoCompleteRequest(IN PIRP Irp
,
1161 IN CCHAR PriorityBoost
)
1163 /* Call the fastcall */
1164 IofCompleteRequest(Irp
, PriorityBoost
);
1167 #define IoCompleteRequest IofCompleteRequest
1174 IoEnqueueIrp(IN PIRP Irp
)
1176 /* This is the same as calling IoQueueThreadIrp */
1177 IoQueueThreadIrp(Irp
);
1185 IofCallDriver(IN PDEVICE_OBJECT DeviceObject
,
1188 PDRIVER_OBJECT DriverObject
;
1189 PIO_STACK_LOCATION StackPtr
;
1191 /* Make sure this is a valid IRP */
1192 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1194 /* Get the Driver Object */
1195 DriverObject
= DeviceObject
->DriverObject
;
1197 /* Decrease the current location and check if */
1198 Irp
->CurrentLocation
--;
1199 if (Irp
->CurrentLocation
<= 0)
1201 /* This IRP ran out of stack, bugcheck */
1202 KeBugCheckEx(NO_MORE_IRP_STACK_LOCATIONS
, (ULONG_PTR
)Irp
, 0, 0, 0);
1205 /* Now update the stack location */
1206 StackPtr
= IoGetNextIrpStackLocation(Irp
);
1207 Irp
->Tail
.Overlay
.CurrentStackLocation
= StackPtr
;
1209 /* Get the Device Object */
1210 StackPtr
->DeviceObject
= DeviceObject
;
1213 return DriverObject
->MajorFunction
[StackPtr
->MajorFunction
](DeviceObject
,
1219 IopClearStackLocation(IN PIO_STACK_LOCATION IoStackLocation
)
1221 IoStackLocation
->MinorFunction
= 0;
1222 IoStackLocation
->Flags
= 0;
1223 IoStackLocation
->Control
&= SL_ERROR_RETURNED
;
1224 IoStackLocation
->Parameters
.Others
.Argument1
= 0;
1225 IoStackLocation
->Parameters
.Others
.Argument2
= 0;
1226 IoStackLocation
->Parameters
.Others
.Argument3
= 0;
1227 IoStackLocation
->FileObject
= NULL
;
1235 IofCompleteRequest(IN PIRP Irp
,
1236 IN CCHAR PriorityBoost
)
1238 PIO_STACK_LOCATION StackPtr
, LastStackPtr
;
1239 PDEVICE_OBJECT DeviceObject
;
1240 PFILE_OBJECT FileObject
;
1247 NTSTATUS ErrorCode
= STATUS_SUCCESS
;
1248 PREPARSE_DATA_BUFFER DataBuffer
;
1249 IOTRACE(IO_IRP_DEBUG
,
1250 "%s - Completing IRP %p\n",
1254 /* Make sure this IRP isn't getting completed twice or is invalid */
1255 if ((Irp
->CurrentLocation
) > (Irp
->StackCount
+ 1))
1258 KeBugCheckEx(MULTIPLE_IRP_COMPLETE_REQUESTS
, (ULONG_PTR
)Irp
, 0, 0, 0);
1261 /* Some sanity checks */
1262 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1263 ASSERT(!Irp
->CancelRoutine
);
1264 ASSERT(Irp
->IoStatus
.Status
!= STATUS_PENDING
);
1265 ASSERT(Irp
->IoStatus
.Status
!= (NTSTATUS
)0xFFFFFFFF);
1267 /* Get the last stack */
1268 LastStackPtr
= (PIO_STACK_LOCATION
)(Irp
+ 1);
1269 if (LastStackPtr
->Control
& SL_ERROR_RETURNED
)
1271 /* Get the error code */
1272 ErrorCode
= PtrToUlong(LastStackPtr
->Parameters
.Others
.Argument4
);
1276 * Start the loop with the current stack and point the IRP to the next stack
1277 * and then keep incrementing the stack as we loop through. The IRP should
1278 * always point to the next stack location w.r.t the one currently being
1279 * analyzed, so completion routine code will see the appropriate value.
1280 * Because of this, we must loop until the current stack location is +1 of
1281 * the stack count, because when StackPtr is at the end, CurrentLocation is +1.
1283 for (StackPtr
= IoGetCurrentIrpStackLocation(Irp
),
1284 Irp
->CurrentLocation
++,
1285 Irp
->Tail
.Overlay
.CurrentStackLocation
++;
1286 Irp
->CurrentLocation
<= (Irp
->StackCount
+ 1);
1288 Irp
->CurrentLocation
++,
1289 Irp
->Tail
.Overlay
.CurrentStackLocation
++)
1291 /* Set Pending Returned */
1292 Irp
->PendingReturned
= StackPtr
->Control
& SL_PENDING_RETURNED
;
1294 /* Check if we failed */
1295 if (!NT_SUCCESS(Irp
->IoStatus
.Status
))
1297 /* Check if it was changed by a completion routine */
1298 if (Irp
->IoStatus
.Status
!= ErrorCode
)
1300 /* Update the error for the current stack */
1301 ErrorCode
= Irp
->IoStatus
.Status
;
1302 StackPtr
->Control
|= SL_ERROR_RETURNED
;
1303 LastStackPtr
->Parameters
.Others
.Argument4
= UlongToPtr(ErrorCode
);
1304 LastStackPtr
->Control
|= SL_ERROR_RETURNED
;
1308 /* Check if there is a Completion Routine to Call */
1309 if ((NT_SUCCESS(Irp
->IoStatus
.Status
) &&
1310 (StackPtr
->Control
& SL_INVOKE_ON_SUCCESS
)) ||
1311 (!NT_SUCCESS(Irp
->IoStatus
.Status
) &&
1312 (StackPtr
->Control
& SL_INVOKE_ON_ERROR
)) ||
1314 (StackPtr
->Control
& SL_INVOKE_ON_CANCEL
)))
1316 /* Clear the stack location */
1317 IopClearStackLocation(StackPtr
);
1319 /* Check for highest-level device completion routines */
1320 if (Irp
->CurrentLocation
== (Irp
->StackCount
+ 1))
1322 /* Clear the DO, since the current stack location is invalid */
1323 DeviceObject
= NULL
;
1327 /* Otherwise, return the real one */
1328 DeviceObject
= IoGetCurrentIrpStackLocation(Irp
)->DeviceObject
;
1331 /* Call the completion routine */
1332 Status
= StackPtr
->CompletionRoutine(DeviceObject
,
1336 /* Don't touch the Packet in this case, since it might be gone! */
1337 if (Status
== STATUS_MORE_PROCESSING_REQUIRED
) return;
1341 /* Otherwise, check if this is a completed IRP */
1342 if ((Irp
->CurrentLocation
<= Irp
->StackCount
) &&
1343 (Irp
->PendingReturned
))
1345 /* Mark it as pending */
1346 IoMarkIrpPending(Irp
);
1349 /* Clear the stack location */
1350 IopClearStackLocation(StackPtr
);
1354 /* Check if the IRP is an associated IRP */
1355 if (Irp
->Flags
& IRP_ASSOCIATED_IRP
)
1357 /* Get the master IRP and count */
1358 MasterIrp
= Irp
->AssociatedIrp
.MasterIrp
;
1359 MasterCount
= InterlockedDecrement(&MasterIrp
->AssociatedIrp
.IrpCount
);
1362 for (Mdl
= Irp
->MdlAddress
; Mdl
; Mdl
= NextMdl
)
1364 /* Go to the next one */
1365 NextMdl
= Mdl
->Next
;
1369 /* Free the IRP itself */
1372 /* Complete the Master IRP */
1373 if (!MasterCount
) IofCompleteRequest(MasterIrp
, PriorityBoost
);
1377 /* Check whether we have to reparse */
1378 if (Irp
->IoStatus
.Status
== STATUS_REPARSE
)
1380 if (Irp
->IoStatus
.Information
> IO_REMOUNT
)
1382 /* If that's a reparse tag we understand, save the buffer from deletion */
1383 if (Irp
->IoStatus
.Information
== IO_REPARSE_TAG_MOUNT_POINT
)
1385 ASSERT(Irp
->Tail
.Overlay
.AuxiliaryBuffer
!= NULL
);
1386 DataBuffer
= (PREPARSE_DATA_BUFFER
)Irp
->Tail
.Overlay
.AuxiliaryBuffer
;
1387 Irp
->Tail
.Overlay
.AuxiliaryBuffer
= NULL
;
1391 Irp
->IoStatus
.Status
= STATUS_IO_REPARSE_TAG_NOT_HANDLED
;
1396 /* Check if we have an auxiliary buffer */
1397 if (Irp
->Tail
.Overlay
.AuxiliaryBuffer
)
1400 ExFreePool(Irp
->Tail
.Overlay
.AuxiliaryBuffer
);
1401 Irp
->Tail
.Overlay
.AuxiliaryBuffer
= NULL
;
1404 /* Check if this is a Paging I/O or Close Operation */
1405 if (Irp
->Flags
& (IRP_PAGING_IO
| IRP_CLOSE_OPERATION
))
1407 /* Handle a Close Operation or Sync Paging I/O */
1408 if (Irp
->Flags
& (IRP_SYNCHRONOUS_PAGING_IO
| IRP_CLOSE_OPERATION
))
1410 /* Set the I/O Status and Signal the Event */
1411 Flags
= Irp
->Flags
& (IRP_SYNCHRONOUS_PAGING_IO
| IRP_PAGING_IO
);
1412 *Irp
->UserIosb
= Irp
->IoStatus
;
1413 KeSetEvent(Irp
->UserEvent
, PriorityBoost
, FALSE
);
1415 /* Free the IRP for a Paging I/O Only, Close is handled by us */
1416 if (Flags
) IoFreeIrp(Irp
);
1422 KeInitializeApc(&Irp
->Tail
.Apc
1423 &Irp
->Tail
.Overlay
.Thread
->Tcb
,
1424 Irp
->ApcEnvironment
,
1425 IopCompletePageWrite
,
1430 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1435 /* Not implemented yet. */
1436 UNIMPLEMENTED_DBGBREAK("Not supported!\n");
1440 /* Get out of here */
1444 /* Unlock MDL Pages, page 167. */
1445 Mdl
= Irp
->MdlAddress
;
1452 /* Check if we should exit because of a Deferred I/O (page 168) */
1453 if ((Irp
->Flags
& IRP_DEFER_IO_COMPLETION
) && !(Irp
->PendingReturned
))
1455 /* Restore the saved reparse buffer for the caller */
1456 if (Irp
->IoStatus
.Status
== STATUS_REPARSE
&&
1457 Irp
->IoStatus
.Information
== IO_REPARSE_TAG_MOUNT_POINT
)
1459 Irp
->Tail
.Overlay
.AuxiliaryBuffer
= (PCHAR
)DataBuffer
;
1463 * Return without queuing the completion APC, since the caller will
1464 * take care of doing its own optimized completion at PASSIVE_LEVEL.
1469 /* Get the thread and file object */
1470 Thread
= Irp
->Tail
.Overlay
.Thread
;
1471 FileObject
= Irp
->Tail
.Overlay
.OriginalFileObject
;
1473 /* Make sure the IRP isn't canceled */
1476 /* Initialize the APC */
1477 KeInitializeApc(&Irp
->Tail
.Apc
,
1479 Irp
->ApcEnvironment
,
1487 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1494 /* The IRP just got canceled... does a thread still own it? */
1497 /* Yes! There is still hope! Initialize the APC */
1498 KeInitializeApc(&Irp
->Tail
.Apc
,
1500 Irp
->ApcEnvironment
,
1508 KeInsertQueueApc(&Irp
->Tail
.Apc
,
1515 /* Nothing left for us to do, kill it */
1516 ASSERT(Irp
->Cancel
);
1517 IopCleanupIrp(Irp
, FileObject
);
1524 IopSynchronousCompletion(IN PDEVICE_OBJECT DeviceObject
,
1528 if (Irp
->PendingReturned
)
1529 KeSetEvent((PKEVENT
)Context
, IO_NO_INCREMENT
, FALSE
);
1530 return STATUS_MORE_PROCESSING_REQUIRED
;
1538 IoForwardIrpSynchronously(IN PDEVICE_OBJECT DeviceObject
,
1544 /* Check if next stack location is available */
1545 if (Irp
->CurrentLocation
< Irp
->StackCount
)
1547 /* No more stack location */
1551 /* Initialize event */
1552 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
1554 /* Copy stack location for next driver */
1555 IoCopyCurrentIrpStackLocationToNext(Irp
);
1557 /* Set a completion routine, which will signal the event */
1558 IoSetCompletionRoutine(Irp
, IopSynchronousCompletion
, &Event
, TRUE
, TRUE
, TRUE
);
1560 /* Call next driver */
1561 Status
= IoCallDriver(DeviceObject
, Irp
);
1563 /* Check if irp is pending */
1564 if (Status
== STATUS_PENDING
)
1566 /* Yes, wait for its completion */
1567 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
1570 /* Return success */
1579 IoFreeIrp(IN PIRP Irp
)
1581 PNPAGED_LOOKASIDE_LIST List
;
1582 PP_NPAGED_LOOKASIDE_NUMBER ListType
= LookasideSmallIrpList
;
1584 IOTRACE(IO_IRP_DEBUG
,
1585 "%s - Freeing IRPs %p\n",
1589 /* Make sure the Thread IRP list is empty and that it OK to free it */
1590 ASSERT(Irp
->Type
== IO_TYPE_IRP
);
1591 ASSERT(IsListEmpty(&Irp
->ThreadListEntry
));
1592 ASSERT(Irp
->CurrentLocation
>= Irp
->StackCount
);
1594 /* If this was a pool alloc, free it with the pool */
1595 if (!(Irp
->AllocationFlags
& IRP_ALLOCATED_FIXED_SIZE
))
1598 ExFreePoolWithTag(Irp
, TAG_IRP
);
1602 /* Check if this was a Big IRP */
1603 if (Irp
->StackCount
!= 1) ListType
= LookasideLargeIrpList
;
1606 Prcb
= KeGetCurrentPrcb();
1608 /* Use the P List */
1609 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].P
;
1610 List
->L
.TotalFrees
++;
1612 /* Check if the Free was within the Depth or not */
1613 if (ExQueryDepthSList(&List
->L
.ListHead
) >= List
->L
.Depth
)
1615 /* Let the balancer know */
1616 List
->L
.FreeMisses
++;
1618 /* Use the L List */
1619 List
= (PNPAGED_LOOKASIDE_LIST
)Prcb
->PPLookasideList
[ListType
].L
;
1620 List
->L
.TotalFrees
++;
1622 /* Check if the Free was within the Depth or not */
1623 if (ExQueryDepthSList(&List
->L
.ListHead
) >= List
->L
.Depth
)
1625 /* All lists failed, use the pool */
1626 List
->L
.FreeMisses
++;
1627 ExFreePoolWithTag(Irp
, TAG_IRP
);
1632 /* The free was within the Depth */
1635 InterlockedPushEntrySList(&List
->L
.ListHead
,
1646 IoGetPagingIoPriority(IN PIRP Irp
)
1648 IO_PAGING_PRIORITY Priority
;
1654 /* Check what priority it has */
1655 if (Flags
& IRP_CLASS_CACHE_OPERATION
)
1658 Priority
= IoPagingPriorityHigh
;
1660 else if (Flags
& IRP_PAGING_IO
)
1662 /* Normal priority */
1663 Priority
= IoPagingPriorityNormal
;
1667 /* Invalid -- not a paging IRP */
1668 Priority
= IoPagingPriorityInvalid
;
1671 /* Return the priority */
1680 IoGetRequestorProcess(IN PIRP Irp
)
1682 /* Return the requestor process */
1683 if (Irp
->Tail
.Overlay
.Thread
)
1685 if (Irp
->ApcEnvironment
== OriginalApcEnvironment
)
1687 return Irp
->Tail
.Overlay
.Thread
->ThreadsProcess
;
1689 else if (Irp
->ApcEnvironment
== AttachedApcEnvironment
)
1691 return (PEPROCESS
)Irp
->Tail
.Overlay
.Thread
->Tcb
.ApcState
.Process
;
1703 IoGetRequestorProcessId(IN PIRP Irp
)
1707 /* Return the requestor process' id */
1708 Process
= IoGetRequestorProcess(Irp
);
1709 if (Process
) return PtrToUlong(Process
->UniqueProcessId
);
1719 IoGetRequestorSessionId(IN PIRP Irp
,
1720 OUT PULONG pSessionId
)
1724 /* Return the session */
1725 if (Irp
->Tail
.Overlay
.Thread
)
1727 Process
= Irp
->Tail
.Overlay
.Thread
->ThreadsProcess
;
1728 *pSessionId
= MmGetSessionId(Process
);
1729 return STATUS_SUCCESS
;
1732 *pSessionId
= (ULONG
)-1;
1733 return STATUS_UNSUCCESSFUL
;
1741 IoGetTopLevelIrp(VOID
)
1743 /* Return the IRP */
1744 return (PIRP
)PsGetCurrentThread()->TopLevelIrp
;
1752 IoInitializeIrp(IN PIRP Irp
,
1753 IN USHORT PacketSize
,
1757 IOTRACE(IO_IRP_DEBUG
,
1758 "%s - Initializing IRP %p\n",
1761 RtlZeroMemory(Irp
, PacketSize
);
1763 /* Set the Header and other data */
1764 Irp
->Type
= IO_TYPE_IRP
;
1765 Irp
->Size
= PacketSize
;
1766 Irp
->StackCount
= StackSize
;
1767 Irp
->CurrentLocation
= StackSize
+ 1;
1768 Irp
->ApcEnvironment
= KeGetCurrentThread()->ApcStateIndex
;
1769 Irp
->Tail
.Overlay
.CurrentStackLocation
= (PIO_STACK_LOCATION
)(Irp
+ 1) + StackSize
;
1771 /* Initialize the Thread List */
1772 InitializeListHead(&Irp
->ThreadListEntry
);
1780 IoIsOperationSynchronous(IN PIRP Irp
)
1785 /* If the IRP requests synchronous paging I/O, if the file object was opened
1786 for synchronous I/O, if the IRP_SYNCHRONOUS_API flag is set in the IRP
1787 the operation is synchronous */
1788 SynchIO
= (IoGetCurrentIrpStackLocation(Irp
)->FileObject
->Flags
& FO_SYNCHRONOUS_IO
) ||
1789 (Irp
->Flags
& IRP_SYNCHRONOUS_API
) || (Irp
->Flags
& IRP_SYNCHRONOUS_PAGING_IO
);
1791 /* If the IRP requests asynchronous paging I/O, the operation is asynchronous,
1792 even if one of the above conditions is true */
1793 ForceAsync
= (Irp
->Flags
& IRP_PAGING_IO
) && !(Irp
->Flags
& IRP_SYNCHRONOUS_PAGING_IO
);
1795 /* Check the flags */
1796 if (SynchIO
&& !ForceAsync
)
1798 /* Synch API or Paging I/O is OK, as is Sync File I/O */
1802 /* Otherwise, it is an asynchronous operation. */
1811 IoIsValidNameGraftingBuffer(IN PIRP Irp
,
1812 IN PREPARSE_DATA_BUFFER ReparseBuffer
)
1823 IoMakeAssociatedIrp(IN PIRP Irp
,
1827 IOTRACE(IO_IRP_DEBUG
,
1828 "%s - Associating IRP %p\n",
1832 /* Allocate the IRP */
1833 AssocIrp
= IoAllocateIrp(StackSize
, FALSE
);
1834 if (!AssocIrp
) return NULL
;
1837 AssocIrp
->Flags
|= IRP_ASSOCIATED_IRP
;
1839 /* Set the Thread */
1840 AssocIrp
->Tail
.Overlay
.Thread
= Irp
->Tail
.Overlay
.Thread
;
1842 /* Associate them */
1843 AssocIrp
->AssociatedIrp
.MasterIrp
= Irp
;
1852 IoQueueThreadIrp(IN PIRP Irp
)
1854 IOTRACE(IO_IRP_DEBUG
,
1855 "%s - Queueing IRP %p\n",
1859 /* Use our inlined routine */
1860 IopQueueIrpToThread(Irp
);
1865 * Reference: Chris Cant's "Writing WDM Device Drivers"
1869 IoReuseIrp(IN OUT PIRP Irp
,
1872 UCHAR AllocationFlags
;
1873 IOTRACE(IO_IRP_DEBUG
,
1874 "%s - Reusing IRP %p\n",
1878 /* Make sure it's OK to reuse it */
1879 ASSERT(!Irp
->CancelRoutine
);
1880 ASSERT(IsListEmpty(&Irp
->ThreadListEntry
));
1882 /* Get the old flags */
1883 AllocationFlags
= Irp
->AllocationFlags
;
1885 /* Reinitialize the IRP */
1886 IoInitializeIrp(Irp
, Irp
->Size
, Irp
->StackCount
);
1888 /* Duplicate the data */
1889 Irp
->IoStatus
.Status
= Status
;
1890 Irp
->AllocationFlags
= AllocationFlags
;
1898 IoSetTopLevelIrp(IN PIRP Irp
)
1901 PsGetCurrentThread()->TopLevelIrp
= (ULONG_PTR
)Irp
;
1904 #if defined (_WIN64)
1908 IN PIRP Irp OPTIONAL
)