1 /* $Id: irp.c,v 1.33 2000/12/23 02:37:39 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>
38 #include <internal/debug.h>
40 /* FUNCTIONS ****************************************************************/
46 * FUNCTION: Releases a caller allocated irp
56 IoMakeAssociatedIrp (PIRP Irp
, CCHAR StackSize
)
58 * FUNCTION: Allocates and initializes an irp to associated with a master irp
61 * StackSize = Number of stack locations to be allocated in the irp
62 * RETURNS: The irp allocated
67 AssocIrp
= IoAllocateIrp(StackSize
,FALSE
);
73 IoInitializeIrp (PIRP Irp
, USHORT PacketSize
, CCHAR StackSize
)
75 * FUNCTION: Initalizes an irp allocated by the caller
77 * Irp = IRP to initalize
78 * PacketSize = Size in bytes of the IRP
79 * StackSize = Number of stack locations in the IRP
84 memset(Irp
, 0, PacketSize
);
85 Irp
->Size
= PacketSize
;
86 Irp
->StackCount
= StackSize
;
87 Irp
->CurrentLocation
= StackSize
;
88 Irp
->Tail
.Overlay
.CurrentStackLocation
= IoGetCurrentIrpStackLocation(Irp
);
93 IofCallDriver (PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
95 * FUNCTION: Sends an IRP to the next lower driver
99 PDRIVER_OBJECT DriverObject
;
100 PIO_STACK_LOCATION Param
;
102 // DPRINT("IoCallDriver(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
104 DriverObject
= DeviceObject
->DriverObject
;
105 Param
= IoGetNextIrpStackLocation(Irp
);
107 Irp
->Tail
.Overlay
.CurrentStackLocation
--;
108 Irp
->CurrentLocation
--;
110 // DPRINT("MajorFunction %d\n", Param->MajorFunction);
111 // DPRINT("DriverObject->MajorFunction[Param->MajorFunction] %x\n",
112 // DriverObject->MajorFunction[Param->MajorFunction]);
113 Status
= DriverObject
->MajorFunction
[Param
->MajorFunction
](DeviceObject
,
121 IoCallDriver (PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
123 return IofCallDriver (
132 IoAllocateIrp (CCHAR StackSize
, BOOLEAN ChargeQuota
)
134 * FUNCTION: Allocates an IRP
136 * StackSize = the size of the stack required for the irp
137 * ChargeQuota = Charge allocation to current threads quota
138 * RETURNS: Irp allocated
144 DbgPrint("IoAllocateIrp(StackSize %d ChargeQuota %d)\n",
147 KeDumpStackFrames(0,8);
152 // Irp = ExAllocatePoolWithQuota(NonPagedPool,IoSizeOfIrp(StackSize));
153 Irp
= ExAllocatePool(NonPagedPool
,IoSizeOfIrp(StackSize
));
157 Irp
= ExAllocatePool(NonPagedPool
,IoSizeOfIrp(StackSize
));
165 IoInitializeIrp(Irp
, IoSizeOfIrp(StackSize
), StackSize
);
167 // DPRINT("Irp %x Irp->StackPtr %d\n", Irp, Irp->CurrentLocation);
173 VOID
IopCompleteRequest(struct _KAPC
* Apc
,
174 PKNORMAL_ROUTINE
* NormalRoutine
,
175 PVOID
* NormalContext
,
176 PVOID
* SystemArgument1
,
177 PVOID
* SystemArgument2
)
179 DPRINT("IopCompleteRequest(Apc %x, SystemArgument1 %x, "
180 "(*SystemArgument1) %x\n", Apc
, SystemArgument1
,
182 IoSecondStageCompletion((PIRP
)(*SystemArgument1
),
183 (KPRIORITY
)(*SystemArgument2
));
189 IofCompleteRequest (PIRP Irp
, CCHAR PriorityBoost
)
191 * FUNCTION: Indicates the caller has finished all processing for a given
192 * I/O request and is returning the given IRP to the I/O manager
194 * Irp = Irp to be cancelled
195 * PriorityBoost = Increment by which to boost the priority of the
196 * thread making the request
203 DPRINT("IoCompleteRequest(Irp %x, PriorityBoost %d) Event %x THread %x\n",
204 Irp
,PriorityBoost
, Irp
->UserEvent
, PsGetCurrentThread());
206 for (i
=0;i
<Irp
->StackCount
;i
++)
208 if (Irp
->Stack
[i
].CompletionRoutine
!= NULL
)
210 Status
= Irp
->Stack
[i
].CompletionRoutine(
211 Irp
->Stack
[i
].DeviceObject
,
213 Irp
->Stack
[i
].CompletionContext
);
214 if (Status
== STATUS_MORE_PROCESSING_REQUIRED
)
219 if (Irp
->Stack
[i
].Control
& SL_PENDING_RETURNED
)
221 Irp
->PendingReturned
= TRUE
;
225 if (Irp
->PendingReturned
)
227 DPRINT("Dispatching APC\n");
228 Thread
= &Irp
->Tail
.Overlay
.Thread
->Tcb
;
229 KeInitializeApc(&Irp
->Tail
.Apc
,
238 KeInsertQueueApc(&Irp
->Tail
.Apc
,
240 (PVOID
)(ULONG
)PriorityBoost
,
242 DPRINT("Finished dispatching APC\n");
246 DPRINT("Calling completion routine directly\n");
247 IoSecondStageCompletion(Irp
,PriorityBoost
);
248 DPRINT("Finished completition routine\n");
255 IoCompleteRequest (PIRP Irp
, CCHAR PriorityBoost
)
264 /**********************************************************************
266 * IoIsOperationSynchronous@4
269 * Check if the I/O operation associated with the given IRP
273 * Irp Packet to check.
276 * TRUE if Irp's operation is synchronous; otherwise FALSE.
280 IoIsOperationSynchronous (
285 PFILE_OBJECT FileObject
= NULL
;
288 * Check the associated FILE_OBJECT's
291 FileObject
= Irp
->Tail
.Overlay
.OriginalFileObject
;
292 if (!(FO_SYNCHRONOUS_IO
& FileObject
->Flags
))
294 /* Check IRP's flags. */
296 if (!( (IRP_SYNCHRONOUS_API
| IRP_SYNCHRONOUS_PAGING_IO
)
304 * Check more IRP's flags.
307 if ( !(IRP_MOUNT_COMPLETION
& Flags
)
308 || (IRP_SYNCHRONOUS_PAGING_IO
& Flags
)
314 * Otherwise, it is an
315 * asynchronous operation.
339 Thread
= PsGetCurrentThread ();
340 Thread
->TopLevelIrp
->TopLevelIrp
= Irp
;
350 return (PsGetCurrentThread ()->TopLevelIrp
->TopLevelIrp
);