1 /* $Id: irp.c,v 1.36 2001/04/09 02:45:04 dwelch Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/irp.c
7 * PROGRAMMER: David Welch (welch@mcmail.com)
12 /* NOTES *******************************************************************
31 /* INCLUDES ****************************************************************/
33 #include <ddk/ntddk.h>
34 #include <internal/io.h>
35 #include <internal/ps.h>
36 #include <internal/pool.h>
39 #include <internal/debug.h>
41 /* GLOBALS *******************************************************************/
43 #define TAG_IRP TAG('I', 'R', 'P', ' ')
45 /* FUNCTIONS ****************************************************************/
51 * FUNCTION: Releases a caller allocated irp
61 IoMakeAssociatedIrp (PIRP Irp
, CCHAR StackSize
)
63 * FUNCTION: Allocates and initializes an irp to associated with a master irp
66 * StackSize = Number of stack locations to be allocated in the irp
67 * RETURNS: The irp allocated
72 AssocIrp
= IoAllocateIrp(StackSize
,FALSE
);
78 IoInitializeIrp (PIRP Irp
, USHORT PacketSize
, CCHAR StackSize
)
80 * FUNCTION: Initalizes an irp allocated by the caller
82 * Irp = IRP to initalize
83 * PacketSize = Size in bytes of the IRP
84 * StackSize = Number of stack locations in the IRP
89 memset(Irp
, 0, PacketSize
);
90 Irp
->Size
= PacketSize
;
91 Irp
->StackCount
= StackSize
;
92 Irp
->CurrentLocation
= StackSize
;
93 Irp
->Tail
.Overlay
.CurrentStackLocation
= IoGetCurrentIrpStackLocation(Irp
);
98 IofCallDriver (PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
100 * FUNCTION: Sends an IRP to the next lower driver
104 PDRIVER_OBJECT DriverObject
;
105 PIO_STACK_LOCATION Param
;
107 // DPRINT("IoCallDriver(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
109 DriverObject
= DeviceObject
->DriverObject
;
110 Param
= IoGetNextIrpStackLocation(Irp
);
112 Irp
->Tail
.Overlay
.CurrentStackLocation
--;
113 Irp
->CurrentLocation
--;
115 // DPRINT("MajorFunction %d\n", Param->MajorFunction);
116 // DPRINT("DriverObject->MajorFunction[Param->MajorFunction] %x\n",
117 // DriverObject->MajorFunction[Param->MajorFunction]);
118 Status
= DriverObject
->MajorFunction
[Param
->MajorFunction
](DeviceObject
,
126 IoCallDriver (PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
128 return IofCallDriver (
137 IoAllocateIrp (CCHAR StackSize
, BOOLEAN ChargeQuota
)
139 * FUNCTION: Allocates an IRP
141 * StackSize = the size of the stack required for the irp
142 * ChargeQuota = Charge allocation to current threads quota
143 * RETURNS: Irp allocated
149 DbgPrint("IoAllocateIrp(StackSize %d ChargeQuota %d)\n",
152 KeDumpStackFrames(0,8);
157 // Irp = ExAllocatePoolWithQuota(NonPagedPool,IoSizeOfIrp(StackSize));
158 Irp
= ExAllocatePoolWithTag(NonPagedPool
, IoSizeOfIrp(StackSize
),
163 Irp
= ExAllocatePoolWithTag(NonPagedPool
,IoSizeOfIrp(StackSize
),
172 IoInitializeIrp(Irp
, IoSizeOfIrp(StackSize
), StackSize
);
174 // DPRINT("Irp %x Irp->StackPtr %d\n", Irp, Irp->CurrentLocation);
180 VOID
IopCompleteRequest(struct _KAPC
* Apc
,
181 PKNORMAL_ROUTINE
* NormalRoutine
,
182 PVOID
* NormalContext
,
183 PVOID
* SystemArgument1
,
184 PVOID
* SystemArgument2
)
186 DPRINT("IopCompleteRequest(Apc %x, SystemArgument1 %x, "
187 "(*SystemArgument1) %x\n", Apc
, SystemArgument1
,
189 IoSecondStageCompletion((PIRP
)(*SystemArgument1
),
190 (KPRIORITY
)(*SystemArgument2
));
196 IofCompleteRequest (PIRP Irp
, CCHAR PriorityBoost
)
198 * FUNCTION: Indicates the caller has finished all processing for a given
199 * I/O request and is returning the given IRP to the I/O manager
201 * Irp = Irp to be cancelled
202 * PriorityBoost = Increment by which to boost the priority of the
203 * thread making the request
209 DPRINT("IoCompleteRequest(Irp %x, PriorityBoost %d) Event %x THread %x\n",
210 Irp
,PriorityBoost
, Irp
->UserEvent
, PsGetCurrentThread());
212 for (i
=0;i
<Irp
->StackCount
;i
++)
214 if (Irp
->Stack
[i
].CompletionRoutine
!= NULL
)
216 Status
= Irp
->Stack
[i
].CompletionRoutine(
217 Irp
->Stack
[i
].DeviceObject
,
219 Irp
->Stack
[i
].CompletionContext
);
220 if (Status
== STATUS_MORE_PROCESSING_REQUIRED
)
225 if (Irp
->Stack
[i
].Control
& SL_PENDING_RETURNED
)
227 Irp
->PendingReturned
= TRUE
;
230 if (Irp
->PendingReturned
)
232 DPRINT("Dispatching APC\n");
233 KeInitializeApc(&Irp
->Tail
.Apc
,
234 &Irp
->Tail
.Overlay
.Thread
->Tcb
,
242 KeInsertQueueApc(&Irp
->Tail
.Apc
,
244 (PVOID
)(ULONG
)PriorityBoost
,
246 DPRINT("Finished dispatching APC\n");
250 DPRINT("Calling completion routine directly\n");
251 IoSecondStageCompletion(Irp
,PriorityBoost
);
252 DPRINT("Finished completition routine\n");
259 IoCompleteRequest (PIRP Irp
, CCHAR PriorityBoost
)
268 /**********************************************************************
270 * IoIsOperationSynchronous@4
273 * Check if the I/O operation associated with the given IRP
277 * Irp Packet to check.
280 * TRUE if Irp's operation is synchronous; otherwise FALSE.
284 IoIsOperationSynchronous (
289 PFILE_OBJECT FileObject
= NULL
;
292 * Check the associated FILE_OBJECT's
295 FileObject
= Irp
->Tail
.Overlay
.OriginalFileObject
;
296 if (!(FO_SYNCHRONOUS_IO
& FileObject
->Flags
))
298 /* Check IRP's flags. */
300 if (!( (IRP_SYNCHRONOUS_API
| IRP_SYNCHRONOUS_PAGING_IO
)
308 * Check more IRP's flags.
311 if ( !(IRP_MOUNT_COMPLETION
& Flags
)
312 || (IRP_SYNCHRONOUS_PAGING_IO
& Flags
)
318 * Otherwise, it is an
319 * asynchronous operation.
343 Thread
= PsGetCurrentThread ();
344 Thread
->TopLevelIrp
->TopLevelIrp
= Irp
;
354 return (PsGetCurrentThread ()->TopLevelIrp
->TopLevelIrp
);